face_detect.py 2.83 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
2
import cv2

3
# Haarcascades
4
5
6
7
faceDet_one = cv2.CascadeClassifier('resources/haarcascade/haarcascade_frontalface_default.xml')
faceDet_two = cv2.CascadeClassifier('resources/haarcascade/haarcascade_frontalface_alt2.xml')
faceDet_three = cv2.CascadeClassifier('resources/haarcascade/haarcascade_frontalface_alt.xml')
faceDet_four = cv2.CascadeClassifier('resources/haarcascade/haarcascade_frontalface_alt_tree.xml')
Arne Gerdes's avatar
Arne Gerdes committed
8
9


tihmels's avatar
tihmels committed
10
def extract_faces(image, resize):
11
12
13
14
15
16
17
    """
    Calls the method through which the face is to be found in the image and calls the
     method on which the found face continues to be edited
    :param image: Input
    :param resize: Resize coordinates of the face
    :return: The normalized face in the image
    """
18
    faces = find_faces(image)
tihmels's avatar
tihmels committed
19
    normalized_faces = [preprocess_face(face, resize) for face in faces]
20
21
    return normalized_faces

Arne Gerdes's avatar
Arne Gerdes committed
22

Arne Gerdes's avatar
Arne Gerdes committed
23
def find_faces(image):
24
25
26
27
28
    """
    Calls the method to detect the face in the image and cuts it out
    :param image: Input
    :return: The cutted face
    """
tihmels's avatar
tihmels committed
29
    faces_coordinates = locate_faces(image)
Arne Gerdes's avatar
Arne Gerdes committed
30
    cutted_faces = [image[y:y + h, x:x + w] for (x, y, w, h) in faces_coordinates]
31
    return cutted_faces
Arne Gerdes's avatar
Arne Gerdes committed
32
33


tihmels's avatar
tihmels committed
34
def preprocess_face(face, resize):
35
36
37
38
39
40
    """
    Edit the found face
    :param face: Found face
    :param resize: Resize coordinates of the face
    :return: Edited face
    """
Arne Gerdes's avatar
Arne Gerdes committed
41
    face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
tihmels's avatar
tihmels committed
42
    face = cv2.resize(face, (resize, resize))
43
44
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    face = clahe.apply(face)
Arne Gerdes's avatar
Arne Gerdes committed
45
46
47
    return face


48
def locate_faces(image, scaleFactor=1.1, minNeighbors=6, minSize=(350, 350)):
49
50
51
52
53
54
55
56
    """
    Find the face in the image with Haarcascades
    :param image: Input
    :param scaleFactor: Parameter specifying how much the image size is reduced at each image scale
    :param minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain it
    :param minSize: Minimum possible object size. Objects smaller than that are ignored
    :return: Found face
    """
57
    face = faceDet_one.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
58
                                        flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
59

60
61
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
62

63
    face = faceDet_two.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
64
                                        flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
65

66
67
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
68

69
    face = faceDet_three.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
70
                                          flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
71

72
73
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
74

75
    face = faceDet_four.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
76
                                         flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
77

78
79
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
80
81

    return ''