谷歌云视觉不接受base64编码的图像python [英] Google cloud vision not accepting base64 encoded images python

查看:14
本文介绍了谷歌云视觉不接受base64编码的图像python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 base64 编码图像发送到 Google Cloud Vision 时遇到问题.有趣的是,如果我通过 URI 发送图像,它工作正常,所以我怀疑我的编码方式有问题.

I'm having a problem with base64 encoded images sent to Google Cloud Vision. Funny thing is that if I send the image via URI, it works fine, so I suspect there is something wrong the way I'm encoding.

交易如下:

from google.cloud import vision
import base64
client = vision.ImageAnnotatorClient()
image_path ='8720911950_91828a2aeb_b.jpg'
with open(image_path, 'rb') as image:
    image_content = image.read()
    content = base64.b64encode(image_content)   
    response = client.annotate_image({'image': {'content': content}, 'features': [{'type': vision.enums.Feature.Type.LABEL_DETECTION}],})
    print(response)

我总是得到的回应是:

error {
  code: 3
  message: "Bad image data."
}

如果我尝试使用 URI:

If I try using URI instead:

response = client.annotate_image({'image': {'source': {'image_uri': 'https://farm8.staticflickr.com/7408/8720911950_91828a2aeb_b.jpg'}}, 'features': [{'type': vision.enums.Feature.Type.LABEL_DETECTION}],})

回复正常...

label_annotations {
  mid: "/m/0168g6"
  description: "factory"
  score: 0.7942917943000793
}
label_annotations {
  mid: "/m/03rnh"
  description: "industry"
  score: 0.7761002779006958
}

我遵循了 Google 提供的推荐的编码方式

I've followed the recommended way to encode from Google

知道这里出了什么问题吗?

Any idea what is wrong here?

推荐答案

我对 Google Cloud Vision 没有任何经验,但是在查看他们的文档和示例后,我的感觉是链接的 关于图像数据的 base64 编码的文档页面 适用于您自己创建和发送 HTTP 请求时的情况,没有使用 vision.ImageAnnotatorClient.后者似乎自动对图像数据进行编码,因此在您的示例中应用了双重编码.因此,我认为您应该从代码中删除编码步骤:

I don't have any experience with Google Cloud Vision, however after looking at their documentation and examples, my feeling is that the linked documentation page about base64 encoding of image data is for the case when you create and send the HTTP requests on your own, without using vision.ImageAnnotatorClient. The latter seems to encode the image data automatically, hence in your example double encoding is applied. Therefore I believe that you should remove the encoding step from your code:

from google.cloud import vision
import base64
client = vision.ImageAnnotatorClient()
image_path ='8720911950_91828a2aeb_b.jpg'
with open(image_path, 'rb') as image:
    content = image.read()
    response = client.annotate_image({'image': {'content': content}, 'features': [{'type': vision.enums.Feature.Type.LABEL_DETECTION}],})
    print(response)

这篇关于谷歌云视觉不接受base64编码的图像python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆