无法读取图像路径。属性错误: 'list'对象没有属性'read'



我正在预测花朵的图像标签。我正在通过 argparser 将图像的路径作为参数传递。但是我收到以下错误。

我该如何解决这个问题?

python predict.py --input_img 'flowers/test/99/image_07833.jpg'

checkpoint loaded
image path is ['flowers/test/99/image_07833.jpg']
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/PIL/Image.py", line 2481, in open
fp.seek(0)
AttributeError: 'list' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "predict.py", line 40, in <module>
top_probs, top_labels, top_flowers = utils.predict(image_path, model, cat_to_name, topk)
File "/home/workspace/ImageClassifier/utils.py", line 249, in predict
image = process_image(image_path)
File "/home/workspace/ImageClassifier/utils.py", line 213, in process_image
img = Image.open(image)
File "/opt/conda/lib/python3.6/site-packages/PIL/Image.py", line 2483, in open
fp = io.BytesIO(fp.read())
AttributeError: 'list' object has no attribute 'read'

在将参数input_img添加到参数解析器时,您可能会设置nargs=1
像这样:

parser.add_argument('--input_img', type=open, nargs=1)

从文档中:

请注意,nargs=1 生成一个项目的列表。这与 默认值,其中项由其自身生成。

只需删除nargs

parser.add_argument('--input_img', type=open)

最新更新