我有一个名为“seg_train”的文件夹,其中有 6 个带标签的文件夹:建筑物、树木、街道、冰川、森林、海洋和山脉。我正在尝试使用 open cv 读取这些文件夹中的所有图像,为此我编写了一个函数,但我不知道我做错了什么。这6个文件夹总共大约有14000个文件,但我编写的函数只从一个文件夹中读取2300个文件。能否请你帮忙? 这是Python代码
result- Shape of Images: (2382, 150, 150, 3)
Shape of Labels: (2382,)
i was expecting (14000,150,150,3)
for image_file in os.listdir(r'C:/Users/dhvan/Desktop/intel-image-classification/seg_train/seg_train/'+ labels):
image = cv2.imread(r'C:/Users/dhvan/Desktop/intel-image-classification/seg_train/seg_train/' + labels + r'/'+ image_file)
image = cv2.resize(image,(150,150))
Images.append(image)
Labels.append(label)
return shuffle(Images,Labels,random_state=812490023)
最佳答案
我建议使用下面的代码。另外,我认为如果您尝试读取 14000 个大小为 (150,150,3) 的图像,您将收到资源耗尽错误,因为这将使用非常大的数量 的内存。如果您正在构建 CNN 分类器,我建议您使用 Keras ImageDataGenerator.flowfrom 目录批量读取图像,文档位于此处 https://keras.io/preprocessing/image/
import os
dir=r'C:/Users/dhvan/Desktop/intel-image-classification/seg_train/seg_train'
path_to_labels=os.path.join (dir, 'labels')
dir_list=os.listdir(path_to_labels)
for images in dir_list:
path_to_images=os.path.join (path_to_labels, images)
cv2.imread(path_to_images)
关于python - 使用 open cv 读取图像时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60252227/