繁体   English   中英

Can Keras.load_img可以处理文件夹中的多个图像

[英]Can Keras.load_img work on multiple images in folder

我正在研究一种使用Keras对狗和猫的图片进行分类的算法。 根据下面的代码,当我输入一个图像时,这是有效的,但我的问题是Keras.load_img可以处理文件夹中的多个图像吗? 当我尝试这个时,它表示列表对象没有搜索和读取属性。

from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'dog'
else:
prediction = 'cat'

我认为有一个解决方案,你可以试试这个:

images = []
for img in os.listdir(folder_path):
    img = image.load_img(img, target_size=(img_width, img_height))
    img = img.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    images.append(img)
images = np.vstack(images)
classes = classifier.predict(images, batch_size=10)
print(classes)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM