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

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

11
12
13
14
15
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
16
17
emotions = ["neutral", "anger", "disgust", "happy", "surprise"]  # Define emotions

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

29
30
31
    logging.debug("%d files in folder %s" % (len(files), emotion))

    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
60
61
            logging.debug("face found in file: %s" % f)
            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
70
71

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


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

logging.debug(str(totalFaces) + " Gesichter in " + str(totalFiles) + " Dateien gefunden.\n")

logging.debug("Kein Gesicht wurde gefunden in:")
for f in undetected:
    logging.debug(f)