-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_distance_detection.py
282 lines (207 loc) · 9.42 KB
/
social_distance_detection.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
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2
import os
import sys
import cv2
from math import pow, sqrt
import playsound
from threading import Thread
def detect_and_predict_mask(frame, faceNet, maskNet):
# grab the dimensions of the frame and then construct a blob
# from it
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
(104.0, 177.0, 123.0))
# pass the blob through the network and obtain the face detections
faceNet.setInput(blob)
detections = faceNet.forward()
# initialize our list of faces, their corresponding locations,
# and the list of predictions from our face mask network
faces = []
locs = []
preds = []
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with
# the detection
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the confidence is
# greater than the minimum confidence
if confidence > args["confidence"]:
# compute the (x, y)-coordinates of the bounding box for
# the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# ensure the bounding boxes fall within the dimensions of
# the frame
(startX, startY) = (max(0, startX), max(0, startY))
(endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# extract the face ROI, convert it from BGR to RGB channel
# ordering, resize it to 224x224, and preprocess it
face = frame[startY:endY, startX:endX]
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
face = cv2.resize(face, (224, 224))
face = img_to_array(face)
face = preprocess_input(face)
face = np.expand_dims(face, axis=0)
# add the face and bounding boxes to their respective
# lists
faces.append(face)
locs.append((startX, startY, endX, endY))
# only make a predictions if at least one face was detected
if len(faces) > 0:
# for faster inference we'll make batch predictions on *all*
# faces at the same time rather than one-by-one predictions
# in the above `for` loop
preds = maskNet.predict(faces)
# return a 2-tuple of the face locations and their corresponding
# locations
return (locs, preds)
# Parse the arguments from command line
arg = argparse.ArgumentParser(description='Social distance detection')
arg.add_argument("-a", "--alarm", type=str, default="", help="path alarm .WAV file")
arg.add_argument('-v', '--video', type = str, default = '', help = 'Video file path. If no path is given, video is captured using device.')
arg.add_argument('-m', '--model', required = True, help = "Path to the pretrained model.")
arg.add_argument('-p', '--prototxt', required = True, help = 'Prototxt of the model.')
arg.add_argument('-l', '--labels', required = True, help = 'Labels of the dataset.')
arg.add_argument('-c', '--confidence', type = float, default = 0.2, help='Set confidence for detecting objects')
arg.add_argument("-f", "--face", type=str, default="face_detector", help="path to face detector model directory")
arg.add_argument("-m1", "--model1", type=str, default="model/mask_detector.model", help="path to trained face mask detector model")
#arg.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections")
args = vars(arg.parse_args())
ALARM_ON = False
def sound_alarm(path):
# play an alarm sound
playsound.playsound(path)
labels = [line.strip() for line in open(args['labels'])]
# Generate random bounding box bounding_box_color for each label
bounding_box_color = np.random.uniform(0, 255, size=(len(labels), 3))
print("[INFO] loading face detector model...")
prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"])
weightsPath = os.path.sep.join([args["face"], "res10_300x300_ssd_iter_140000.caffemodel"])
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
# load the face mask detector model from disk
print("[INFO] loading face mask detector model...")
maskNet = load_model(args["model1"])
# Load model
print("\nLoading model...\n")
network = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
print("\nStreaming video using device...\n")
# Capture video from file or through device
if args['video']:
cap = cv2.VideoCapture(args['video'])
else:
cap = cv2.VideoCapture(0)
frame_no = 0
while True:
frame_no = frame_no+1
# Capture one frame after another
ret, frame = cap.read()
#frame = imutils.resize(frame, width=1000)
# detect faces in the frame and determine if they are wearing a
if not ret:
break
(h, w) = frame.shape[:2]
# Resize the frame to suite the model requirements. Resize the frame to 300X300 pixels
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)
network.setInput(blob)
detections = network.forward()
pos_dict = dict()
coordinates = dict()
# Focal length
F = 615
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > args["confidence"]:
class_id = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype('int')
# Filtering only persons detected in the frame. Class Id of 'person' is 15
if class_id == 15.00:
(locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
# loop over the detected face locations and their corresponding
# locations
for (box, pred) in zip(locs, preds):
# unpack the bounding box and predictions
(startX_mask, startY_mask, endX_mask, endY_mask) = box
(mask, withoutMask) = pred
# determine the class label and color we'll use to draw
# the bounding box and text
if mask > withoutMask:
label_mask = "Mask"
color = (0, 255, 0)
ALARM_ON = False
else:
label_mask="No Mask"
color = (0, 0, 255)
if not ALARM_ON:
ALARM_ON = True
if args["alarm"] != "":
t = Thread(target=sound_alarm, args=(args["alarm"],))
t.deamon = True
t.start()
# Draw bounding box for the object
cv2.rectangle(frame, (startX, startY), (endX, endY), bounding_box_color[class_id], 2)
label = "{}: {:.2f}%".format(labels[class_id], confidence * 100)
label_mask = "{}: {:.2f}%".format(label_mask, max(mask, withoutMask) * 100)
print("{}".format(label))
cv2.putText(frame, label, (startX_mask, startY_mask - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
cv2.rectangle(frame, (startX_mask, startY_mask), (endX_mask, endY_mask), color, 2)
coordinates[i] = (startX, startY, endX, endY)
# Mid point of bounding box
x_mid = round((startX+endX)/2,4)
y_mid = round((startY+endY)/2,4)
height = round(endY-startY,4)
# Distance from camera based on triangle similarity
distance = (165 * F)/height
print("Distance(cm):{dist}\n".format(dist=distance))
# Mid-point of bounding boxes (in cm) based on triangle similarity technique
x_mid_cm = (x_mid * distance) / F
y_mid_cm = (y_mid * distance) / F
pos_dict[i] = (x_mid_cm,y_mid_cm,distance)
# Distance between every object detected in a frame
close_objects = set()
for i in pos_dict.keys():
for j in pos_dict.keys():
if i < j:
dist = sqrt(pow(pos_dict[i][0]-pos_dict[j][0],2) + pow(pos_dict[i][1]-pos_dict[j][1],2) + pow(pos_dict[i][2]-pos_dict[j][2],2))
# Check if distance less than 2 metres or 200 centimetres
if dist < 200:
close_objects.add(i)
close_objects.add(j)
for i in pos_dict.keys():
if i in close_objects:
COLOR = (0,0,255)
if not ALARM_ON:
ALARM_ON = True
if args["alarm"] != "":
t = Thread(target=sound_alarm, args=(args["alarm"],))
t.deamon = True
t.start()
else:
COLOR = (0,255,0)
ALARM_ON = False
(startX, startY, endX, endY) = coordinates[i]
cv2.rectangle(frame, (startX, startY), (endX, endY), COLOR, 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
# Convert cms to feet
cv2.putText(frame, 'Depth: {i} ft'.format(i=round(pos_dict[i][2]/30.48,4)), (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR, 2)
cv2.namedWindow('Frame',cv2.WINDOW_NORMAL)
# Show frame
cv2.imshow('Frame', frame)
cv2.resizeWindow('Frame',800,600)
key = cv2.waitKey(1) & 0xFF
# Press `q` to exit
if key == ord("q"):
break
# Clean
cap.release()
cv2.destroyAllWindows()