keras AttributeError:模块“footer”没有属性“footer”

6vl6ewon  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(107)
def convert_to_grayscale(self):
    subdirectories = self.list_subdirectories()

    for subdirectory_name in subdirectories:
        subdirectory_path = os.path.join(self.base_dir, subdirectory_name)
        image_files = [os.path.join(subdirectory_path, file) for file in os.listdir(subdirectory_path) if os.path.isfile(os.path.join(subdirectory_path, file))]

        if image_files:
            for image_path in image_files:
                img = image.load_img(image_path)
                img_array = image.img_to_array(img)
                img_gray = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
                img_gray = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)  # Ensure it's 3-channel
                cv2.imwrite(image_path, img_gray)

    print("Grayscale conversion and saving completed.")

字符串
当调用此:image_processing.convert_to_grayscale()
我得到这个错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-feb64db6939a> in <cell line: 1>()
----> 1 image_processing.convert_to_grayscale()

<ipython-input-27-7a06c7c14fb3> in convert_to_grayscale(self)
     52             for image_path in image_files:
     53                 img = image.load_img(image_path)
---> 54                 if len(image.shape) == 3 and image.shape[2] == 3:
     55                     img = cv2.cvtColor(image.img_to_array(img), cv2.COLOR_BGR2GRAY)
     56 

AttributeError: module 'keras.utils' has no attribute 'shape'


解决这个问题,我尝试了不同的方法,我也使用chatgpt,但没有得到正确的答案。

erhoui1w

erhoui1w1#

image.shape替换为img.shape

52             for image_path in image_files:
     53                 img = image.load_img(image_path)
---> 54                 if len(image.shape) == 3 and image.shape[2] == 3:
     55                     img = cv2.cvtColor(image.img_to_array(img), cv2.COLOR_BGR2GRAY)

字符串
替换:

if len(image.shape) == 3 and image.shape[2] == 3:


有:

if len(img.shape) == 3 and img.shape[2] == 3:


但是,如果img是PIL映像,则可以检查mode属性:

if img.mode == 'RGB':
    img = cv2.cvtColor(...)

相关问题