【发布时间】:2018-09-04 15:06:35
【问题描述】:
我无法保存 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)
输出出现在屏幕上,但我不知道如何保存它。有人有什么建议吗?
【问题讨论】:
标签: google-api google-cloud-platform google-vision