【发布时间】:2022-12-24 01:12:18
【问题描述】:
我想问一下我的代码是否有任何方法可以从包含大量水果图像的文件夹中随机选择图像。我的想法是使用随机图像测试我的 CNN 模型。这是我试过的代码,但出现如下所示的错误。
from keras.preprocessing import image
import numpy as np
import os
import random
test_img = random.choice(os.listdir("drive/My Drive/HAZIQ/TESTTEST/MODELTEST/"))
img = image.load_img(test_img, target_size = (208,256))
img = image.img_to_array(img, dtype=np.uint8)
img = np.array(img)/255.0
prediction = model.predict(img[np.newaxis, ...])
print("Probability: ",np.max(prediction[0], axis=-1))
predicted_class = class_names[np.argmax(prediction[0], axis=-1)]
print("Classified: ",predicted_class,'\n')
plt.axis('off')
plt.imshow(img.squeeze())
plt.title("Loaded Image")
错误
FileNotFoundError Traceback (most recent call > last) in () > 5 > 6 test_img = random.choice(os.listdir("drive/My Drive/HAZIQ/TESTTEST/MODELTEST/")) > ----> 7 img = image.load_img(test_img, target_size = (208,256)) > 8 img = image.img_to_array(img, dtype=np.uint8) > 9 img = np.array(img)/255.0 1 帧 /usr/local/lib/python3 .7/dist-packages/keras_preprocessing/image/utils.py > in load_img(path, grayscale, color_mode, target_size, interpolation) > 111 raise ImportError('Could not import PIL.Image.' > 112 '使用@987654323 @ 需要 PIL。') > --> 113 with open(path, 'rb') as f: > 114 img = pil_image.open(io.BytesIO(f.read())) > 115 if color_mode == 'grayscale ':FileNotFoundError:[Errno 2] 没有这样的文件或目录:'32660-3194-5469.jpg'
我可以确认“32660-3194-5469.jpg”在文件夹中。我不知道为什么它说没有这样的文件或目录。
我希望它是这样的
任何帮助都会很棒。
谢谢!
【问题讨论】:
标签: python tensorflow keras conv-neural-network google-colaboratory