I would like to cache KeyPoint
in a JSON file and then retrieve them later for use in a FlannBasedMatcher
. Is there a way to convert the KeyPoint
to something like an array of strings or floats that could be stored and then retreived from a JSON file? I think this should be ok for the descriptors since they just look like an array of ints.
COMPUTING KEYPOINTs
kp2, des2 = brisk.detectAndCompute(img2, None)
MATCHER
matches = flann.knnMatch(des1,des2,k=2)
You can save your KeyPoint to a JSON file directly in string type:
import json
def save_2_jason(arr):
data = {}
cnt = 0
for i in arr:
data['KeyPoint_%d'%cnt] = []
data['KeyPoint_%d'%cnt].append({'x': i.pt[0]})
data['KeyPoint_%d'%cnt].append({'y': i.pt[1]})
data['KeyPoint_%d'%cnt].append({'size': i.size})
cnt+=1
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Save to data.txt with json format:
(kpt, desc) = brisk.detectAndCompute(img, None)
save_2_jason(kpt)
Converting back to KeyPoint from a JSON file need change it to cv2.KeyPoint class:
import json
def read_from_jason():
result = []
with open('data.txt') as json_file:
data = json.load(json_file)
cnt = 0
while(data.__contains__('KeyPoint_%d'%cnt)):
pt = cv2.KeyPoint(x=data['KeyPoint_%d'%cnt][0]['x'],y=data['KeyPoint_%d'%cnt][1]['y'], _size=data['KeyPoint_%d'%cnt][2]['size'])
result.append(pt)
cnt+=1
return result
Read from data.txt you save:
kpt_read_from_jason = read_from_jason()