The error you encountered is
TypeError: Object of type 'RepeatedCompositeContainer'
is not JSON serializable
- The Vision library returns plain protobuf objects, which can be serialized to JSON using:
from google.protobuf.json_format
import MessageToJson
serialized = MessageToJson(original)
I have an api to call this function.,I have a function to detect labels from an anycodings_python image.,How can I have a function pointer template as a template parameter?,How to retrieve the value of the selected option from a dynamically created select in a table
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
I have an api to call this function.
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
The error you encountered is
TypeError: Object of type 'RepeatedCompositeContainer'
is not JSON serializable
- The Vision library returns plain protobuf objects, which can be serialized to JSON using:
from google.protobuf.json_format
import MessageToJson
serialized = MessageToJson(original)
The Python "TypeError: Object of type function is not JSON serializable" occurs when we try to serialize a function to JSON. To solve the error, make sure to call the function and serialize the object that the function returns.,We called the function, so we serialized the dict object rather than trying to serialize the function itself.,To solve the error, make sure to call the function and serialize its return value rather than the function itself.,You have to add parenthesis to call a function, e.g. get_employee(), otherwise we pass a reference to the get_employee function to the json.dumps method which is not what we want.
Copied! import json def get_employee(): return { 'name': 'Alice', 'age': 30 } #⛔️ TypeError: Object of type function is not JSON serializable json_str = json.dumps(get_employee) #👈️ forgot to call function
Copied!import json
def get_employee():
return {'name': 'Alice', 'age': 30}
# ✅ call function get_employee() and NOT get_employee
json_str = json.dumps(get_employee())
print(json_str) # 👉️ '{"name": "Alice", "age": 30}'
print(type(json_str)) # 👉️ <class 'str'>
TypeError: Object of type 'RepeatedCompositeContainer' is not JSON serializable,这个问题在这个 GitHub link 中得到了回答,这可能有助于解决您的问题。 您遇到的错误是
使用 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
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
这个问题在这个 GitHub link 中得到了回答,这可能有助于解决您的问题。
您遇到的错误是
TypeError: Object of type 'RepeatedCompositeContainer'
is not JSON serializable
from google.protobuf.json_format
import MessageToJson
serialized = MessageToJson(original)