When I tried to write KeyPoint to a file using the Pickle module, I got an error. It is a memo of the countermeasure at that time. If you search for it, it will come out quite quickly, so there may not be much demand, but for the time being.
http://stackoverflow.com/questions/26501193/opencv-python-find-a-code-for-writing-keypoins-to-a-file Almost the same as the answer here
out_kp_and_des.py
import cv2
import pickle
img = cv2.imread('XXXX.jpg')
detector = cv2.AKAZE_create()
#keypoints and descriptors
kp, des = detector.detectAndCompute(img, None)
with open('XXXX.pickle', mode='wb') as f:
pickle.dump((kp, des), f)
If you try to Pickle KeyPoint as it is, you will get an error like `` `PicklingError: Can't pickle <class'cv2.KeyPoint'>: it's not the same object as cv2.KeyPoint```.
out_kp_and_des.py
import cv2
import pickle
img = cv2.imread('XXXX.jpg')
detector = cv2.AKAZE_create()
kp, des = detector.detectAndCompute(img, None)
index = []
for p in kp:
temp = (p.pt, p.size, p.angle, p.response, p.octave, p.class_id)
index.append(temp)
with open('XXXX.pickle', mode='wb') as f:
pickle.dump((index, des), f)
Take out the KeyPoint attribute and put it in the list to make it feel like Pickle. The file reading looks like the following.
load_kp_and_des.py
import cv2
import pickle
with open('XXXX.pickle', mode='rb') as f:
index, des = pickle.load(f)
kp = []
for p in index:
temp = cv2.KeyPoint(x=p[0][0], y=p[0][1], _size=p[1], _angle=p[2],
_response=p[3], _octave=p[4], _class_id=p[5])
kp.append(temp)
Recommended Posts