-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceDetector.py
38 lines (34 loc) · 1.29 KB
/
faceDetector.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
import cv2
class faceDetector:
def __init__(self, net):
self.__net = net
def detectFaceOpenCVDnn(self, frame, threshold=0.7):
frameHeight = frame.shape[0]
frameWidth = frame.shape[1]
blob = cv2.dnn.blobFromImage(
frame, 1.0, (300, 300), [104, 117, 123], True, False,
)
center = []
self.__net.setInput(blob)
detections = self.__net.forward()
boxes = []
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > threshold:
x1 = int(detections[0, 0, i, 3] * frameWidth)
y1 = int(detections[0, 0, i, 4] * frameHeight)
x2 = int(detections[0, 0, i, 5] * frameWidth)
y2 = int(detections[0, 0, i, 6] * frameHeight)
center = [int((x1+x2)/2), int((y1+y2)/2)]
boxes.append([x1, y1, x2, y2])
cv2.rectangle(
frame,
(x1, y1),
(x2, y2),
(0, 255, 0),
int(round(frameHeight / 150)),
8,
)
cv2.circle(frame, (center[0], center[1]),
4, (0, 0, 255), -1)
return frame, boxes, center