Kus*_*ikh 5 python json google-api
任务:
问题:
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))
Run Code Online (Sandbox Code Playgroud)
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
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
]
Run Code Online (Sandbox Code Playgroud)
类型:
google.protobuf.internal.containers.RepeatedCompositeFieldContainer
尝试过:
小智 7
Google Vision 2.0 需要不同的代码,如果不更改代码,将抛出以下错误:
object has no attribute 'DESCRIPTOR'
Run Code Online (Sandbox Code Playgroud)
以下是如何使用 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'])
Run Code Online (Sandbox Code Playgroud)
注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
小智 6
该库返回普通的 protobuf 对象,可以使用以下方法将其序列化为 JSON:
from google.protobuf.json_format import MessageToJson
serialized = MessageToJson(original)
Run Code Online (Sandbox Code Playgroud)
这对我有用。
找到解决方案。它不能转换为 JSON,但可以像这样访问:
print(logos[0].bounding_poly.vertices[0].x)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1705 次 |
| 最近记录: |