-
Notifications
You must be signed in to change notification settings - Fork 197
/
social distancing analyser 2.0.py
251 lines (197 loc) · 7.94 KB
/
social distancing analyser 2.0.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
import time
import math
import cv2
import numpy as np
confid = 0.5
thresh = 0.5
# vname=""
vname=input("Video name in videos folder: ")
if(vname==""):
vname="Town.mp4"
vid_path = "./videos/"+vname
angle_factor = 0.8
H_zoom_factor = 1.2
# Calibration needed for each video
def dist(c1, c2):
return ((c1[0] - c2[0]) ** 2 + (c1[1] - c2[1]) ** 2) ** 0.5
def T2S(T):
S = abs(T/((1+T**2)**0.5))
return S
def T2C(T):
C = abs(1/((1+T**2)**0.5))
return C
def isclose(p1,p2):
c_d = dist(p1[2], p2[2])
if(p1[1]<p2[1]):
a_w = p1[0]
a_h = p1[1]
else:
a_w = p2[0]
a_h = p2[1]
T = 0
try:
T=(p2[2][1]-p1[2][1])/(p2[2][0]-p1[2][0])
except ZeroDivisionError:
T = 1.633123935319537e+16
S = T2S(T)
C = T2C(T)
d_hor = C*c_d
d_ver = S*c_d
vc_calib_hor = a_w*1.3
vc_calib_ver = a_h*0.4*angle_factor
c_calib_hor = a_w *1.7
c_calib_ver = a_h*0.2*angle_factor
# print(p1[2], p2[2],(vc_calib_hor,d_hor),(vc_calib_ver,d_ver))
if (0<d_hor<vc_calib_hor and 0<d_ver<vc_calib_ver):
return 1
elif 0<d_hor<c_calib_hor and 0<d_ver<c_calib_ver:
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()]
FR=0
vs = cv2.VideoCapture(vid_path)
# vs = cv2.VideoCapture(0) ## USe this if you want to use webcam feed
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]
FW=W
if(W<1075):
FW = 1075
FR = np.zeros((H+210,FW,3), np.uint8)
col = (255,255,255)
FH = H + 210
FR[:] = col
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 = []
idf = idxs.flatten()
close_pair = []
s_close_pair = []
center = []
co_info = []
for i in idf:
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
cen = [int(x + w / 2), int(y + h / 2)]
center.append(cen)
cv2.circle(frame, tuple(cen),1,(0,0,0),1)
co_info.append([w, h, cen])
status.append(0)
for i in range(len(center)):
for j in range(len(center)):
g = isclose(co_info[i],co_info[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:
cv2.line(FR,(0,H+1),(FW,H+1),(0,0,0),2)
cv2.putText(FR, "Social Distancing Analyser wrt. COVID-19", (210, H+60),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
cv2.rectangle(FR, (20, H+80), (510, H+180), (100, 100, 100), 2)
cv2.putText(FR, "Connecting lines shows closeness among people. ", (30, H+100),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (100, 100, 0), 2)
cv2.putText(FR, "-- YELLOW: CLOSE", (50, H+90+40),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 170, 170), 2)
cv2.putText(FR, "-- RED: VERY CLOSE", (50, H+40+110),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# cv2.putText(frame, "-- PINK: Pathway for Calibration", (50, 150),
# cv2.FONT_HERSHEY_SIMPLEX, 0.5, (180,105,255), 1)
cv2.rectangle(FR, (535, H+80), (1060, H+140+40), (100, 100, 100), 2)
cv2.putText(FR, "Bounding box shows the level of risk to the person.", (545, H+100),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (100, 100, 0), 2)
cv2.putText(FR, "-- DARK RED: HIGH RISK", (565, H+90+40),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 150), 2)
cv2.putText(FR, "-- ORANGE: LOW RISK", (565, H+150),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 120, 255), 2)
cv2.putText(FR, "-- GREEN: SAFE", (565, H+170),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 150, 0), 2)
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)
cv2.putText(FR, tot_str, (10, H +25),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
cv2.putText(FR, safe_str, (200, H +25),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 170, 0), 2)
cv2.putText(FR, low_str, (380, H +25),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 120, 255), 2)
cv2.putText(FR, high_str, (630, H +25),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 150), 2)
(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)
FR[0:H, 0:W] = frame
frame = FR
cv2.imshow('Social distancing analyser', frame)
cv2.waitKey(1)
if writer is None:
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
writer = cv2.VideoWriter("op_"+vname, fourcc, 30,
(frame.shape[1], frame.shape[0]), True)
writer.write(frame)
print("Processing finished: open"+"op_"+vname)
writer.release()
vs.release()