我无法保存Google Vision API提供的输出。我正在使用Python并使用演示图像进行测试。我收到以下错误:
TypeError: [mid:...] + is not JSON serializable
我执行的代码:
import io
import os
import json
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
vision_client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'demo-image.jpg') # Your image path from current directory
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = vision_client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description, label.score, label.mid)
with open('labels.json', 'w') as fp:
json.dump(labels, fp)
输出显示在屏幕上,但我不确切知道如何保存它。有人有什么建议吗?
也许你已经能够找到你的问题的解决方案(如果是这种情况,我邀请你分享它作为你自己的帖子的答案),但无论如何,让我分享一些可能有用的笔记其他有类似问题的用户:
正如您可以使用Python中的type()
函数检查,response
是google.cloud.vision_v1.types.AnnotateImageResponse
type的对象,而labels[i]
是google.cloud.vision_v1.types.EntityAnnotation
type的对象。他们似乎没有任何开箱即用的实现来将它们转换为JSON,正如你想要的那样,所以我相信在EntityAnnotation
中转换每个labels
的最简单方法是将它们变成Python词典,然后将它们全部分组到一个数组中,并将其转换为JSON。
为此,我在您的代码段中添加了一些简单的代码行:
[...]
label_dicts = [] # Array that will contain all the EntityAnnotation dictionaries
print('Labels:')
for label in labels:
# Write each label (EntityAnnotation) into a dictionary
dict = {'description': label.description, 'score': label.score, 'mid': label.mid}
# Populate the array
label_dicts.append(dict)
with open('labels.json', 'w') as fp:
json.dump(label_dicts, fp)
我能够使用以下函数保存输出:
# Save output as JSON
def store_json(json_input):
with open(json_file_name, 'a') as f:
f.write(json_input + '\n')
正如@dsesto所提到的,我必须定义一本字典。在这本词典中,我已经定义了我想在输出中保存的信息类型。
with open(photo_file, 'rb') as image:
image_content = base64.b64encode(image.read())
service_request = service.images().annotate(
body={
'requests': [{
'image': {
'content': image_content
},
'features': [{
'type': 'LABEL_DETECTION',
'maxResults': 20,
},
{
'type': 'TEXT_DETECTION',
'maxResults': 20,
},
{
'type': 'WEB_DETECTION',
'maxResults': 20,
}]
}]
})
当前Vision库中的对象缺少序列化功能(尽管这是一个好主意)。
值得注意的是,他们即将发布一个截然不同的Vision库(现在是视觉大师的回购,尽管尚未发布到PyPI),这将是可能的。请注意,它是向后兼容的升级,因此会有一些(希望不会太多)转换工作。
该库返回普通的protobuf对象,可以使用以下命令将其序列化为JSON:
from google.protobuf.json_format import MessageToJson
serialized = MessageToJson(original)
你也可以使用像protobuf3-to-dict这样的东西
谷歌发布了一个图书馆
from google.protobuf.json_format import MessageToJson
webdetect = vision_client.web_detection(blob_source)
jsonObj = MessageToJson(webdetect)