从Python中的Cloud Vision API解析响应(批处理)

时间:2018-02-18 23:51:06

标签: python protocol-buffers google-cloud-vision

我想将大量请求传递给Google Cloud Vision API以进行标签检测 - 在Python中。一切顺利。我的问题是:如何阅读结果?

以下是请求的示例:

{'image': {'source': {'image_uri': 'my obj on gs'}},
 'features': [{'type': vision.enums.Feature.Type.LABEL_DETECTION}],}

和代码:

from google.cloud import vision

client = vision.ImageAnnotatorClient()
responses = client.batch_annotate_images(requests)

我使用batch_annotate_images发送请求列表并获取google.cloud.vision_v1.types.BatchAnnotateImagesResponse。我无法遍历响应,也没有一种方法可以成为明显的候选者。以下是可用的内容:

['ByteSize',
 'Clear',
 'ClearExtension',
 'ClearField',
 'CopyFrom',
 'DESCRIPTOR',
 'DiscardUnknownFields',
 'Extensions',
 'FindInitializationErrors',
 'FromString',
 'HasExtension',
 'HasField',
 'IsInitialized',
 'ListFields',
 'MergeFrom',
 'MergeFromString',
 'ParseFromString',
 'RESPONSES_FIELD_NUMBER',
 'RegisterExtension',
 'SerializePartialToString',
 'SerializeToString',
 'SetInParent',
 'WhichOneof]

我唯一能想到的是:

import json
from google.protobuf.json_format import MessageToJson

json_result = MessageToJson(responses)
dict_result = json.loads(json_result)

它可以解决问题,但与标记single photo相比,似乎相当复杂。

有更好的方法吗?

如果没有,我假设我在batch_annotate_images列表中传递的请求的顺序与我得到的响应完全匹配?

1 个答案:

答案 0 :(得分:0)

使用getResponse方法获取“annotatedImagesResponse”的Java列表,您可以从中解析所需的信息。

编辑:您从该库中获得的annotatedImagesResponse中没有“getResponse”方法。响应以property代替。你应该可以从中获得RepeatedField