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

查看:174
本文介绍了将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

尝试:

Protobuf转换为python中的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天全站免登陆