Eliminare 'src/BEBLIDRescorerDB.py'
This commit is contained in:
parent
dffb1c25f3
commit
1a2f821158
|
|
@ -1,81 +0,0 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import LFUtilities
|
||||
import BEBLIDParameters
|
||||
import ImageRecognitionSettings as settings
|
||||
from LFDB import LFDB
|
||||
|
||||
|
||||
class BEBLIDRescorerDB:
|
||||
|
||||
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)
|
||||
self.lf_db = LFDB(settings.DB_LF)
|
||||
|
||||
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)
|
||||
|
||||
def rescore_by_img(self, query, resultset):
|
||||
max_inliers = -1
|
||||
res = []
|
||||
counter = 0
|
||||
if len(query[0]) > 0:
|
||||
for data_id, _ in resultset:
|
||||
try:
|
||||
blob = self.lf_db.get(data_id)
|
||||
serialized_obj = LFUtilities.deserialize_object(blob)
|
||||
data_el = LFUtilities.unpickle_keypoints(serialized_obj)
|
||||
|
||||
|
||||
if len(data_el[1]) > 0:
|
||||
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)
|
||||
|
||||
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 3.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, lf):
|
||||
self.lf.append(lf)
|
||||
|
||||
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')
|
||||
Loading…
Reference in New Issue