Google云视觉不接受base64编码图像python
我在发送到Google Cloud Vision的base64编码图像时遇到问题。有趣的是,如果我通过URI发送图像,它可以正常工作,所以我怀疑我编码的方式有什么问题。Google云视觉不接受base64编码图像python
这里的交易:
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
}
我跟着recommended way to encode从谷歌
任何想法,这里有什么问题?
我对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)
正确!你是对的:)这里有更多的信息,好奇心https://googlecloudplatform.github.io/google-cloud-python/stable/vision/gapic/types.html#google.cloud.vision_v1.types.Image。内容 – AlejandroVK
@AlejandroVK这有点晚,但我正在尝试做同样的事情,我正在寻找如何做到这一点。您提供的链接已损坏。你能刷新它还是分享我如何编码要在Vision API中使用的图像? – Aka
@Aka让我知道这是否有帮助https://gist.github.com/internetmosquito/b1c1f00ac99d9aa1f79cfdc21be36b5b – AlejandroVK
Base64!= 64位。这些是非常不同的事情。 – Thomas
尝试'含量= base64.b64encode(image_content).decode()' – Leon
@Leon我得到这个 “类型错误:“/ 9J/4AAQSkZJRgABAQEA8ADwAAD/4gJASUNDX1BST0ZJTEUAAQEAAAIwQURCRQIQAABtbnRyUkdCIFhZWiAHzwAGAAMAAAAAAAB具有类型海峡,但预期中的一种:字节 ” – AlejandroVK