82 lines
3.5 KiB
Python
82 lines
3.5 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
import LFUtilities
|
|
import BEBLIDParameters
|
|
import ImageRecognitionSettings as settings
|
|
from line_profiler_pycharm import profile
|
|
|
|
class BEBLIDRescorer:
|
|
|
|
def __init__(self):
|
|
#self.lf = LFUtilities.load(settings.DATASET_BEBLID)
|
|
#self.ids = np.loadtxt(settings.DATASET_IDS, dtype=str).tolist()
|
|
#self.bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
|
self.bf = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_BRUTEFORCE_HAMMING)
|
|
|
|
def rescore_by_id(self, query_id, resultset):
|
|
#query_idx = self.ids.index(query_id)
|
|
query = LFUtilities.load_img_lf(settings.DATASET_LF_FOLDER, query_id)
|
|
return self.rescore_by_img(query, resultset)
|
|
|
|
@profile
|
|
def rescore_by_img(self, query, resultset):
|
|
max_inliers = -1
|
|
res = []
|
|
counter = 0
|
|
if len(query[0]) > BEBLIDParameters.MIN_GOOD_MATCHES:
|
|
for data_id, _ in resultset:
|
|
try:
|
|
#data_el = LFUtilities.loadz_img_lf(settings.DATASET_LF_FOLDER, data_id)
|
|
data_el = LFUtilities.unpickle_img_lf(settings.DATASET_LF_FOLDER, data_id)
|
|
|
|
if len(data_el[1]) > BEBLIDParameters.MIN_GOOD_MATCHES:
|
|
nn_matches = self.bf.knnMatch(query[1], data_el[1], 2)
|
|
good = [m for m, n in nn_matches if m.distance < BEBLIDParameters.NN_MATCH_RATIO * n.distance]
|
|
|
|
if len(good) > BEBLIDParameters.MIN_GOOD_MATCHES:
|
|
src_pts = np.float32([query[0][m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
|
|
#dst_pts = np.float32([data_el[0][m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
|
|
dst_pts = data_el[0][[m.trainIdx for m in good]].reshape(-1, 1, 2)
|
|
|
|
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
|
|
matches_mask = mask.ravel().tolist()
|
|
# print(len(good))
|
|
inliers = np.count_nonzero(matches_mask)
|
|
# print(inliers)
|
|
if (inliers >= BEBLIDParameters.MIN_INLIERS and inliers > max_inliers):
|
|
max_inliers = inliers
|
|
res.append((data_id, round(inliers/len(good), 3)))
|
|
print(data_id)
|
|
print(f'candidate n. {counter}')
|
|
#to get just the first candidate
|
|
break
|
|
except Exception as e:
|
|
print('rescore error evaluating ' + data_id)
|
|
print(e)
|
|
pass
|
|
counter += 1
|
|
|
|
if res:
|
|
res.sort(key=lambda result: result[1], reverse=True)
|
|
return res
|
|
|
|
def add(self, kp, des, id):
|
|
# LFUtilities.save_img_lf(dest, filename, kp, des)
|
|
# LFUtilities.savez_img_lf(dest, filename, kp, des)
|
|
LFUtilities.pickle_img_lf(settings.DATASET_LF_FOLDER, id, kp, des)
|
|
|
|
def remove(self, idx):
|
|
self.descs = np.delete(self.descs, idx, axis=0)
|
|
|
|
def save(self, is_backup=False):
|
|
lf_save_file = settings.DATASET_LF
|
|
ids_file = settings.DATASET_IDS_LF
|
|
if lf_save_file != "None":
|
|
if is_backup:
|
|
lf_save_file += '.bak'
|
|
ids_file += '.bak'
|
|
|
|
LFUtilities.save(lf_save_file, self.lf)
|
|
np.savetxt(ids_file, self.ids, fmt='%s')
|