简体   繁体   English

谷歌云视觉不接受base64编码图像python

[英]Google cloud vision not accepting base64 encoded images python

I'm having a problem with base64 encoded images sent to Google Cloud Vision.我在将 base64 编码图像发送到 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.有趣的是,如果我通过 URI 发送图像,它工作正常,所以我怀疑我的编码方式有问题。

Here's the deal:这是交易:

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)

The response I get always is:我得到的回应总是:

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

If I try using URI instead:如果我尝试改用 URI:

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}],})

Response is ok...回复正常...

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

I've followed the recommended way to encode from Google我已经按照推荐的方式从谷歌编码

Any idea what is wrong here?知道这里有什么问题吗?

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 .我对 Google Cloud Vision 没有任何经验,但是在查看了他们的文档和示例之后,我的感觉是,关于图像数据的 base64 编码的链接文档页面适用于您自己创建和发送 HTTP 请求的情况,不使用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)

Well, if you still want to use base64 encoded image data, you will have to convert it into byte array using module before sending request to annotate image.好吧,如果您仍想使用 base64 编码的图像数据,则必须使用模块将其转换为字节数组,然后再发送对图像进行注释的请求。 This base64 to bytearray should be used when creating API or when you are receiving input in the form of encoded data without actual path/url.在创建 API 或以编码数据的形式接收没有实际路径/url 的输入时,应该使用这个 base64 到 bytearray。 Otherwise, use as it is by providing path or url as pointed out by @Leon.否则,通过提供@Leon 指出的路径或 url 原样使用。

import binascii
content = binascii.a2b_base64(base64_encoded_image_data)

pass this content as value for content argument in annotate_image method.将此内容作为 annotate_image 方法中内容参数的值传递。 Then, you will get a correct response.然后,您将得到正确的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM