face_detect.py 3.49 KB
Newer Older
tihmels's avatar
tihmels committed
1 2 3
# ***************************************************
# * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
tihmels's avatar
tihmels committed
4
# * This file is part of project mood.
tihmels's avatar
tihmels committed
5
# *
tihmels's avatar
tihmels committed
6
# * project mood can not be copied and/or distributed without the express
tihmels's avatar
tihmels committed
7 8 9 10 11 12 13 14
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/


15 16
import cv2

17
# Haarcascades
18 19 20 21
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')
22 23


tihmels's avatar
tihmels committed
24
def extract_faces(image, resize):
25 26 27 28 29 30 31
    """
    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
    """
32
    faces = find_faces(image)
tihmels's avatar
tihmels committed
33
    normalized_faces = [preprocess_face(face, resize) for face in faces]
34 35
    return normalized_faces

Arne Gerdes's avatar
Arne Gerdes committed
36

37
def find_faces(image):
38 39 40 41 42
    """
    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
43
    faces_coordinates = locate_faces(image)
44
    cutted_faces = [image[y:y + h, x:x + w] for (x, y, w, h) in faces_coordinates]
45
    return cutted_faces
46 47


tihmels's avatar
tihmels committed
48
def preprocess_face(face, resize):
49 50 51 52 53 54
    """
    Edit the found face
    :param face: Found face
    :param resize: Resize coordinates of the face
    :return: Edited face
    """
55
    face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
tihmels's avatar
tihmels committed
56
    face = cv2.resize(face, (resize, resize))
57 58
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    face = clahe.apply(face)
59 60 61
    return face


62
def locate_faces(image, scaleFactor=1.1, minNeighbors=6, minSize=(350, 350)):
63 64 65 66 67 68 69 70
    """
    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
    """
71
    face = faceDet_one.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
72
                                        flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
73

74 75
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
76

77
    face = faceDet_two.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
78
                                        flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
79

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

83
    face = faceDet_three.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
84
                                          flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
85

86 87
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
88

89
    face = faceDet_four.detectMultiScale(image, scaleFactor, minNeighbors, minSize=minSize,
Arne Gerdes's avatar
Arne Gerdes committed
90
                                         flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
91

92 93
    if len(face) == 1:
        return face
tihmels's avatar
tihmels committed
94 95

    return ''