【问题标题】:getting image from url and want to predict image from own classification model从 url 获取图像并希望从自己的分类模型中预测图像
【发布时间】:2020-10-25 13:57:27
【问题描述】:

我训练了一个模型,该模型可以对图像进行分类,当我直接从前端发送图像时一切正常,但现在我想从 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.```

【问题讨论】:

    标签: python tensorflow keras computer-vision


    【解决方案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,讨论了类似的问题。

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 1970-01-01
      • 2020-08-17
      • 2020-04-16
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      相关资源
      最近更新 更多