-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_img_db.py
76 lines (63 loc) · 2.42 KB
/
test_img_db.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
import pprint
import cPickle as pickle
# import _pickle as pickle
import cv2
import time
import csv
import datetime
now = datetime.datetime.now()
# img = cv2.imread('./images/home/home1.jpg', 0)
print('loading test image...')
img = cv2.imread('./images/testBooks/butterfly/5.jpg', 0)
# print img
# Initiate STAR detector
orb = cv2.ORB_create()
# orb = cv2.xfeatures2d.SIFT_create()
# compute the descriptors with ORB
print('extracting descriptors from a test image')
kp, des = orb.detectAndCompute(img, None)
bf = cv2.BFMatcher()
# open a database file
pkl_file = open('./database/book_image_dabase_many.pkl', 'rb')
# get zipped object
print('reading descriptors from a file')
zipped_obj = pickle.load(pkl_file)
pkl_file.close()
print('comparing...')
percent = []
image = []
for image_descriptor, image_name in zipped_obj:
start_time = time.time()
matches = bf.knnMatch(des, image_descriptor, k=2)
good_points = []
for m, n in matches:
if m.distance < 0.6*n.distance:
good_points.append(m)
number_keypoints = 0
if len(image_descriptor) <= len(des):
number_keypoints = len(image_descriptor)
else:
number_keypoints = len(des)
print("Title: " + image_name)
percentage_similarity = float(len(good_points)) / number_keypoints * 100
print("--- %s seconds ---" % (time.time() - start_time))
print("Similarity: " + str((percentage_similarity)) + " %\n")
percent.append(str(int(percentage_similarity)))
image.append(image_name)
# pprint.pprint(data1)
zipped = sorted(zip(percent, image), key=lambda pair: pair[0], reverse= True)
# zipped = sorted(zipped, key = lambda x: x[0])
print('writing results to a file...')
with open('results_comparison.csv', 'a') as csvfile:
fieldnames = ['similarity(%)', 'image name', 'time and date']
# spamwriter = csv.writer(csvfile, delimiter=' ',
# quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer = csv.DictWriter(csvfile, delimiter='\t', fieldnames=fieldnames)
writer.writeheader()
for percentage, name_image in zipped:
writer.writerow(
{'similarity(%)': percentage + " %", 'image name': name_image, 'time and date': datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")})
# writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
# writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
print('finished writing to a file')
print('Done!!!')