【发布时间】: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