-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeople_count.py
47 lines (32 loc) · 1.35 KB
/
people_count.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
import cv2
import numpy as np
import imutils
protopath = "MobileNetSSD_deploy.prototxt"
modelpath = "MobileNetSSD_deploy.caffemodel"
detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath)
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
def main(filePath):
image = cv2.imread(filePath)
image = imutils.resize(image, width=600)
(H, W) = image.shape[:2]
blob = cv2.dnn.blobFromImage(image, 0.007843, (W, H), 127.5)
detector.setInput(blob)
person_detections = detector.forward()
count = 0
for i in np.arange(0, person_detections.shape[2]):
confidence = person_detections[0, 0, i, 2]
if confidence > 0.75:
idx = int(person_detections[0, 0, i, 1])
if CLASSES[idx] != "person":
continue
count+=1
person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])
(startX, startY, endX, endY) = person_box.astype("int")
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
# cv2.imshow("Results", image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return count