编程语言
首页 > 编程语言> > python-将Google Vision API响应转换为JSON

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

尝试过:

Protobuf to json in python

解决方法:

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

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

标签:google-api,json,python
来源: https://codeday.me/bug/20191109/2013137.html