-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFINAL.py
476 lines (420 loc) · 20.3 KB
/
FINAL.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
from keras.preprocessing import image
import numpy as np
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
import cv2
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
from utils import label_map_util
from utils import visualization_utils as vis_util
import pytesseract
import smtplib
config = tf.ConfigProto(
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1)
# device_count = {'GPU': 1}
)
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
set_session(session)
img_width, img_height = 150, 150
model = tf.keras.models.load_model('./FINAL_FILES/tf_model.h5')
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
############## INPUT INPUT INPUT ########################################
#INPUT THE IMAGE YOU WANT TO TEST
test_img_Location = './FINAL_FILES/test_images/xxxxxx.jpg'
#INPUT THE MAIL ADDRESS AS REQUIRED
login_mail='example@gmail.com'
login_password='xxxxxxxxx'
to_mail='exapmle@gmail.com'
##INPUT THE LOCATION OF PyTESSERACT.EXE FILE LOCATION
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract\\tesseract.exe'
##########################################################################
image_np = cv2.imread(test_img_Location)
y_len = np.size(image_np, 0)
x_len = np.size(image_np, 1)
# predicting images
img = image.load_img(test_img_Location, target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images)
#print(classes)
if ((classes[0][0]) > 0.5 ):
#print("not empty")
out = 1
else:
#print("empty")
out = 0
lower = np.array([246,246,246])
upper = np.array([255,255,255])
if (y_len == 4032):
x1=755
y1=0
x2=3024
y2=290
crop_img = image_np[y1:y2, x1:x2]
dim = (1500,300)
resized = cv2.resize(crop_img, dim, interpolation = cv2.INTER_AREA)#x1 y1 top left vertex
mask = cv2.inRange(resized, lower, upper)
mask = cv2.resize(mask, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
#cv2.imshow("cropped", mask)
#cv2.imwrite("filename.jpg", mask)
text_time = pytesseract.image_to_string(mask)
#print(text)
x1l=755
y1l=290
x2l=3024
y2l=1000
crop_imgl = image_np[y1l:y2l, x1l:x2l]
dim = (700,300)
resizedl = cv2.resize(crop_imgl, dim, interpolation = cv2.INTER_AREA)#x1 y1 top left vertex
maskl = cv2.inRange(resizedl, lower, upper)
maskl = cv2.resize(maskl, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
maskl = cv2.medianBlur(maskl, 3)
#cv2.imshow("cropped", maskl)
#cv2.imwrite("filenamel.jpg", maskl)
textl_loc = pytesseract.image_to_string(maskl)
else:
x11=1700
y11=0
x22=4200
y22=290
dim = (1500,300)
crop_img1 = image_np[y11:y22, x11:x22]
resized = cv2.resize(crop_img1, dim, interpolation = cv2.INTER_AREA)#x1 y1 top left vertex
mask = cv2.inRange(resized, lower, upper)
mask = cv2.resize(mask, None, fx=5, fy=5, interpolation=cv2.INTER_CUBIC)
#cv2.imshow("cropped", mask)
#cv2.imwrite("filename.jpg", mask)
text_time = pytesseract.image_to_string(mask)
#print(text)
x11l=1700
y11l=290
x22l=4200
y22l=1000
diml = (700,300)
crop_img1l = image_np[y11l:y22l, x11l:x22l]
resizedl = cv2.resize(crop_img1l, diml, interpolation = cv2.INTER_AREA)#x1 y1 top left vertex
maskl = cv2.inRange(resizedl, lower, upper)
maskl = cv2.resize(maskl, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
maskl = cv2.medianBlur(maskl, 3)
#cv2.imshow("cropped", maskl)
#cv2.imwrite("filename.jpg", maskl)
textl_loc = pytesseract.image_to_string(maskl)
#print(textl)
if (out == 0):
print("Shelf is EMPTY")
print("Finding name and price of the item from the shelf...")
# title of our window
#title = "out"
# # Model preparation
PATH_TO_FROZEN_GRAPH = './FINAL_FILES/frozen_inference_graph_item.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = './FINAL_FILES/labelmap_item.pbtxt'
NUM_CLASSES = 1
# ## Load a (frozen) Tensorflow model into memory.
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# # Detection
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
#while True:
# Get raw pixels from the screen, save it to a Numpy array
image_np = cv2.imread(test_img_Location)
y = np.size(image_np, 0)
x = np.size(image_np, 1)
#cv2.imshow('image_np',image_np)
#image_np = cv2.resize(image_np,(800,640))
# To get real color we do this:
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Visualization of the results of a detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=3)
total_final=cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Show image with detection
#cv2.imshow(title,total_final )
ymin = int((boxes[0][0][0]*y))
xmin = int((boxes[0][0][1]*x))
ymax = int((boxes[0][0][2]*y))
xmax = int((boxes[0][0][3]*x))
#print(scores[0])
Result = np.array(total_final[ymin:ymax,xmin:xmax])
#in_range = cv2.inRange(total_final,(0,127,0),(0,255,127))
#cv2.imshow('in_range',total_final[ymin:ymax,xmin:xmax])
mask = cv2.resize(total_final[ymin:ymax,xmin:xmax], None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
mask = cv2.medianBlur(mask, 7)
lower_black = np.array([0,0,0]) #example value
upper_black = np.array([105,105,105]) #example value
mask = cv2.inRange(mask, lower_black, upper_black)
#mask = cv2.inRange(total_final[ymin:ymax,xmin:xmax], lower_black, upper_black)
#mask = cv2.cvtColor(maskc,cv2.COLOR_BGR2GRAY)
#cv2.imshow('in',mask)
text1 = pytesseract.image_to_string(mask, lang="eng")
#print(text1)
#text1b = pytesseract.image_to_string(mask, lang="eng")
# # Model preparation
PATH_TO_FROZEN_GRAPH = './FINAL_FILES/frozen_inference_graph_price.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = './FINAL_FILES/labelmap_price.pbtxt'
NUM_CLASSES = 1
# ## Load a (frozen) Tensorflow model into memory.
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# # Detection
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
#while True:
# Get raw pixels from the screen, save it to a Numpy array
image_np = cv2.imread(test_img_Location)
y = np.size(image_np, 0)
x = np.size(image_np, 1)
#cv2.imshow('image_np',image_np)
#image_np = cv2.resize(image_np,(800,640))
# To get real color we do this:
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Visualization of the results of a detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=3)
total_final=cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Show image with detection
#cv2.imshow(title,total_final )
ymin = int((boxes[0][0][0]*y))
xmin = int((boxes[0][0][1]*x))
ymax = int((boxes[0][0][2]*y))
xmax = int((boxes[0][0][3]*x))
#print(scores[0])
Result = np.array(total_final[ymin:ymax,xmin:xmax])
#in_range = cv2.inRange(total_final,(0,127,0),(0,255,127))
#cv2.imshow('in_range',total_final[ymin:ymax,xmin:xmax])
mask = total_final[ymin:ymax,xmin:xmax]
#mask = cv2.resize(mask,(750,300))
mask = cv2.resize(mask, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
mask = cv2.medianBlur(mask, 5)
lower_black = np.array([0,0,0]) #example value
upper_black = np.array([135,135,135]) #example value
mask = cv2.inRange(mask, lower_black, upper_black)
#mask = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
cv2.imshow('price',mask)
#cv2.imwrite('temp',total_final[ymin:ymax,xmin:xmax])
text = pytesseract.image_to_string(mask, lang="eng")
#print(text)
message = "ITEM:" + text1 + ' of Price ' + text + ' $ is missing from shelf.'+ '\n' + 'Location: '+ '\n' + textl_loc + '\n' + '\n'+ 'Time:'+ '\n' + text_time
msg = message
print(message)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(login_mail,login_password)
server.sendmail(login_mail,to_mail, msg)
server.quit()
print("Success: Email sent!")
cv2.waitKey(0)
cv2.destroyAllWindows()
if (out == 1):
print("Shelf is NOT EMPTY")
print("Finding name and price of the item from the shelf...")
# title of our window
#title = "out"
# # Model preparation
PATH_TO_FROZEN_GRAPH = './FINAL_FILES/frozen_inference_graph_item.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = './FINAL_FILES/labelmap_item.pbtxt'
NUM_CLASSES = 1
# ## Load a (frozen) Tensorflow model into memory.
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# # Detection
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
#while True:
# Get raw pixels from the screen, save it to a Numpy array
image_np = cv2.imread(test_img_Location)
y = np.size(image_np, 0)
x = np.size(image_np, 1)
#cv2.imshow('image_np',image_np)
#image_np = cv2.resize(image_np,(800,640))
# To get real color we do this:
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Visualization of the results of a detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=3)
total_final=cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Show image with detection
#cv2.imshow(title,total_final )
ymin = int((boxes[0][0][0]*y))
xmin = int((boxes[0][0][1]*x))
ymax = int((boxes[0][0][2]*y))
xmax = int((boxes[0][0][3]*x))
#print(scores[0])
Result = np.array(total_final[ymin:ymax,xmin:xmax])
#in_range = cv2.inRange(total_final,(0,127,0),(0,255,127))
#cv2.imshow('in_range',total_final[ymin:ymax,xmin:xmax])
maskc = cv2.resize(total_final[ymin:ymax,xmin:xmax], None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
mask = cv2.medianBlur(maskc, 7)
lower_black = np.array([0,0,0]) #example value
upper_black = np.array([125,125,125]) #example value
mask = cv2.inRange(mask, lower_black, upper_black)
mask = cv2.inRange(total_final[ymin:ymax,xmin:xmax], lower_black, upper_black)
#cv2.imshow('in',mask)
text1 = pytesseract.image_to_string(mask, lang="eng")
#print(text1)
#mask = cv2.cvtColor(maskc,cv2.COLOR_BGR2GRAY)
#text1b = pytesseract.image_to_string(mask, lang="eng")
# # Model preparation
PATH_TO_FROZEN_GRAPH = './FINAL_FILES/frozen_inference_graph_price.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = './FINAL_FILES/labelmap_price.pbtxt'
NUM_CLASSES = 1
# ## Load a (frozen) Tensorflow model into memory.
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# # Detection
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
#while True:
# Get raw pixels from the screen, save it to a Numpy array
image_np = cv2.imread(test_img_Location)
y = np.size(image_np, 0)
x = np.size(image_np, 1)
#cv2.imshow('image_np',image_np)
#image_np = cv2.resize(image_np,(800,640))
# To get real color we do this:
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Visualization of the results of a detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=3)
total_final=cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Show image with detection
#cv2.imshow(title,total_final )
ymin = int((boxes[0][0][0]*y))
xmin = int((boxes[0][0][1]*x))
ymax = int((boxes[0][0][2]*y))
xmax = int((boxes[0][0][3]*x))
#print(scores[0])
Result = np.array(total_final[ymin:ymax,xmin:xmax])
#in_range = cv2.inRange(total_final,(0,127,0),(0,255,127))
#cv2.imshow('in_range',total_final[ymin:ymax,xmin:xmax])
mask = total_final[ymin:ymax,xmin:xmax]
#ask = cv2.resize(mask,(350,140))
mask = cv2.resize(mask, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
mask = cv2.medianBlur(mask, 5)
lower_black = np.array([0,0,0]) #example value
upper_black = np.array([125,125,125]) #example value
mask = cv2.inRange(mask, lower_black, upper_black)
#mask = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
#cv2.imshow('price',mask)
#cv2.imwrite('temp',total_final[ymin:ymax,xmin:xmax])
text = pytesseract.image_to_string(mask, lang="eng")
#print(text)
message = "ITEM:" + text1 + ' of Price ' + text + ' $ is prsent in the shelf.'+ '\n' + 'Location: '+ '\n' + textl_loc + '\n' + '\n'+ 'Time:'+ '\n' + text_time
msg = message
print(message)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(login_mail,login_password)
server.sendmail(login_mail,to_mail, message)
server.quit()
print("Success: Email sent!")
cv2.waitKey(0)
cv2.destroyAllWindows()