-
Notifications
You must be signed in to change notification settings - Fork 8
/
face_detection.py
34 lines (31 loc) · 1.46 KB
/
face_detection.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
import cv2
import numpy as np
# Resize back to original size
def resize_to_original(frame, width, height):
return cv2.resize(frame, (width, height), interpolation=cv2.INTER_AREA)
def face_detection(frame, detector, use_larger_box=False, larger_box_coef=1.0):
"""Face detection on a single frame.
Args:
frame(np.array): a single frame.
use_larger_box(bool): whether to use a larger bounding box on face detection.
larger_box_coef(float): Coef. of larger box.
Returns:
face_box_coor(List[int]): coordinates of face bouding box.
"""
detector = cv2.CascadeClassifier('./utils/haarcascade_frontalface_default.xml')
face_zone = detector.detectMultiScale(frame)
if len(face_zone) < 1:
print("ERROR: No Face Detected")
face_box_coor = [0, 0, frame.shape[0], frame.shape[1]]
elif len(face_zone) >= 2:
face_box_coor = np.argmax(face_zone, axis=0)
face_box_coor = face_zone[face_box_coor[2]]
print("Warning: More than one faces are detected(Only cropping the biggest one.)")
else:
face_box_coor = face_zone[0]
if use_larger_box:
face_box_coor[0] = max(0, face_box_coor[0] - (larger_box_coef - 1.0) / 2 * face_box_coor[2])
face_box_coor[1] = max(0, face_box_coor[1] - (larger_box_coef - 1.0) / 2 * face_box_coor[3])
face_box_coor[2] = larger_box_coef * face_box_coor[2]
face_box_coor[3] = larger_box_coef * face_box_coor[3]
return face_box_coor