/
bof_search.py
72 lines (49 loc) · 1.7 KB
/
bof_search.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
# coding=utf-8
from __future__ import print_function
import glob
import pprint
import sys
import os
import cv2
import numpy as np
import pickle
IMAGES_DIR = '/home/xtal/Code/flatsearch/var/images/'
TEMP_DIR = '/home/xtal/'
NUM_MATCHES = 10
FLANN_INDEX_KDTREE = 1
flann_params = dict(algorithm = FLANN_INDEX_KDTREE,
trees = 4)
def describe(path, dictionary):
matcher = cv2.FlannBasedMatcher(flann_params, {}) # блять
detector = cv2.SIFT()
extractor = cv2.DescriptorExtractor_create('SIFT')
bowDE = cv2.BOWImgDescriptorExtractor(extractor, matcher)
bowDE.setVocabulary(dictionary)
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kp = detector.detect(gray, None)
des = bowDE.compute(gray, kp, None)
return des
def clear_directory(result_path):
for path in os.listdir(result_path):
os.unlink(os.path.join(result_path, path))
def main():
dict_path = TEMP_DIR + sys.argv[1]
dictionary = pickle.load(open(dict_path, 'rb'))
index_path = TEMP_DIR + sys.argv[2]
index = pickle.load(open(index_path, 'rb'))
image_path = IMAGES_DIR + sys.argv[3]
image_des = describe(image_path, dictionary)
result_path = TEMP_DIR + sys.argv[4]
clear_directory(result_path)
result = []
for path, des in index.items():
likelyhood = cv2.compareHist(image_des, des, cv2.cv.CV_COMP_CHISQR)
result.append((likelyhood, path))
best_match = sorted(result, key=lambda x: x[0])[:NUM_MATCHES]
for rank, i in enumerate(best_match):
likelyhood, path = i
os.symlink(path, result_path + '/{}.jpeg'.format(rank))
pprint.pprint(best_match)
if __name__ == '__main__':
main()