我训练了一个模型,当我直接从前端发送图像时,它将图像分类为一切正常,但是现在我想从url获取图像并进行预测。
考虑我有一个类似 www.example.com/image.jpg
的网址
我的代码是:
url = 'https://icon2.cleanpng.com/20171220/gze/twitter-logo-png-5a3a1851372e76.0876249315137567532269680.jpg'
response = requests.get(url)
img = Image.open(BytesIO(response.content))
imgplot = plt.imshow(img)
img_test = tf.expand_dims(img, axis=0)
classes = np.argmax(parrotModel.predict(img_test), axis=-1)
print(str(classes[0]))
错误:
ValueError: Attempt to convert a value (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=626x417 at 0x7FD401D29898>) with an unsupported type (<class 'PIL.JpegImagePlugin.JpegImageFile'>) to a Tensor.```
答案 0 :(得分:1)
这里的问题是您需要将图像转换为numpy
数组。
img_test = tf.expand_dims(np.array(img), axis=0)
另外,在这里看看:ValueError: Attempt to convert a value (<PIL.PngImagePlugin.PngImageFile image mode=RGB size=519x600 at 0x7F95AD916518>) with an unsupported type,讨论了类似的问题。