【问题标题】:Convert Google Vision API response to JSON将 Google Vision API 响应转换为 JSON
【发布时间】:2018-07-15 08:54:04
【问题描述】:

任务:

  • 将 Google Vision API 响应转换为 JSON

问题:

  • API 调用的返回值不是 JSON 格式

Python 函数

def detect_logos(path):
"""Detects logos in the file."""
client = vision.ImageAnnotatorClient()

# [START migration_logo_detection]
with io.open(path, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

response = client.logo_detection(image=image)
logos = response.logo_annotations

print('Logos:')
print(logos)
print(type(logos))

Google 在线 JSON

"logoAnnotations": [
{
  "mid": "/m/02wwnh",
  "description": "Maxwell House",
  "score": 0.41142157,
  "boundingPoly": {
    "vertices": [
      {
        "x": 74,
        "y": 129
      },
      {
        "x": 161,
        "y": 129
      },
      {
        "x": 161,
        "y": 180
      },
      {
        "x": 74,
        "y": 180
      }
    ]
  }
}

Google 回复(列表)

 [mid: "/m/02wwnh"
description: "Maxwell House"
score: 0.4114215672016144
bounding_poly {
  vertices {
    x: 74
    y: 129
  }
  vertices {
    x: 161
    y: 129
  }
  vertices {
    x: 161
    y: 180
  }
  vertices {
    x: 74
    y: 180
  }
}
]

类型:

google.protobuf.internal.containers.RepeatedCompositeFieldContainer

尝试过:

Protobuf to json in python

【问题讨论】:

  • 您编写的 python 脚本是否只返回 Google 响应(列表),它是不带逗号返回的,还是只是您在这里的方式?
  • 是的,它没有逗号,正是我粘贴的内容
  • 你能在一个小的python脚本中提供一个简化版本的调用吗...我会运行它并找出问题的根源
  • 当然,请检查。该函数调用视觉 API,我得到了我提到的响应。

标签: python json google-api


【解决方案1】:

Google vision 2.0 需要不同的代码,如果代码没有改变,会抛出以下错误:

object has no attribute 'DESCRIPTOR'

这是一个如何使用 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'] 对此有一个开放的功能请求:googleapis/proto-plus-python#109

注意 2:如果 x=0,google vision api 不会返回 x 坐标。如果 x 不存在,protobuf 将默认 x=0。在使用 MessageToJson() 的 python vision 1.0.0 中,这些 x 值不包含在 json 中,但现在使用 python vision 2.0.0 和 .To_Json() 这些值包含为 x:0

【讨论】:

  • 这也解决了我遇到的日语编码问题!
【解决方案2】:

该库返回纯 protobuf 对象,可以使用以下方法将其序列化为 JSON:

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

这对我有用。

【讨论】:

  • 它返回此错误:AttributeError: 'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'DESCRIPTOR'
  • MessageToJson(response._pb) 为我工作。 credit to Martin J
【解决方案3】:

找到解决方案。不能转成 JSON 但可以这样访问:

print(logos[0].bounding_poly.vertices[0].x)

【讨论】:

  • 感谢@Kushal 为我解惑。我在使用 Vision API 时遇到了很多困难。这对我有用。
猜你喜欢
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2021-08-02
  • 1970-01-01
相关资源
最近更新 更多