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

import os

10
from email_service import sendMail
tihmels's avatar
tihmels committed
11
from face_detect import locate_faces
Arne Gerdes's avatar
Arne Gerdes committed
12

13
14
15
logfile = 'logs/sorted_set_facedetector.log'

logging.basicConfig(level=logging.NOTSET, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
16
                    datefmt='%m-%d %H:%M',
17
                    filename=logfile,
18
                    filemode='w')
Arne Gerdes's avatar
Arne Gerdes committed
19
"""
20
Argument Parser erlaubt Parameter für die Verarbeitung anzugeben.
Arne Gerdes's avatar
Arne Gerdes committed
21
"""
22
23

parser = argparse.ArgumentParser(description='Sorted Set Face Creator Application')
24
25
26
27
parser.add_argument('--source', action='store', dest='img_source', default='resources/img_data/sorted_set/',
                    help='path to image source')
parser.add_argument('--dataset', action='store', dest='dataset', default='resources/img_data/dataset/', help='path to dataset')
parser.add_argument('--r', action='store', dest='resize', default=150, type=int, help='resize factor')
28
parser.add_argument('-e', action='append', dest='emotions', default=['happy', 'neutral', 'surprise'], help='declare emotions that should be processed')
29
30
31
32
parser.add_argument('-c', action='store', dest='scaleFactor', default=1.1, type=float,
                    help='scale factor - haar')
parser.add_argument('-n', action='store', dest='minNeighbors', default=6, type=int, help='min neighbors - haar')
parser.add_argument('-s', action='store', dest='minSize', default=40, type=int, help='min size - haar')
33
parser.add_argument('--email', action='store_true', help='activate email notifications')
34
arguments = parser.parse_args()
35
36
37
38
39
40
41
42
43
44
logging.debug(arguments)

source_path = arguments.img_source
dataset_path = arguments.dataset
resizeFactor = arguments.resize
emotions = arguments.emotions
scaleFactor = arguments.scaleFactor
minNeighbors = arguments.minNeighbors
minSize = arguments.minSize
email = arguments.email
45
46


47
if len(glob.glob(dataset_path + '*')) > 0:
48
    deleteDataset = input(
49
        'Im Dataset befinden sich Dateien. Durch diesen Vorgang werden die existierenden Daten gelöscht. Fortfahren (y/n): ')
50
51

    if deleteDataset == 'y':
52
        for file in glob.glob(dataset_path + '*'):
53
54
55
            shutil.rmtree(file)
    else:
        sys.exit()
Arne Gerdes's avatar
Arne Gerdes committed
56

tihmels's avatar
tihmels committed
57
58
59
60
totalFiles: int = 0
totalFaces: int = 0
undetected: list = []

Arne Gerdes's avatar
Arne Gerdes committed
61

Arne Gerdes's avatar
Arne Gerdes committed
62
def detect_faces(emotion):
Arne Gerdes's avatar
Arne Gerdes committed
63
64
65
    """
    Holt alle Dateien zu einer Emotion aus dem sorted_set
    """
66
    files = glob.glob(source_path + '{}/*'.format(emotion))
67
68
69
70

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

72
    logging.info("Found {} {} files".format(len(files), emotion))
73
74

    fileNumber = 0
Arne Gerdes's avatar
Arne Gerdes committed
75
76
    for f in files:
        frame = cv2.imread(f)  # Open image
77
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # Convert image to grayscale
Arne Gerdes's avatar
Arne Gerdes committed
78

79
        facefeatures = locate_faces(gray, scaleFactor, minNeighbors, (minSize, minSize))
tihmels's avatar
tihmels committed
80
81

        if facefeatures is '':
82
            logging.info('No face detected ' + f)
83
            undetected.append(f)
84
85
            if len(undetected) % 200 == 0 and email:
                sendMail('Already ' + str(len(undetected)) + ' not detected faces', filepath=logfile)
86
87
88
89
90
91
        else:
            # Cut and save face
            for (x, y, w, h) in facefeatures:  # get coordinates and size of rectangle containing face
                totalFaces += 1
                gray = gray[y:y + h, x:x + w]  # Cut the frame to size
                try:
92
93
                    out = cv2.resize(gray, (resizeFactor, resizeFactor))  # Resize face so all images have same size
                    success = cv2.imwrite(dataset_path + '{}/{}.jpg'.format(emotion, fileNumber), out)  # Write image
94
                    if not success:
95
96
                        logging.error('Problem while writing file ' + f + ' occurred...' )
                        if email:
97
                            sendMail('Problem while writing file', body=f + ' to ' + datasetPath + '{}/{}.jpg'.format(emotion, fileNumber))
98
                except:
99
                    logging.error('Some error with ' + f)
100
                    if email:
101
                        sendMail('Problem while writing file', body=f)
102
                    pass  # If error, pass file
103

tihmels's avatar
tihmels committed
104
        totalFiles += 1  # Increment image number
105
        fileNumber += 1
tihmels's avatar
tihmels committed
106

107
if email:
108
    sendMail('Facedetector started notification')
109

110
for emotion in emotions:
Arne Gerdes's avatar
Arne Gerdes committed
111

112
113
    if not os.path.exists(dataset_path + emotion):
        os.makedirs(dataset_path + emotion)
Arne Gerdes's avatar
Arne Gerdes committed
114

115
116
    detect_faces(emotion)  # Call functional

117
118
logging.info('{} faces in {} files found'.format(totalFaces, totalFiles))
logging.info('In {} files no face could be detected'.format(totalFiles - totalFaces))
tihmels's avatar
tihmels committed
119

120
for f in undetected:
121
122
    logging.info(f)

123
124
if email:
    sendMail('Facedetector finished notification', filepath=logfile)