-
Notifications
You must be signed in to change notification settings - Fork 52
/
face_detect.py
35 lines (28 loc) · 989 Bytes
/
face_detect.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
"""
This module contains face detections functions.
"""
import cv2
faceCascade = cv2.CascadeClassifier('models/haarcascade_frontalface_default.xml')
def find_faces(image):
faces_coordinates = _locate_faces(image)
cutted_faces = [image[y:y + h, x:x + w] for (x, y, w, h) in faces_coordinates]
normalized_faces = [_normalize_face(face) for face in cutted_faces]
return zip(normalized_faces, faces_coordinates)
def _normalize_face(face):
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face = cv2.resize(face, (350, 350))
return face
def _locate_faces(image):
faces = faceCascade.detectMultiScale(
image,
scaleFactor=1.1,
minNeighbors=15,
minSize=(70, 70)
)
return faces # list of (x, y, w, h)
if __name__ == "__main__":
image = cv2.imread('test_data/test.jpg')
cv2.imshow("face", image)
for index, face in enumerate(find_faces(image)):
cv2.imshow("face %s" %index, face[0])
cv2.waitKey(0)