sorted_set_facedetector.py 2.54 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
2
3
4
5
6
7
8
9
10
11
12
import cv2
import glob

faceDet = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_default.xml')
faceDet_two = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_alt2.xml')
faceDet_three = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_alt.xml')
faceDet_four = cv2.CascadeClassifier('Haarcascade/haarcascade_frontalface_alt_tree.xml')

emotions = ["neutral", "anger", "disgust", "happy", "surprise"]  # Define emotions


def detect_faces(emotion):
tihmels's avatar
tihmels committed
13
    files = glob.glob('Basis_data/sorted_set/%s/*w' % emotion)  # Get list of all images with emotion
Arne Gerdes's avatar
Arne Gerdes committed
14
15
16
17
18
19

    filenumber = 0
    for f in files:
        frame = cv2.imread(f)  # Open image
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Convert image to grayscale

tihmels's avatar
tihmels committed
20
21
22
23
        scaleFactor = 1.05
        minNeighbors = 10
        minSize = (20, 20)

Arne Gerdes's avatar
Arne Gerdes committed
24
        # Detect face using 4 different classifiers
tihmels's avatar
tihmels committed
25
        face = faceDet.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
Arne Gerdes's avatar
Arne Gerdes committed
26
                                        flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
27
        face_two = faceDet_two.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
Arne Gerdes's avatar
Arne Gerdes committed
28
                                                flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
29
        face_three = faceDet_three.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
Arne Gerdes's avatar
Arne Gerdes committed
30
                                                    flags=cv2.CASCADE_SCALE_IMAGE)
tihmels's avatar
tihmels committed
31
        face_four = faceDet_four.detectMultiScale(gray, scaleFactor, minNeighbors, minSize,
Arne Gerdes's avatar
Arne Gerdes committed
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
                                                  flags=cv2.CASCADE_SCALE_IMAGE)

        # Go over detected faces, stop at first detected face, return empty if no face.
        if len(face) == 1:
            facefeatures = face
        elif len(face_two) == 1:
            facefeatures = face_two
        elif len(face_three) == 1:
            facefeatures = face_three
        elif len(face_four) == 1:
            facefeatures = face_four
        else:
            facefeatures = ""

        # Cut and save face
        for (x, y, w, h) in facefeatures:  # get coordinates and size of rectangle containing face
            print("face found in file: %s" % f)
            gray = gray[y:y + h, x:x + w]  # Cut the frame to size

            try:
                out = cv2.resize(gray, (250, 250))  # Resize face so all images have same size
                cv2.imwrite('Basis_data/dataset/%s/%s.jpg' % (emotion, filenumber), out)  # Write image
            except:
                pass  # If error, pass file
        filenumber += 1  # Increment image number


for emotion in emotions:
    detect_faces(emotion)  # Call functiona