-
Notifications
You must be signed in to change notification settings - Fork 0
/
room_capacity_checker.py
140 lines (107 loc) · 4.36 KB
/
room_capacity_checker.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
import time
import cv2
import numpy as np
from mask_detection import faceNet,maskNet,detect_and_predict_mask
confid = 0.5
thresh = 0.5
vid_path = "4.mp4"
save_path="o4.mp4"
room_safe_capacity = 3
labelsPath = "coco.names"
LABELS = open(labelsPath).read().strip().split("\n")
np.random.seed(42)
weightsPath = "yolov3.weights"
configPath = "yolov3.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)
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, 0:q]
(H, W) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layerOutputs = net.forward(ln)
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()
center = 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)
total_p = len(center)
safe_p = status.count(0)
kk = 0
for i in idf:
cv2.putText(frame, "Confernce Room GPTW wrt. COVID-19", (50, 45),cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
tot_str = "TOTAL COUNT: " + str(total_p)
safe_str = "SAFE COUNT: " + str(room_safe_capacity)
if int(total_p)>room_safe_capacity:
total_color=(0, 0, 150)
else:
total_color=(255, 255, 255)
cv2.putText(frame, tot_str, (10, H - 100),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, total_color, 2)
cv2.putText(frame, safe_str, (10, H - 65),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
#------------detect wearing a mask or not--------------
f=frame[y:y+h,x:x+w]
(locs, preds) = detect_and_predict_mask(f, faceNet, maskNet)
for (box, pred) in zip(locs, preds):
(startX, startY, endX, endY) = box
(mask, withoutMask) = pred
if withoutMask > mask:
color = (0,0,255)
label = "{}: {:.2f}%".format("No Mask", max(mask, withoutMask) * 100)
else:
color = (0, 255, 0)
label = "{}: {:.2f}%".format("Mask", max(mask, withoutMask) * 100)
cv2.putText(frame, label, (startX+x, startY+y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.65, color, 2)
cv2.rectangle(frame, (startX+x, startY+y), (endX+x, endY+y), color, 2)
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
cv2.imshow('Conference Room GPTW wrt. Covid19', frame)
cv2.waitKey(1)
if writer is None:
fourcc = cv2.VideoWriter_fourcc(*'DIVX') #MJPG/MP4V
writer = cv2.VideoWriter(save_path, fourcc, 30,(frame.shape[1], frame.shape[0]), True)
writer.write(frame)
print("Processing finished: open {}".format(save_path))
writer.release()
vs.release()