【发布时间】:2019-02-20 09:20:42
【问题描述】:
我目前能够使用以下代码将 OpenCV 图像帧发送到我的 Flask 服务器
def sendtoserver(frame):
imencoded = cv2.imencode(".jpg", frame)[1]
headers = {"Content-type": "text/plain"}
try:
conn.request("POST", "/", imencoded.tostring(), headers)
response = conn.getresponse()
except conn.timeout as e:
print("timeout")
return response
但我想发送一个 unique_id 以及我尝试使用 JSON 组合框架和 id 的框架,但出现以下错误 TypeError: Object of type 'bytes' is not JSON serializable 有人知道如何将一些额外的数据与框架一起发送到服务器.
更新:
json格式代码
def sendtoserver(frame):
imencoded = cv2.imencode(".jpg", frame)[1]
data = {"uid" : "23", "frame" : imencoded.tostring()}
headers = {"Content-type": "application/json"}
try:
conn.request("POST", "/", json.dumps(data), headers)
response = conn.getresponse()
except conn.timeout as e:
print("timeout")
return response
【问题讨论】:
-
您能粘贴一下您是如何尝试将您的框架和唯一 ID 结合起来的吗?此外,如果您将 unique_id 显式转换为字节,您会发现此错误。将其留给 JSON 来处理事物的编码解码,如下所述:stackoverflow.com/questions/44682018/…
-
我在@RahulRaghunath 问题中添加了我的 json 代码