dtype对象的图片数据在Tensorflow 2.0中无法转换为float错误。这是什么原因造成的?

Image data of dtype object cannot be converted to float error in Tensorflow 2.0. What's causing this?

按照教程不断提示图片数据转换错误。 img.shape 好像也有问题

我检查过文件位置是否准确,移动了一个文件来检查问题,现在我完全被难住了

# region Directory setting
dog_train_path = "./training_set/dogs/"
# endregion

# region Preparing and loading files
with zipfile2.ZipFile(train_file, 'r') as unzipped:
    unzipped.extractall()


def load_image(file_path):
    return cv2.imread(file_path)

def extract_numerical_label(file_name):
    return 1 if "dog" in file_name else 0


# region Load training labels and variables
image_files = os.listdir(dog_train_path)
train_images = [load_image(dog_train_path + file) for file in image_files]
train_labels = [extract_numerical_label(file) for file in image_files]


# region Preprocess data
def preprocess_image(img, side=96):
    plt.imshow(None)
    min_side = min(img.shape[0], img.shape[1])
    img = img[:min_side, :min_side]
    img = cv2.resize(img, (side, side))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return img / 255.0


# endregion

# region Test before and after images
preview_index = 1000
plt.subplot(1, 2, 1)
plt.imshow(train_images[preview_index])
plt.subplot(1, 2, 2)
plt.imshow(preprocess_image(train_images[preview_index]), cmap="gray")
# endregion

错误:

文件 "C:\Users\directory\matplotlib\image.py",第 685 行,在 set_data "float".format(self._A.dtype)) TypeError: dtype对象的图像数据无法转换为float

这个问题似乎既是由于我正在读取的目录中的错误,也是由于从数据集中读取的文件不正确。

请务必仔细检查您的内容和目录!