将 Google Vision API 响应转换为 JSON [英] Convert Google Vision API response to JSON

查看:53
本文介绍了将 Google Vision API 响应转换为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:

  • 将 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

google.protobuf.internal.containers.RepeatedCompositeFieldContainer

尝试过:

Python 中的 Protobuf 到 json

推荐答案

找到解决方案.它不能转换为 JSON,但可以这样访问:

Found solution. It can not be converted to JSON but can be accessed like this:

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

这篇关于将 Google Vision API 响应转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆