-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_distancing_analyser.py
214 lines (159 loc) · 7.12 KB
/
social_distancing_analyser.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
import time
import cv2
import numpy as np
confid = 0.5
thresh = 0.5
vid_path = "./videos/video.mp4"
# Calibration needed for each video
def calibrated_dist(p1, p2):
return ((p1[0] - p2[0]) ** 2 + 550 / ((p1[1] + p2[1]) / 2) * (p1[1] - p2[1]) ** 2) ** 0.5
def isclose(p1, p2):
c_d = calibrated_dist(p1, p2)
calib = (p1[1] + p2[1]) / 2
if 0 < c_d < 0.15 * calib:
return 1
elif 0 < c_d < 0.2 * calib:
return 2
else:
return 0
labelsPath = "./coco.names"
LABELS = open(labelsPath).read().strip().split("\n")
np.random.seed(42)
weightsPath = "./yolov3.weights"
configPath = "./yolov3.cfg"
###### use this for faster processing (caution: slighly lower accuracy) ###########
# weightsPath = "./yolov3-tiny.weights" ## https://pjreddie.com/media/files/yolov3-tiny.weights
# configPath = "./yolov3-tiny.cfg" ## https://github.com/pjreddie/darknet/blob/master/cfg/yolov3-tiny.cfg
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
vs = cv2.VideoCapture(vid_path)
writer = None
(W, H) = (None, None)
fl = 0
q = 0
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
if W is None or H is None:
(H, W) = frame.shape[:2]
q = W
frame = frame[0:H, 200:q]
(H, W) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if LABELS[classID] == "person":
if confidence > confid:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, confid, thresh)
if len(idxs) > 0:
status = list()
idf = idxs.flatten()
close_pair = list()
s_close_pair = list()
center = list()
dist = list()
for i in idf:
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
center.append([int(x + w / 2), int(y + h / 2)])
status.append(0)
for i in range(len(center)):
for j in range(len(center)):
g = isclose(center[i], center[j])
if g == 1:
close_pair.append([center[i], center[j]])
status[i] = 1
status[j] = 1
elif g == 2:
s_close_pair.append([center[i], center[j]])
if status[i] != 1:
status[i] = 2
if status[j] != 1:
status[j] = 2
total_p = len(center)
low_risk_p = status.count(2)
high_risk_p = status.count(1)
safe_p = status.count(0)
kk = 0
for i in idf:
sub_img = frame[10:170, 10:W - 10]
black_rect = np.ones(sub_img.shape, dtype=np.uint8) * 0
res = cv2.addWeighted(sub_img, 0.77, black_rect, 0.23, 1.0)
frame[10:170, 10:W - 10] = res
cv2.putText(frame, "Social Distancing Analyser wrt. COVID-19", (210, 45),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (20, 60), (510, 160), (170, 170, 170), 2)
cv2.putText(frame, "Connecting lines shows closeness among people. ", (30, 80),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 1)
cv2.putText(frame, "-- YELLOW: CLOSE", (50, 110),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1)
cv2.putText(frame, "-- RED: VERY CLOSE", (50, 130),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
cv2.rectangle(frame, (535, 60), (W - 20, 160), (170, 170, 170), 2)
cv2.putText(frame, "Bounding box shows the level of risk to the person.", (545, 80),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 1)
cv2.putText(frame, "-- DARK RED: HIGH RISK", (565, 110),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 150), 1)
cv2.putText(frame, "-- ORANGE: LOW RISK", (565, 130),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 120, 255), 1)
cv2.putText(frame, "-- GREEN: SAFE", (565, 150),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
tot_str = "TOTAL COUNT: " + str(total_p)
high_str = "HIGH RISK COUNT: " + str(high_risk_p)
low_str = "LOW RISK COUNT: " + str(low_risk_p)
safe_str = "SAFE COUNT: " + str(safe_p)
sub_img = frame[H - 120:H, 0:210]
black_rect = np.ones(sub_img.shape, dtype=np.uint8) * 0
res = cv2.addWeighted(sub_img, 0.8, black_rect, 0.2, 1.0)
frame[H - 120:H, 0:210] = res
cv2.putText(frame, tot_str, (10, H - 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
cv2.putText(frame, safe_str, (10, H - 65),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1)
cv2.putText(frame, low_str, (10, H - 40),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 120, 255), 1)
cv2.putText(frame, high_str, (10, H - 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 150), 1)
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
if status[kk] == 1:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 150), 2)
elif status[kk] == 0:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 120, 255), 2)
kk += 1
for h in close_pair:
cv2.line(frame, tuple(h[0]), tuple(h[1]), (0, 0, 255), 2)
for b in s_close_pair:
cv2.line(frame, tuple(b[0]), tuple(b[1]), (0, 255, 255), 2)
cv2.imshow('Social distancing analyser', frame)
cv2.waitKey(1)
if writer is None:
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
writer = cv2.VideoWriter("output.mp4", fourcc, 30,
(frame.shape[1], frame.shape[0]), True)
writer.write(frame)
print("Processing finished: open output.mp4")
writer.release()
vs.release()