process_model.py 2.99 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1 2 3 4
import cv2
import glob
import random
import numpy as np
tihmels's avatar
tihmels committed
5
import sys
tihmels's avatar
tihmels committed
6
import logging
Arne Gerdes's avatar
Arne Gerdes committed
7

tihmels's avatar
tihmels committed
8
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
tihmels's avatar
tihmels committed
9 10
                    datefmt='%m-%d %H:%M',
                    filename='logs/process_model.log')
11 12

args = sys.argv # liest Input Parameter
tihmels's avatar
tihmels committed
13
logging.debug('Fisherface training initialized')
Arne Gerdes's avatar
Arne Gerdes committed
14

15
file = open("gray.csv", "w")
16 17 18 19 20 21 22 23

def _get_faces_from_emotion(emotion):
    """
    Holt alle Dateien zu einer Emotion aus dem Dataset, mischt sie und teilt sie in ein Trainings- und Prognoseset.
    :param emotion: Die Emotion
    :return: training, prediction
    """
    files = glob.glob('basis_data/dataset/{}/*'.format(emotion))
Arne Gerdes's avatar
Arne Gerdes committed
24 25 26 27 28
    random.shuffle(files)
    training = files[:int(len(files) * 0.8)]  # get first 80% of file list
    prediction = files[-int(len(files) * 0.2):]  # get last 20% of file list
    return training, prediction

29 30 31 32 33 34 35 36
def image_preprocessing(image):
    """
    Preprocessing der Dateien
    :param item: Bild
    :return:
    """
    img = cv2.imread(image)  # open image
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert to grayscale
37 38
    equal = cv2.equalizeHist(gray)
    return equal
Arne Gerdes's avatar
Arne Gerdes committed
39 40 41 42 43 44 45

def make_sets():
    training_data = []
    training_labels = []
    prediction_data = []
    prediction_labels = []
    for emotion in emotions:
46
        training, prediction = _get_faces_from_emotion(emotion)
Arne Gerdes's avatar
Arne Gerdes committed
47 48
        # Append data to training and prediction list, and generate labels 0-7
        for item in training:
tihmels's avatar
tihmels committed
49 50
            img = image_preprocessing(item)
            training_data.append(img)  # append image array to training data list
Arne Gerdes's avatar
Arne Gerdes committed
51 52 53
            training_labels.append(emotions.index(emotion))

        for item in prediction:  # repeat above process for prediction set
tihmels's avatar
tihmels committed
54 55
            img = image_preprocessing(item)
            prediction_data.append(img)
Arne Gerdes's avatar
Arne Gerdes committed
56 57
            prediction_labels.append(emotions.index(emotion))

tihmels's avatar
tihmels committed
58

Arne Gerdes's avatar
Arne Gerdes committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    return training_data, training_labels, prediction_data, prediction_labels

def run_recognizer():
    training_data, training_labels, prediction_data, prediction_labels = make_sets()

    fishface.train(training_data, np.asarray(training_labels))

    cnt = 0
    correct = 0
    incorrect = 0
    for image in prediction_data:
        pred, conf = fishface.predict(image)
        if pred == prediction_labels[cnt]:
            correct += 1
            cnt += 1
        else:
            incorrect += 1
            cnt += 1
    return ((100 * correct) / (correct + incorrect))

tihmels's avatar
tihmels committed
79 80 81
if len(args) > 1:
    tags = ', '.join(args[1:])
    logging.debug(tags.upper())
tihmels's avatar
tihmels committed
82

83
emotions = ["happy", "neutral", "surprise"]  # Emotion list
tihmels's avatar
tihmels committed
84
fishface = cv2.face.FisherFaceRecognizer_create()
Arne Gerdes's avatar
Arne Gerdes committed
85 86 87

# Now run it
metascore = []
tihmels's avatar
tihmels committed
88

89
for i in range(0, 10):
Arne Gerdes's avatar
Arne Gerdes committed
90
    correct = run_recognizer()
91
    file.write("{}\n".format(int(correct)))
tihmels's avatar
tihmels committed
92
    logging.debug("{} : {}%".format(i, int(correct)))
Arne Gerdes's avatar
Arne Gerdes committed
93
    metascore.append(correct)
94 95


96
file.close()
Arne Gerdes's avatar
Arne Gerdes committed
97

tihmels's avatar
tihmels committed
98
logging.debug("{} iterations - {}% average\n".format(len(metascore), np.mean(metascore)))
tihmels's avatar
tihmels committed
99
fishface.write('basis_data/models/detection_model.xml')