【问题标题】:Google cloud vision not accepting base64 encoded images python谷歌云视觉不接受base64编码图像python
【发布时间】:2018-01-23 12:47:52
【问题描述】:

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

这是交易:

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:

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 的 recommended way to encode

知道这里有什么问题吗?

【问题讨论】:

  • Base64 != 64 位。这些是非常不同的事情。
  • 试试content = base64.b64encode(image_content).decode()
  • @Leon 我得到这个“TypeError: '/9j/4AAQSkZJRgABAQEA8ADwAAD/4gJASUNDX1BST0ZJTEUAAQEAAAIwQURCRQIQAABtbnRyUkdCIFhZWiAHzwAGAAMAAAAAAAB 的类型为 str,但预期为以下之一:字节”
  • 您是否尝试过像this example 中的另一种方法,其中编码显然是在types.Image() 构造函数中自动完成的。
  • @Leon 我不想使用他们所谓的快捷方式,我想使用注释器客户端,所以我选择分析类型,更重要的是,每个请求发送多个图像跨度>

标签: python google-cloud-platform google-cloud-vision


【解决方案1】:

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

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)

【讨论】:

  • 正确!你猜对了 :) 更多信息在这里,对于好奇的人googlecloudplatform.github.io/google-cloud-python/stable/vision/…
  • @AlejandroVK 这有点晚了,但我正在尝试做同样的事情,我正在寻找如何去做。您提供的链接已损坏。您能否刷新它或分享如何对要在 Vision API 中使用的图像进行编码?
  • @Aka 让我知道这是否有帮助 gist.github.com/internetmosquito/…
  • @AlejandroVK 谢谢。
  • AttributeError: module 'google.cloud.vision' has no attribute 'enums' 我怎样才能摆脱这个错误?
【解决方案2】:

好吧,如果您仍想使用 base64 编码的图像数据,则必须在发送请求注释图像之前使用模块将其转换为字节数组。 在创建 API 或以编码数据形式接收没有实际路径/url 的输入时,应使用此 base64 到 bytearray。 否则,通过提供@Leon 指出的路径或 url 按原样使用。

import binascii
content = binascii.a2b_base64(base64_encoded_image_data)

将此 content 作为 annotate_image 方法中 content 参数的值传递。 然后,您将得到正确的响应。

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    相关资源
    最近更新 更多