-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_IPCAM HW.py
179 lines (164 loc) · 7.01 KB
/
final_IPCAM HW.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
##import serial
##ser = serial.Serial(port = "COM3", baudrate = '9600',timeout = 0.5)
##
import urllib.request
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tensorflow as tf
from distutils.version import StrictVersion
from collections import defaultdict
from PIL import Image
from object_detection.utils import ops as utils_ops
import smtplib
from email.message import EmailMessage
import imghdr
from time import sleep
email_add = 'harsini20072001@gmail.com'
email_pass = "Grijesh@2004"
msg = EmailMessage()
msg['Subject'] = "Face Mask"
msg['From'] = "harsini20072001@gmail.com"
msg['To'] = "harsini20072001@gmail.com"
msg.set_content("With Out Mask")
def email():
with open('capture.jpg','rb')as f:
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
msg.add_attachment(file_data, maintype = 'image', subtype = file_type, filename = file_name)
with smtplib.SMTP_SSL('smtp.gmail.com',465)as smtp:
smtp.login(email_add,email_pass)
smtp.send_message(msg)
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
if StrictVersion(tf.__version__) < StrictVersion('1.9.0'):
raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')
from utils import label_map_util
from utils import visualization_utils as vis_util
MODEL_NAME = 'inference_graph'
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = 'training/labelmap.pbtxt'
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='')
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
def run_inference_for_single_image(image, graph):
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
global a2
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
if a1 == 1 and output_dict['detection_classes'][0] == 1 and output_dict['detection_scores'][0] > 0.70:
print('No Mask')
ser.write('2'.encode())
sleep(1)
a2=1
if a1 == 1 and output_dict['detection_classes'][0] == 2 and output_dict['detection_scores'][0] > 0.70 :
print('InCorrect Mask')
ser.write('3'.encode())
sleep(1)
a2=1
if a1 == 1 and output_dict['detection_classes'][0] == 3 and output_dict['detection_scores'][0] > 0.70:
print('Correct Mask')
ser.write('1'.encode())
sleep(1)
if a2==1:
a2=0
sleep(1)
email()
sleep(1)
return output_dict
def serial_func():
print("serial enabled 1")
a=ser.readline().decode('ascii') # reading serial data
print(a)
b=a
print(len(b))
global a1
if len(b)>=2:
for letter in b:
if(letter == 'M'):
D1 = b[1]
a1 = int(D1)
print("RECIVED VALUE: ",a1)
if a1 == 5:
a1=1
import serial
ser = serial.Serial('COM3',baudrate=9600,timeout=1)
ser.flushInput()
a1=0
a2=0
import cv2
url='http://192.168.43.1:8080/shot.jpg'
try:
with detection_graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
while True:
imgPath = urllib.request.urlopen(url)
imgNp = np.array(bytearray(imgPath.read()), dtype=np.uint8)
image_np = cv2.imdecode(imgNp, -1)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
cv2.imwrite('capture.jpg',image_np)
serial_func()
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
cv2.imshow('object_detection', cv2.resize(image_np,(800,600)))
if cv2.waitKey(1)& 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
except Exception as e:
print(e)
#cap.release()