Ale*_*oVK 5 python google-cloud-platform google-cloud-vision
发送到Google Cloud Vision的base64编码图像出现问题。有趣的是,如果我通过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)
Run Code Online (Sandbox Code Playgroud)
我总是得到的答复是:
error {
code: 3
message: "Bad image data."
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用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}],})
Run Code Online (Sandbox Code Playgroud)
响应还可以...
label_annotations {
mid: "/m/0168g6"
description: "factory"
score: 0.7942917943000793
}
label_annotations {
mid: "/m/03rnh"
description: "industry"
score: 0.7761002779006958
}
Run Code Online (Sandbox Code Playgroud)
知道这里有什么问题吗?
我对 Google Cloud Vision 没有任何经验,但是在查看了他们的文档和示例后,我的感觉是有关图像数据的 base64 编码的链接文档页面适用于您自己创建和发送 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2138 次 |
| 最近记录: |