-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detector.py
91 lines (82 loc) · 2.78 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
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
import cv2
import mediapipe as mp
class Face:
def __init__(self):
# Face detection
self.i =None
self.j=None
self.id = None
self.confidence = None
self.bbox = None
self.centroid= None
self.origbbox = None
self.img = None
# Facial Landmarks
self.landmarks = None
self.hland = None
# Face recognition
self.aligned_face = None
self.embedding = None
self.name = None
self.distance = None
self.best_index = None
# Mouth track
self.mouth = None
# Head pose estimation
self.head = None
# Spoof detection
self.spoof = None
self.spoof_score = None
self.x=None
self.y=None
self.w=None
self.h=None
# Crop face based on its bounding box
def get_face(frame, bbox):
real_h, real_w, c = frame.shape
x,y,w,h = bbox
y1 = 0 if y < 0 else y
x1 = 0 if x < 0 else x
y2 = real_h if y1 + h > real_h else y + h
x2 = real_w if x1 + w > real_w else x + w
face = frame[y1:y2,x1:x2,:]
return face
def expand_bbox(frame, bbox):
real_h, real_w, _ = frame.shape
x,y,w,h = bbox
z = 1.5
ch = 0.145*h
x1,y1 = int(x - w*(z-1)/2) , int(y - h*(z-1)/2 -ch)
x1, y1 = x1 if x1>=0 else 0, y1 if y1>=0 else 0
w1, h1 = real_w if int(x1+w*z) > real_w else int(w*z) , real_h if int(y1+h*z) > real_h else int(h*z)
return x1,y1,w1,h1
def detect_faces(frame, confidence = 0.7):
"""
Outputs the frame with detected face, alert_bool and cropped face
"""
faces = None
mp_face_detection = mp.solutions.face_detection
with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence= confidence) as face_detector:
# To improve performance, optionally mark the frame as not writeable to
# pass by reference.
frame.flags.writeable = False
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#Face detection:
results = face_detector.process(frame)
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# Get bboxes for detected faces
if results.detections:
faces = []
for id, detection in enumerate(results.detections):
face = Face()
face.id = id
bbox = detection.location_data.relative_bounding_box
ih, iw, ic = frame.shape
bbox = int(bbox.xmin * iw), int(bbox.ymin * ih), int(bbox.width * iw), int(bbox.height * ih)
face.origbbox = bbox
face.confidence = detection.score
face.bbox = expand_bbox(frame, face.origbbox)
face.img = get_face(frame, face.bbox)
faces.append(face)
return faces