gpt4 book ai didi

python - 如何使用 python 查询本地镜像的 Google Cloud Vision API?

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:56 26 4
gpt4 key购买 nike

我想使用本地存储的图像向 Google 视觉 API 发送请求。我遵循“Python Client for Google Cloud Vision¶ ”指南,特别是“注释图像”下的代码示例。为了构建我的请求,我遵循“Base64 Encoding”中的示例。

这是我编写的代码:

from google.cloud.vision import ImageAnnotatorClient
import os
import base64
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'keys/sc_google.json'

def encode_image(image_path):
with open(image_path, "rb") as f:
image_content = f.read()
return base64.b64encode(image_content)

client = ImageAnnotatorClient()
image_path = 'images/happy_baby3.png'
query = {
"requests": [
{
"image": {
"content": encode_image(image_path)
},
"features": [
{
"type": "FACE_DETECTION",
"maxResults": 10
}
]
}
]
}
response = client.annotate_image(query)
print(response.annotations)

当我运行代码时,我收到以下错误消息:

Traceback (most recent call last):
File "/mnt/c/Users/nomis/PycharmProjects/hello_web_app/emotion_detection.py", line 29, in <module>
response = client.annotate_image(query)
File "/usr/local/lib/python3.6/dist-packages/google/cloud/vision_helpers/__init__.py", line 55, in annotate_image
image = protobuf.get(request, "image")
File "/usr/local/lib/python3.6/dist-packages/google/api_core/protobuf_helpers.py", line 192, in get
raise KeyError(key)
KeyError: 'image'

我也尝试过this Example但出现错误“在‘types.py’中找不到引用‘图像’。

您能否解释一下我做错了什么,并提供一个关于如何使用本地镜像查询 Google Vision API 的最小工作示例?

最佳答案

在您链接的示例中,没有请求数组。试试这个:

query = {
"image": {
"content": encode_image(image_path)
},
"features": [
{
"type": "FACE_DETECTION",
"maxResults": 10
}
]
}

关于python - 如何使用 python 查询本地镜像的 Google Cloud Vision API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58971073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com