This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
detect_person.py
64 lines (51 loc) · 2.07 KB
/
detect_person.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
import cv2
import argparse
import time
import numpy as np
class detectPerson:
def __init__(self):
self.detector = cv2.dnn.readNetFromCaffe(
'MobileNetSSD_deploy.prototxt', 'MobileNetSSD_deploy.caffemodel')
def detectSingle(self, image):
(h, w) = image.shape[:2]
imageBlob = cv2.dnn.blobFromImage(
cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)
self.detector.setInput(imageBlob)
detections = self.detector.forward()
if len(detections) > 0:
i = np.argmax(detections[0, 0, :, 2])
label = detections[0, 0, i, 1]
confidence = detections[0, 0, i, 2]
if confidence > 0.7 and label == 15:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
return [startX, startY, endX, endY]
return [0, 0, 0, 0]
def detectMultiple(self, image):
(h, w) = image.shape[:2]
imageBlob = cv2.dnn.blobFromImage(
cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)
self.detector.setInput(imageBlob)
detections = self.detector.forward()
if len(detections) == 0:
return [[0, 0, 0, 0]]
boundingBoxes = []
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
label = detections[0, 0, i, 1]
if confidence >= 0.7 and label == 15:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
boundingBoxes.append((startX, startY, endX, endY))
return boundingBoxes
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--image', required=True)
args = parser.parse_args()
detector = detectPerson()
img = cv2.imread(args.image)
img = cv2.resize(img, (960, 720))
while True:
start = time.time()
print(detector.detectMultiple(img))
print(time.time() - start)