-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_detector.py
36 lines (32 loc) · 1.17 KB
/
face_detector.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
import cv2
import numpy as np
def get_face_detector(modelFile=None,
configFile=None,
quantized=False):
if quantized:
if modelFile == None:
modelFile = "models/opencv_face_detector_uint8.pb"
if configFile == None:
configFile = "models/opencv_face_detector.pbtxt"
model = cv2.dnn.readNetFromTensorflow(modelFile, configFile)
else:
if modelFile == None:
modelFile = "models/res10_300x300_ssd_iter_140000.caffemodel"
if configFile == None:
configFile = "models/deploy.prototxt"
model = cv2.dnn.readNetFromCaffe(configFile, modelFile)
return model
def find_faces(img, model):
h, w = img.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(img, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
model.setInput(blob)
res = model.forward()
faces = []
for i in range(res.shape[2]):
confidence = res[0, 0, i, 2]
if confidence > 0.5:
box = res[0, 0, i, 3:7] * np.array([w, h, w, h])
(x, y, x1, y1) = box.astype("int")
faces.append([x, y, x1, y1])
return faces