gpt4 book ai didi

python - 'RepeatedCompositeContainer' 类型的对象不是 JSON 可序列化的

转载 作者:行者123 更新时间:2023-12-04 12:01:11 28 4
gpt4 key购买 nike

使用 Google 客户端库与视觉库交互。

我有一个从图像中检测标签的功能。

GoogleVision.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

我有一个 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

但是,它不会返回结果和以下错误

TypeError: Object of type 'RepeatedCompositeContainer' is not JSON serializable

最佳答案

这个问题在这个 GitHub link 中得到了回答,这可能有助于解决您的问题。
您遇到的错误是

TypeError: Object of type 'RepeatedCompositeContainer' is not JSON serializable
以下是GitHub线程中提供的解决方案
  • Vision 库返回普通的 protobuf 对象,可以使用以下命令将其序列化为 JSON:
  • from google.protobuf.json_format import MessageToJson
    serialized = MessageToJson(original)
  • 你也可以使用类似 protobuf3-to-dict 的东西去字典。 .
  • 关于python - 'RepeatedCompositeContainer' 类型的对象不是 JSON 可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860448/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com