gpt4 book ai didi

python - Google 的 Vision Api protobuf 响应对象到 Python 字典

转载 作者:行者123 更新时间:2023-11-28 19:03:20 30 4
gpt4 key购买 nike

我正在开展一个项目,我需要使用 Google 的 Vision API 分析图像并将响应发布到 Dynamodb 表。

我已成功实现 Vision API,但无法将其响应转换为 Python 字典。

这是我尝试过的:

       if form.is_valid():
obj = form
obj.imageFile = form.cleaned_data['imageFile']
obj.textFile = form.cleaned_data['textFile']
obj.save()
print(obj.imageFile)
# Process the image using Google's vision API
image_path = os.path.join(settings.MEDIA_ROOT, 'images/', obj.imageFile.name)
print(image_path)
image = vision_image_manager(image_path)
text_path = os.path.join(settings.MEDIA_ROOT, 'texts/', obj.textFile.name)
text = nlp_text_manager(text_path)
# print(image)
# print(text)
results = {
'imageResponse': image,
'textResult': text
}
print(results.values())
print(type(results))
post_to_dynamo_db(image, text)

这是 Vision api 实现:

def vision_image_manager(image_file):
# Instantiates a client
client = vision.ImageAnnotatorClient()
file_name = str(image_file)
with open(file_name, 'rb') as img_file:
content = img_file.read()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
return labels

这是 post_to_dynamo_db功能:

def post_to_dynamo_db(image, text):
session = boto3.Session(
aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY,
aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY
)
client = session.resource('dynamodb')
table = client.Table('basetbl')
result_dict = {
'image': image,
'text': text
}
json_dict = dict_to_item(result_dict)
# item = dict_to_item(result_dict)
table.put_item(
Item={
'id': int(generate_pid()),
'response_obj': json_dict
}
)

现在,它不返回任何错误,而是返回 response_obj未在数据库表中发布,因为它不是对象的正确形式,这里的问题是 <class 'google.protobuf.pyext._message.RepeatedCompositeContainer'>从 Google 的 API 返回的响应类型。

最佳答案

从 Vision API 获得适当的 python 友好响应的最佳方法是通过 Google 的 Discovery 服务使用此 API。

以下是这将如何为您工作:

def vision_image_manager(image_file):
# Instantiates a client
service = discovery.build('vision', 'v1', credentials=credentials)
# text.png is the image file.
file_name = str(image_file)
with open(file_name, 'rb') as image:
image_content = base64.b64encode(image.read())
service_request = service.images().annotate(body={
'requests': [{
'image': {
'content': image_content.decode('UTF-8')
},
'features': [{
'type': 'LABEL_DETECTION',
}]
}]
})
response = service_request.execute()
print(response['responses'])
res_dict = dict(response)
return res_dict

关于python - Google 的 Vision Api protobuf 响应对象到 Python 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160997/

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