sorted_set_facedetector.py 2.04 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
2
import cv2
import glob
3
import logging
tihmels's avatar
tihmels committed
4
from face_detect import locate_faces
Arne Gerdes's avatar
Arne Gerdes committed
5

6
7
8
9
10
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')

11
emotions = ["happy", "neutral", "surprise"]  # Emotionen die verarbeitet werden sollen
Arne Gerdes's avatar
Arne Gerdes committed
12

tihmels's avatar
tihmels committed
13
14
15
16
totalFiles: int = 0
totalFaces: int = 0
undetected: list = []

Arne Gerdes's avatar
Arne Gerdes committed
17
def detect_faces(emotion):
18
    files = glob.glob('basis_data/sorted_set/{}/*'.format(emotion))  # Holt alle Dateien zu einer Emotion aus dem sorted_set
19
20
21
22

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

tihmels's avatar
tihmels committed
24
    logging.debug("{} files in folder {}".format(len(files), emotion))
25
26

    fileNumber = 0
Arne Gerdes's avatar
Arne Gerdes committed
27
28
    for f in files:
        frame = cv2.imread(f)  # Open image
29
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Convert image to grayscale
Arne Gerdes's avatar
Arne Gerdes committed
30

31
        facefeatures = locate_faces(gray, 1.1, 10, (40, 40))
tihmels's avatar
tihmels committed
32
33

        if facefeatures is '':
34
            undetected.append(f)
Arne Gerdes's avatar
Arne Gerdes committed
35

36
37
38
39
40
41
42
43
44
45
46
        else:
            # Cut and save face
            for (x, y, w, h) in facefeatures:  # get coordinates and size of rectangle containing face
                logging.debug("face found in file: {}".format(f))
                totalFaces += 1
                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/{}/{}.jpg'.format(emotion, fileNumber), out)  # Write image
                except:
                    pass  # If error, pass file
47

tihmels's avatar
tihmels committed
48
        totalFiles += 1  # Increment image number
49
        fileNumber += 1
tihmels's avatar
tihmels committed
50

tihmels's avatar
tihmels committed
51
    logging.debug("end of set\n")
Arne Gerdes's avatar
Arne Gerdes committed
52
53
54


for emotion in emotions:
55
56
    detect_faces(emotion)  # Call functional

tihmels's avatar
tihmels committed
57
logging.debug("{} Gesichter in {} Dateien gefunden.".format(totalFaces, totalFiles))
tihmels's avatar
tihmels committed
58
59
logging.debug("In {} Dateien wurde kein Gesicht gefunden:".format(totalFiles - totalFaces))

60
for f in undetected:
tihmels's avatar
tihmels committed
61
    logging.debug(f)