-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfingerPrint2.py
371 lines (315 loc) · 12.9 KB
/
fingerPrint2.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
__author__ = 'jeremy'
#try adding local binary patterns , and global stuff - color, mean, variance and other moments for fingerprinting
#lalala
import sys
import numpy as np
import cv2
import re
import string
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import urllib
import os
import classify
import json
# for console debugging:
import pdb
#PATH_TO_FILE = sys.argv[1]
REMOTE_FILE = False
FILENAME = ""
BLUE = [255, 0, 0] # rectangle color
RED = [0, 0, 255] # PR BG
GREEN = [0, 255, 0] # PR FG
BLACK = [0, 0, 0] # sure BG
WHITE = [255, 255, 255] # sure FG
def crop_image_to_BB(img, BB_coordinates_string_or_array):
# pdb.set_trace()
if isinstance(BB_coordinates_string_or_array, basestring):
BB_array = json.loads(BB_coordinates_string_or_array)
for i in range (0, len(BB_array)):
BB_array[i] = int(BB_array[i])
else:
BB_array = BB_coordinates_string_or_array
x=BB_array[0]
y=BB_array[1]
w=BB_array[2]
h=BB_array[3]
hh, ww, d = img.shape
if (x+w <= ww) and (y+h <= hh):
rectok=True
r=[x,y,w,h]
#allRects.append(r)
mask = np.zeros(img.shape[:2], np.uint8)
roi= np.zeros((r[3],r[2],3),np.uint8)
mask[r[0]:r[2], r[1]:r[3]] = 255
for xx in range(r[2]):
for yy in range(r[3]):
roi[yy,xx,:]=img[yy+r[1],xx+r[0],:]
else:
rectok=False
print('badrect for file:'+ FILENAME +' imsize:'+str(ww)+','+str(hh)+' vs.:'+str(x+w)+','+str(y+h))
return roi #this is what fp(img) is expecting
def fp(pathToImageFile_Or_cv2ImageArray, bb=None):
REMOTE_FILE = False
#if given a string, check if URL and download if necessary
if isinstance(pathToImageFile_Or_cv2ImageArray, str):
if "://" in pathToImageFile_Or_cv2ImageArray:
FILENAME = "temp.jpg"#pathToImageFile_Or_cv2ImageArray.split('/')[-1].split('#')[0].split('?')[0]
res = urllib.urlretrieve (pathToImageFile_Or_cv2ImageArray, FILENAME)
pathToImageFile_Or_cv2ImageArray = FILENAME
REMOTE_FILE = True
img = cv2.imread(pathToImageFile_Or_cv2ImageArray)
os.remove(pathToImageFile_Or_cv2ImageArray)
else:
img = pathToImageFile_Or_cv2ImageArray
img = crop_image_to_BB(img, bb)
s=5 #crop out the outer 1/s of the image for color/texture-based features
h=img.shape[1]
w=img.shape[0]
# old dumb crop
# r=[h/s,w/s,h-2*h/s,w-2*w/s]
#print('r='+str(r))
# roi= np.zeros((r[3],r[2],3),np.uint8)
# for xx in range(r[2]):
# for yy in range(r[3]):
# roi[yy ,xx,:]=img[yy+r[1],xx+r[0],:]
#new awesome efficient crop
r=[w/s,h/s,w-2*w/s,h-2*h/s] #x,y,w,h
roi = img[r[1]:r[1]+r[3], r[0]:r[0]+r[2]]
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
hueValue=hsv[:,:,0]
satValue=hsv[:,:,1]
intValue=hsv[:,:,2]
# print('hueV'+str(hueValue.shape))
# plt.subplot(2,3,4)
# plt.title('hue')
# plt.imshow(hueValue,cmap=plt.cm.jet) #cm.Greys_r)
# plt.subplot(2,3,5)
# plt.title('sat')
# plt.imshow(satValue,cmap = cm.Greys_r)
# plt.subplot(2,3,6)
# plt.title('int')
# plt.imshow(intValue,cmap = cm.Greys_r)
#OpenCV uses H: 0 - 180, S: 0 - 255, V: 0 - 255
#histograms
bins=25
hueXs = np.linspace(0,180,bins)
nPixels=roi.shape[0]*roi.shape[1]
histHue=cv2.calcHist([hsv],[0],None,[bins],[0,180])
histHue=[item for sublist in histHue for item in sublist] #flatten nested
histHue=np.divide(histHue,nPixels)
histSat=cv2.calcHist([hsv],[1],None,[bins],[0,255])
histSat=[item for sublist in histSat for item in sublist]
histSat=np.divide(histSat,nPixels)
histInt=cv2.calcHist([hsv],[2],None,[bins],[0,255])
histInt=[item for sublist in histInt for item in sublist] #flatten nested list
histInt=np.divide(histInt,nPixels)
#Uniformity t(5)=sum(p.^ 2);
hueUniformity=np.dot(histHue,histHue)
satUniformity=np.dot(histSat,histSat)
intUniformity=np.dot(histInt,histInt)
#Entropy t(6)=-sum(p. *(log2(p+ eps)));
eps=1e-15
lHue=np.log2(histHue+eps)
hueEntropy=np.dot(histHue,lHue)
lSat=np.log2(histSat+eps)
satEntropy=np.dot(histSat,lSat)
lInt=np.log2(histInt+eps)
intEntropy=np.dot(histInt,lInt)
resultVector=[hueUniformity,satUniformity,intUniformity,hueEntropy,satEntropy,intEntropy]
resultVector=np.concatenate((resultVector,histHue, histSat), axis=0)
#print('result')
return(resultVector)
def rmse(predictions, targets):
return np.sqrt((((predictions - targets)) ** 2).mean())
def myRange(start,stop,inc):
r=start
while r<stop:
yield r
r+=inc
def gaussian1(x,x0,c,sigma):
return c*np.exp(-(x-x0)**2/(2*sigma**2))
#def classify_and_fingerprint(path_to_image_file=None):
def classify_and_fingerprint(path_to_image_file):
#pdb.set_trace()
print("in fingerPrint2:classify_and_fingerprint:path_to_image_file: " + str(path_to_image_file))
REMOTE_FILE = False
shirt_found = False
fingerprint = ""
classification_dict = {}
#read arg from command line if none given to function
if path_to_image_file is not None:
PATH_TO_FILE = path_to_image_file
#else:
#wtf is supposed to happen here - who is calling this from command line??
#we want to be able to read URL as well as local file path
if "://" in path_to_image_file:
FILENAME = path_to_image_file.split('/')[-1].split('#')[0].split('?')[0]
res = urllib.urlretrieve (PATH_TO_FILE, FILENAME)
PATH_TO_FILE = FILENAME
REMOTE_FILE = True
#pdb.set_trace()
#main prog starts here
img = cv2.imread(PATH_TO_FILE)
roi = []
classification_dict = classify.classify_image(img)
BB_coordinates = classification_dict["/home/www-data/web2py/applications/fingerPrint/modules/shirtClassifier.xml"]
if len(BB_coordinates) > 0:
if BB_coordinates[0] is not None:
shirt_found = True
roi = crop_image_to_BB(img, BB_coordinates[0])
else:
roi=img
BB_coordinates = [[0,0,0,0]]
print("in fingerPrint2.py: len(BB_coordinates)>0 but BB[0] is None -- REALLY WEIRD:")
print(BB_coordinates)
else:
# roi = None #if no BB was formed , don't return an answer!!!!
print("in fingerPrint2.py:bad roi (could not crop?)-using entire img - len(BB)!>0")
roi=img
BB_coordinates = [[0,0,0,0]]
if roi is not None:
fingerprint = fp(roi)
else:
print("in fingerPrint2.py:bad roi (could not crop?) - using entire img (again)")
fingerprint_length=56
fingerprint=[0 for x in range(fingerprint_length)]
fingerprint[0]=-1
print("in fingerPrint2.py:fp="+str(fingerprint))
if REMOTE_FILE:
os.remove(PATH_TO_FILE)
# right now, we're only doing shirts, so it's binary
# 0 - nothing found, 1 - at least one shirt found.
# even within this simplified case, still need to figure out
# how to deal with multiple shirts in an image
classification_list = []
if shirt_found:
classification_list.append(1)
else:
classification_list.append(0)
print('in fingerPrint2.py:classify_and_fingerprint:fingerprint='+str(fingerprint))
print('in fingerPrint2.py:classify_and_fingerprint:BB='+str(BB_coordinates))
return classification_list, fingerprint, BB_coordinates
#takes classifier file path
def classify_and_fingerprint_with_classifier(path_to_image_file, classifier_xml):
#pdb.set_trace()
print("in fingerPrint2:classify_and_fingerprint:path_to_image_file: " + str(path_to_image_file))
REMOTE_FILE = False
item_found = False
fingerprint = ""
classification_dict = {}
#read arg from command line if none given to function
if path_to_image_file is not None:
PATH_TO_FILE = path_to_image_file
#else:
#wtf is supposed to happen here - who is calling this from command line??
#we want to be able to read URL as well as local file path
if "://" in path_to_image_file:
FILENAME = path_to_image_file.split('/')[-1].split('#')[0].split('?')[0]
res = urllib.urlretrieve (PATH_TO_FILE, FILENAME)
PATH_TO_FILE = FILENAME
REMOTE_FILE = True
#pdb.set_trace()
#main prog starts here
img = cv2.imread(PATH_TO_FILE)
roi = []
classification_dict = classify.classify_image_with_classifier(img, classifier_xml)
BB_coordinates = classification_dict[classifier_xml]
if len(BB_coordinates) > 0:
if BB_coordinates[0] is not None:
item_found = True
roi = crop_image_to_BB(img, BB_coordinates[0])
else:
roi=img
BB_coordinates = [[0,0,0,0]]
print("in fingerPrint2.py: len(BB_coordinates)>0 but BB[0] is None -- REALLY WEIRD:")
print(BB_coordinates)
else:
# roi = None #if no BB was formed , don't return an answer!!!!
print("in fingerPrint2.py:bad roi (could not crop?)-using entire img - len(BB)!>0")
roi=img
BB_coordinates = [[0,0,0,0]]
if roi is not None:
fingerprint = fp(roi)
else:
print("in fingerPrint2.py:bad roi (could not crop?) - using entire img (again)")
fingerprint_length=56
fingerprint=[0 for x in range(fingerprint_length)]
fingerprint[0]=-1
print("in fingerPrint2.py:fp="+str(fingerprint))
if REMOTE_FILE:
os.remove(PATH_TO_FILE)
# right now, we're only doing shirts, so it's binary
# 0 - nothing found, 1 - at least one shirt found.
# even within this simplified case, still need to figure out
# how to deal with multiple shirts in an image
classification_list = []
if item_found:
classification_list.append(1)
else:
classification_list.append(0)
print('in fingerPrint2.py:classify_and_fingerprint:fingerprint='+str(fingerprint))
print('in fingerPrint2.py:classify_and_fingerprint:BB='+str(BB_coordinates))
return classification_list, fingerprint, BB_coordinates
def classify_and_fingerprint_dresses(path_to_image_file):
#pdb.set_trace()
print("in fingerPrint2:classify_and_fingerprint_dresses:path_to_image_file: " + str(path_to_image_file))
REMOTE_FILE = False
item_found = False
fingerprint = ""
classification_dict = {}
#read arg from command line if none given to function
if path_to_image_file is not None:
PATH_TO_FILE = path_to_image_file
#else:
#wtf is supposed to happen here - who is calling this from command line??
#we want to be able to read URL as well as local file path
if "://" in path_to_image_file:
FILENAME = path_to_image_file.split('/')[-1].split('#')[0].split('?')[0]
res = urllib.urlretrieve (PATH_TO_FILE, FILENAME)
PATH_TO_FILE = FILENAME
REMOTE_FILE = True
#pdb.set_trace()
#main prog starts here
img = cv2.imread(PATH_TO_FILE)
roi = []
classification_dict = classify.classify_image(img)
BB_coordinates = classification_dict["/home/www-data/web2py/applications/fingerPrint/modules/dressClassifier001.xml"]
if len(BB_coordinates) > 0:
if BB_coordinates[0] is not None:
item_found = True
roi = crop_image_to_BB(img, BB_coordinates[0])
else:
roi=img
BB_coordinates = [[0,0,0,0]]
print("in fingerPrint2.py: len(BB_coordinates)>0 but BB[0] is None -- REALLY WEIRD:")
print(BB_coordinates)
else:
# roi = None #if no BB was formed , don't return an answer!!!!
print("in fingerPrint2.py:bad roi (could not crop?)-using entire img - len(BB)!>0")
roi=img
BB_coordinates = [[0,0,0,0]]
if roi is not None:
fingerprint = fp(roi)
else:
print("in fingerPrint2.py:bad roi (could not crop?) - using entire img (again)")
fingerprint_length=56
fingerprint=[0 for x in range(fingerprint_length)]
fingerprint[0]=-1
print("in fingerPrint2.py:fp="+str(fingerprint))
if REMOTE_FILE:
os.remove(PATH_TO_FILE)
# right now, we're only doing shirts, so it's binary
# 0 - nothing found, 1 - at least one shirt found.
# even within this simplified case, still need to figure out
# how to deal with multiple shirts in an image
classification_list = []
if item_found:
classification_list.append(2)
else:
classification_list.append(0)
print('in fingerPrint2.py:classify_and_fingerprint_dresses:fingerprint='+str(fingerprint))
print('in fingerPrint2.py:classify_and_fingerprint_dresses:BB='+str(BB_coordinates))
return classification_list, fingerprint, BB_coordinates