我正在尝试使用存储在根项目目录中的文件夹中的图像来测试卷积神经网络。我有一些代码给我一个错误,我不知道为什么或错误来自哪里。我将获得错误的代码和完整跟踪:
代码(文件夹中有6个jpg图像)
import os
for testing_image in os.listdir('testing_images/'):
full_detector('testing_images/' + testing_image)
完整错误追溯
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\Anaconda3\envs\deep_learning_1\lib\site-packages\PIL\Image.py in open(fp, mode)
2546 try:
-> 2547 fp.seek(0)
2548 except (AttributeError, io.UnsupportedOperation):
AttributeError: 'numpy.ndarray' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-123-68ab37d51b76> in <module>()
4 import os
5 for testing_image in os.listdir('testing_images/'):
----> 6 full_detector('testing_images/' + testing_image)
<ipython-input-119-a1a8a7870ff5> in full_detector(image_path)
3 def full_detector(image_path):
4 pred_image = cv2.imread(image_path)
----> 5 dog_breed = Resnet_Predict_Breed(pred_image)
6 if dog_detector(image_path):
7 print('this is dog with the breed : {}'.format(breed))
<ipython-input-118-c02818bc5801> in Resnet_Predict_Breed(img_path)
2 ### and returns the dog breed that is predicted by the model.
3 def Resnet_Predict_Breed(img_path):
----> 4 bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
5 predicted_vector = resnet_model.predict(bottleneck_feature)
6 return dog_names[np.argmax(predicted_vector)]
<ipython-input-18-3693dbe72dab> in path_to_tensor(img_path)
4 def path_to_tensor(img_path):
5 # loads RGB image as PIL.Image.Image type
----> 6 img = image.load_img(img_path, target_size=(224, 224))
7 # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
8 x = image.img_to_array(img)
~\Anaconda3\envs\deep_learning_1\lib\site-packages\keras\preprocessing\image.py in load_img(path, grayscale, target_size, interpolation)
347 raise ImportError('Could not import PIL.Image. '
348 'The use of `array_to_img` requires PIL.')
--> 349 img = pil_image.open(path)
350 if grayscale:
351 if img.mode != 'L':
~\Anaconda3\envs\deep_learning_1\lib\site-packages\PIL\Image.py in open(fp, mode)
2547 fp.seek(0)
2548 except (AttributeError, io.UnsupportedOperation):
-> 2549 fp = io.BytesIO(fp.read())
2550 exclusive_fp = True
2551
AttributeError: 'numpy.ndarray' object has no attribute 'read'
答案 0 :(得分:0)
在这一行:
dog_breed = Resnet_Predict_Breed(pred_image)
请改用image_path
,而无需使用cv2.imread
。