-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseImageSearchSIFT.py
151 lines (135 loc) · 4.32 KB
/
ReverseImageSearchSIFT.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Alexandre Matov, 2017
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pickle
import os
import glob
import sys
import getHistogram
import sklearn.cluster as skcluster
from sklearn.decomposition.pca import PCA
from elasticsearch import Elasticsearch
from time import time
import itertools
import glob
import time
import pprint
from math import ceil
from tqdm import tqdm
from numpy import average, median
import multiprocessing as mp
index_name = 'idx-sift'
# To specify num clusters, use this command:
# NUM_CLUSTERS=200 python ./test_sift_es.py
num_clusters = int(os.environ.get('NUM_CLUSTERS', 5000))
model_path = './out/model.sift-%s.pickle' % num_clusters
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'-l', '--limit',
help = 'how many images to analyze (-1 means no limit)',
default = -1,
type = int,
)
parser.add_argument(
'-p', '--path',
help = 'path to images directory',
default = './flickr-images',
type = str,
)
cpu_count = mp.cpu_count()
parser.add_argument(
'--cpus',
help = 'number of cores to use (defaults to using all cores: %d)' % cpu_count,
default = cpu_count,
type = int,
)
args = parser.parse_args()
if args.limit < 0:
args.limit = None
start = time.time()
data = []
try:
analyze(
basepath = args.path,
limit = args.limit,
cpus = args.cpus,
progress_fn = create_progress_fn(),
results = data,
)
except KeyboardInterrupt:
# If analysis interrupted by Ctrl-C continue with files analysed so far
pass
print('%s images analyzed in ~%.0fs' % (len(data), time.time() - start))
if len(data) == 0:
exit()
print('')
def create_progress_fn():
memo = {
'pbar': None,
'i': 0,
}
def index_source_image(es, file_id, terms):
# by default we connect to localhost:9200
es.indices.create(index=index_name, ignore=400)
es.index(index=index_name, doc_type="artwork", id=file_id, body={"tags": list(set(terms))})
def build_kmeans_model(descriptors, num_clusters=10000):
print('starting clustering (%s clusters)' % num_clusters)
kmeans = skcluster.KMeans(num_clusters)
start = time()
idxs = kmeans.fit_predict(descriptors)
print('done in %0.3fs' % (time() - start))
return idxs, kmeans
path = './flickr-images/*.jpg'
files=glob.glob(path)
sift = cv2.xfeatures2d.SIFT_create()
def buildDictionary():
dictionarySize = num_clusters
print('building dictionary of %s visual words' % dictionarySize)
#BOW = cv2.BOWKMeansTrainer(dictionarySize)
descriptors=[]
for filename in itertools.islice(files_iter, 0, limit):
filenames.append(filename)
for filename in files:
image = cv2.imread(filename,0)
print('processing/features %s...' % filename,)
#plt.imshow(image), plt.show()
#gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, des= sift.detectAndCompute(image, None)
descriptors.extend(des)
#BOW.add(des)
indexes, model = build_kmeans_model(descriptors, dictionarySize)
print(len(indexes))
print(len(descriptors))
print(model.cluster_centers_)
with open(model_path, 'wb+') as f:
pickle.dump(model, f)
return model
#dictionary created
#dictionary = BOW.cluster()
try:
with open(model_path, 'rb') as f:
model = pickle.load( f)
except FileNotFoundError:
model = buildDictionary()
es = Elasticsearch()
# REPLACE FILENAME with a larger library to index over 25k
#path = './1000testImages/*.jpg'
path = './images/moreImages/*.jpg'
files=glob.glob(path)
for filename in files:
image = cv2.imread(filename,0)
print('processing/indexing %s...' % filename,)
#plt.imshow(image), plt.show()
#gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kp, descs= sift.detectAndCompute(image, None)
words = [int(i) for i in model.predict(descs)]
index_source_image(es,filename,words)
print(set(words))
#img2=cv2.drawKeypoints(image,kp,image,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#plt.imshow(img2)
#plt.show()
print('len kp %s...' % len(kp),)