Python 类型为'的对象;关键点';JSON不是可序列化的opencv吗
我正在尝试发送一个带有图像关键点和描述符的服务器请求作为json对象。。。这是我的密码Python 类型为'的对象;关键点';JSON不是可序列化的opencv吗,python,json,opencv,sift,keypoint,Python,Json,Opencv,Sift,Keypoint,我正在尝试发送一个带有图像关键点和描述符的服务器请求作为json对象。。。这是我的密码 import cv2 import requests import json imgDetail = {'keypoints': '', 'descriptor': ''} sift = cv2.xfeatures2d.SIFT_create() img = cv2.imread('images/query.jpg', 0) kp, des = sift.detectAndCompute(img,
import cv2
import requests
import json
imgDetail = {'keypoints': '', 'descriptor': ''}
sift = cv2.xfeatures2d.SIFT_create()
img = cv2.imread('images/query.jpg', 0)
kp, des = sift.detectAndCompute(img, None)
des = des.tolist()
imgDetail['keypoints'] = kp
imgDetail['descriptor'] = des
jsonDump = json.dumps(imgDetail)
resp = requests.post("http://localhost:5000", json=jsonDump, headers={'content-type': 'application/json'})
但它给了我以下的错误
Traceback (most recent call last):
File "E:/python projects/mawa/image.py", line 23, in <module>
jsonDump = json.dumps(imgDetail)
File "E:\Programs\Python\Python36\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "E:\Programs\Python\Python36\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "E:\Programs\Python\Python36\lib\json\encoder.py", line 257, in iterencode return _iterencode(o, 0)
File "E:\Programs\Python\Python36\lib\json\encoder.py", line 180, in default o.__class__.__name__)
TypeError: Object of type 'KeyPoint' is not JSON serializable
回溯(最近一次呼叫最后一次):
文件“E:/python projects/mawa/image.py”,第23行,在
jsonDump=json.dumps(imgDetail)
文件“E:\Programs\Python\Python36\lib\json\\uuuu init\uuuu.py”,第231行,转储
返回默认编码器编码(obj)
文件“E:\Programs\Python\Python36\lib\json\encoder.py”,第199行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
文件“E:\Programs\Python\Python36\lib\json\encoder.py”,第257行,在iterencode return\u iterencode(o,0)中
文件“E:\Programs\Python\Python36\lib\json\encoder.py”,第180行,默认为o
TypeError:类型为“KeyPoint”的对象不可JSON序列化
有人能给出解决方案吗?如果您看到您的kp变量,它是一个关键点实例列表。 i、 e:kp看起来像
[,,,…]
当您尝试转储imgDetail时,keypoints(即:kp)出现错误,因为KeyPoint实例无法序列化
您需要循环kp列表并将实例更改为dict
imgDetail['keypoints'] = [{'angle': k.angle, 'response': k.response} for k in kp]
KeyPoint类没有tolist()或\uuuuuuu dict\uuuu()方法。因此,您可能需要创建自己的dict并传递到json.dumps()。谢谢您的回答。还有一个问题,我以后会在代码中使用这个关键点来绘制这样的匹配。。cv2.自imgDtails['keypoints']以来的drawMatches(img、imgDetails['keypoints']、matchObj['keypoints']、matchObj['keypoints']、matchObj['goodMatch']、None、**draw_参数)不是cv2.KeyPoints对象我将如何绘制匹配如果您想进一步使用它,您可能需要使用pickle和unpickle对象。检查此[链接]()。