Kay*_*Kay 5 python google-cloud-platform
使用 Google 客户端库与视觉库交互。
我有一个从图像中检测标签的功能。
谷歌视觉.py
import os
from google.cloud import vision
from google.cloud.vision import types
from google.protobuf.json_format import MessageToJson
class GoogleVision():
def detectLabels(self, uri):
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.label_detection(image=image)
labels = response.label_annotations
return labels
Run Code Online (Sandbox Code Playgroud)
我有一个 api 来调用这个函数。
from flask_restful import Resource
from flask import request
from flask import json
from util.GoogleVision import GoogleVision
import os
class Vision(Resource):
def get(self):
return {"message": "API Working"}
def post(self):
googleVision = GoogleVision()
req = request.get_json()
url = req['url']
result = googleVision.detectLabels(url)
return result
Run Code Online (Sandbox Code Playgroud)
但是,它不会返回结果和以下错误
类型错误:“RepeatedCompositeContainer”类型的对象不是 JSON 可序列化的
此问题已在此GitHub 链接中得到解答,这可能有助于解决您的问题。
您遇到的错误是
TypeError: Object of type 'RepeatedCompositeContainer' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
以下是 GitHub 线程中提供的解决方案
from google.protobuf.json_format import MessageToJson
serialized = MessageToJson(original)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6131 次 |
最近记录: |