-
Notifications
You must be signed in to change notification settings - Fork 0
/
fingerprint.py
96 lines (59 loc) · 2.13 KB
/
fingerprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from genericpath import isfile
import cv2 as cv
from cv2 import NORM_L2
from cv2 import NORM_HAMMING
import numpy as np
from os.path import join
from os import listdir
from sklearn.metrics import classification_report as report
print("Preparing data...")
filename_list = [f for f in listdir('DB') if isfile(join('DB',f))]
features_base={}
features_suspects={}
for filename in filename_list:
image = cv.imread('DB/'+filename)
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
sift=cv.SIFT_create()
sift_des=sift.detectAndCompute(image,None)
orb = cv.ORB_create()
orb_des = orb.detectAndCompute(image,None)
feat_dict = {'sift': sift_des, 'orb': orb_des}
img_id = filename.split('.')[0]
if img_id.split('_')[-1] == '1':
features_base[img_id.split('_')[0]] = feat_dict
else:
features_suspects[img_id] = feat_dict
#BFmatcher
sift_matcher = cv.BFMatcher_create(cv.NORM_L2)
orb_matcher = cv.BFMatcher_create(cv.NORM_HAMMING)
sift_test=[]
sift_pred=[]
orb_test=[]
orb_pred=[]
i=0
for descriptor in features_suspects:
i+=1
print("Analysing image: "+str(i)+"/"+str(len(features_suspects)))
match_len = {}
for base in features_base:
sift_match=sift_matcher.knnMatch(features_suspects[descriptor]['sift'][1],features_base[base]['sift'][1],k=2)
good_match=[]
for m,n in sift_match:
if m.distance < 0.75*n.distance:
good_match.append(m)
match_len[base]=len(good_match)
sift_pred.append(max(match_len, key=match_len.get))
sift_test.append(descriptor.split('_')[0])
#Dooing the same but with ORB
match_len = {}
for base in features_base:
orb_match=orb_matcher.knnMatch(features_suspects[descriptor]['orb'][1],features_base[base]['orb'][1],k=2)
good_match=[]
for m,n in orb_match:
if m.distance < 0.75*n.distance:
good_match.append(m)
match_len[base]=len(good_match)
orb_pred.append(max(match_len, key=match_len.get))
orb_test.append(descriptor.split('_')[0])
print(report(sift_test,sift_pred))
print(report(orb_test,orb_pred))