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

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')

10
11
12
13
14
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='logs/sorted_set_facedetector.log',
                    filemode='w')

Arne Gerdes's avatar
Arne Gerdes committed
15
16
emotions = ["neutral", "anger", "disgust", "happy", "surprise"]  # Define emotions

tihmels's avatar
tihmels committed
17
18
19
20
totalFiles: int = 0
totalFaces: int = 0
undetected: list = []

Arne Gerdes's avatar
Arne Gerdes committed
21
22

def detect_faces(emotion):
23
24
25
26
27
    files = glob.glob('Basis_data/sorted_set/%s/*' % emotion)  # Get list of all images with emotion

    global undetected
    global totalFaces
    global totalFiles
Arne Gerdes's avatar
Arne Gerdes committed
28

tihmels's avatar
tihmels committed
29
    logging.debug("{} files in folder {}".format(len(files), emotion))
30
31

    fileNumber = 0
Arne Gerdes's avatar
Arne Gerdes committed
32
33
34
35
36
    for f in files:
        frame = cv2.imread(f)  # Open image
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Convert image to grayscale

        # Detect face using 4 different classifiers
37
        face = faceDet.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
Arne Gerdes's avatar
Arne Gerdes committed
38
                                        flags=cv2.CASCADE_SCALE_IMAGE)
39
        face_two = faceDet_two.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
Arne Gerdes's avatar
Arne Gerdes committed
40
                                                flags=cv2.CASCADE_SCALE_IMAGE)
41
        face_three = faceDet_three.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
Arne Gerdes's avatar
Arne Gerdes committed
42
                                                    flags=cv2.CASCADE_SCALE_IMAGE)
43
        face_four = faceDet_four.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=6, minSize=(10, 10),
Arne Gerdes's avatar
Arne Gerdes committed
44
45
46
47
48
49
50
51
52
53
54
55
                                                  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:
56
            undetected.append(f)
Arne Gerdes's avatar
Arne Gerdes committed
57
58
59

        # Cut and save face
        for (x, y, w, h) in facefeatures:  # get coordinates and size of rectangle containing face
tihmels's avatar
tihmels committed
60
            logging.debug("face found in file: {}".format(f))
61
            totalFaces += 1
Arne Gerdes's avatar
Arne Gerdes committed
62
63
64
65
            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
66
                cv2.imwrite('Basis_data/dataset/%s/%s.jpg' % (emotion, fileNumber), out)  # Write image
Arne Gerdes's avatar
Arne Gerdes committed
67
68
            except:
                pass  # If error, pass file
69

tihmels's avatar
tihmels committed
70
        totalFiles += 1  # Increment image number
71
        fileNumber += 1
Arne Gerdes's avatar
Arne Gerdes committed
72
73
74


for emotion in emotions:
75
76
77
    detect_faces(emotion)  # Call functional
    logging.debug("\n")

tihmels's avatar
tihmels committed
78
logging.debug("{} Gesichter in {} Dateien gefunden.".format(totalFaces, totalFiles))
79
80
logging.debug("Kein Gesicht wurde gefunden in:")
for f in undetected:
tihmels's avatar
tihmels committed
81
    logging.debug(f)