愿景API:如何获得JSON-输出

Vision API: How to get JSON-output

我无法保存 Google 愿景 API 给出的输出。我正在使用 Python 并使用演示图像进行测试。我收到以下错误:

TypeError: [mid:...] + is not JSON serializable

我执行的代码:

import io
import os
import json
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
vision_client = vision.ImageAnnotatorClient()


# The name of the image file to annotate
file_name = os.path.join(
    os.path.dirname(__file__),
    'demo-image.jpg') # Your image path from current directory

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
    image = types.Image(content=content)

# Performs label detection on the image file
response = vision_client.label_detection(image=image)
labels = response.label_annotations


print('Labels:')
for label in labels:
    print(label.description, label.score, label.mid)

with open('labels.json', 'w') as fp:
   json.dump(labels, fp)

输出出现在屏幕上,但我不知道如何保存它。有人有什么建议吗?

也许您已经找到了问题的解决方案(如果是这样,我邀请您分享它作为对您自己问题的回答 post),但无论如何,让我分享一些可能对有类似问题的其他用户有用的笔记:

您可以使用 Python 中的 type() 函数进行检查,responsegoogle.cloud.vision_v1.types.AnnotateImageResponse type, while labels[i] is an object of google.cloud.vision_v1.types.EntityAnnotation type 的对象。 None 其中似乎有任何开箱即用的实现来将它们转换为 JSON,正如您正在尝试做的那样,所以我相信转换每个 [=16] 的最简单方法=]在labels中就是将它们变成Python字典,然后将它们全部分组到一个数组中,并将其转换为JSON。

为此,我在您的代码段中添加了一些简单的代码行:

[...]

label_dicts = [] # Array that will contain all the EntityAnnotation dictionaries

print('Labels:')
for label in labels:
    # Write each label (EntityAnnotation) into a dictionary
    dict = {'description': label.description, 'score': label.score, 'mid': label.mid}

    # Populate the array
    label_dicts.append(dict) 

with open('labels.json', 'w') as fp:
   json.dump(label_dicts, fp)

我能够使用以下函数保存输出:

# Save output as JSON
def store_json(json_input):
    with open(json_file_name, 'a') as f:
        f.write(json_input + '\n')

正如@dsesto 提到的,我必须定义一个字典。在这本词典中,我定义了我想在输出中保存的信息类型。

with open(photo_file, 'rb') as image:
    image_content = base64.b64encode(image.read())
    service_request = service.images().annotate(
        body={
            'requests': [{
                'image': {
                    'content': image_content
                },
                'features': [{
                    'type': 'LABEL_DETECTION',
                    'maxResults': 20,
                },
                    {
                        'type': 'TEXT_DETECTION',
                        'maxResults': 20,
                    },
                        {
                            'type': 'WEB_DETECTION',
                            'maxResults': 20,
                        }]
            }]
        })

当前Vision库中的对象缺少序列化功能(尽管这是个好主意)。

值得注意的是,他们即将为 Vision 发布一个完全不同的库(它现在在 master of vision 的 repo 上,虽然还没有发布到 PyPI),这将是可能的。请注意,这是一个向后不兼容的升级,因此会有一些(希望不要太多)转换工作。

那个库 returns 普通 protobuf 对象,可以序列化为 JSON 使用:

from google.protobuf.json_format import MessageToJson
serialized = MessageToJson(original)

您也可以使用 protobuf3-to-dict

Google

发布了一个库
from google.protobuf.json_format import MessageToJson

webdetect = vision_client.web_detection(blob_source) jsonObj = MessageToJson(webdetect)

仅供将来看到此内容的任何人参考,google-cloud-vision 2.0.0 已切换到使用使用不同 serialization/deserialization 代码的 proto-plus。如果在不更改代码的情况下升级到 2.0.0,您可能会遇到的错误是:

object has no attribute 'DESCRIPTOR'

使用google-cloud-vision 2.0.0, protobuf 3.13.0,这里有一个如何序列化和反序列化的例子(例子包括json和protobuf)

import io, json
from google.cloud import vision_v1
from google.cloud.vision_v1 import AnnotateImageResponse

with io.open('000048.jpg', 'rb') as image_file:
    content = image_file.read()

image = vision_v1.Image(content=content)
client = vision_v1.ImageAnnotatorClient()
response = client.document_text_detection(image=image)

# serialize / deserialize proto (binary)
serialized_proto_plus = AnnotateImageResponse.serialize(response)
response = AnnotateImageResponse.deserialize(serialized_proto_plus)
print(response.full_text_annotation.text)

# serialize / deserialize json
response_json = AnnotateImageResponse.to_json(response)
response = json.loads(response_json)
print(response['fullTextAnnotation']['text'])

注意 1:proto-plus 不支持转换为 snake_case 名称,protobuf 支持 preserving_proto_field_name=True。所以目前没有办法将字段名称从 response['full_text_annotation'] 转换为 response['fullTextAnnotation'] 为此有一个 open 关闭的功能请求:googleapis/proto-plus-python#109

注释 2:如果 x=0,google 视觉 api 没有 return x 坐标。如果 x 不存在,protobuf 将默认 x=0。在使用 MessageToJson() 的 python vision 1.0.0 中,这些 x 值未包含在 json 中,但现在使用 python vision 2.0.0 和 .To_Json()这些值包括为 x:0