webcam.py 2.84 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
"""
Arne Gerdes's avatar
Arne Gerdes committed
2 3
Dieses Modul ist das Main-Modul. Es lädt das Modell aus models, zeigt ein Webcam-Bild,
erkennt das Gesicht und seine Emotionen und zeichnet ein Emoticon in das Bild.
Arne Gerdes's avatar
Arne Gerdes committed
4 5 6 7
"""

import cv2

8
from RingBuffer import RingBuffer
9
from WebcamVideoStream import WebcamVideoStream
Arne Gerdes's avatar
Arne Gerdes committed
10 11
from face_detect import find_faces
from image_commons import nparray_as_image, draw_with_alpha
12
import numpy as np
Arne Gerdes's avatar
Arne Gerdes committed
13 14 15 16


def _load_emoticons(emotions):
    """
Arne Gerdes's avatar
Arne Gerdes committed
17 18 19
     Lädt die Emoticons aus dem graphics Ordner.
    :param  emotions: Array von Emotionen.
    :return: Array von Emotions Grafiken.
Arne Gerdes's avatar
Arne Gerdes committed
20
    """
tihmels's avatar
tihmels committed
21
    return [nparray_as_image(cv2.imread('resources/graphics/%s.png' % emotion, -1), mode=None) for emotion in emotions]
Arne Gerdes's avatar
Arne Gerdes committed
22 23


24
def show_webcam_and_run(model, emoticons, window_size=(600, 600), window_name='Mood Expression', update_time=1):
Arne Gerdes's avatar
Arne Gerdes committed
25
    """
Arne Gerdes's avatar
Arne Gerdes committed
26 27 28 29 30 31
    Zeigt ein Webcam-Bild, erkennt Gesichter und Emotionen in Echtzeit und zeichnet Emoticons neben die Gesichter.
    :param model: Trainiertes Model
    :param emoticons: Liste von Emoticons.
    :param window_size: Grösse des Webcam-Fensters.
    :param window_name: Name des Webcam-Fensters.
    :param update_time: Bildaktualisierungzeit.
Arne Gerdes's avatar
Arne Gerdes committed
32
    """
33
    cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
Arne Gerdes's avatar
Arne Gerdes committed
34 35 36 37
    if window_size:
        width, height = window_size
        cv2.resizeWindow(window_name, width, height)

38
    vc = WebcamVideoStream().start()
39
    frame = vc.read()
Arne Gerdes's avatar
Arne Gerdes committed
40 41 42 43
    """
    Der RingBuffer speichert die letzten 7 Predictions
    """
    puffer = RingBuffer(7)
44

45 46
    while True:
        for normalized_face, (x, y, w, h) in find_faces(frame):
Arne Gerdes's avatar
Arne Gerdes committed
47

Arne Gerdes's avatar
Arne Gerdes committed
48
            prediction = model.predict(normalized_face)  # do prediction
49

Arne Gerdes's avatar
Arne Gerdes committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            """
            Seichert die Predictions
            """
            puffer.append(prediction[0])

            """
            Holt die Einträge als Array 
            """
            preds = puffer.get()

            """
            Kein Eintrag im RingBuffer ist None 
            """
            if not (any(x is None for x in preds)):
                """
                Vorkommen der Predictions zählen 
                """
                unique, counts = np.unique(preds, return_counts=True)

                """
                Häufigster Wert wird dargestellt
                """
                image_to_draw = emoticons[unique[0]]
73
                draw_with_alpha(frame, image_to_draw, (40, 40, 200, 200))
Arne Gerdes's avatar
Arne Gerdes committed
74

75 76
        cv2.imshow(window_name, frame)
        frame = vc.read()
Arne Gerdes's avatar
Arne Gerdes committed
77 78 79
        key = cv2.waitKey(update_time)

        if key == 27:  # exit on ESC
80
            vc.stop()
Arne Gerdes's avatar
Arne Gerdes committed
81 82 83 84 85 86
            break

    cv2.destroyWindow(window_name)


if __name__ == '__main__':
87
    emotions = ['happy', 'neutral', 'surprise']
Arne Gerdes's avatar
Arne Gerdes committed
88 89 90
    emoticons = _load_emoticons(emotions)

    fisher_face = cv2.face.FisherFaceRecognizer_create()
Arne Gerdes's avatar
Arne Gerdes committed
91 92

    """Läadt das trainierte Model"""
tihmels's avatar
tihmels committed
93
    fisher_face.read('basis_data/models/detection_model.xml')
Arne Gerdes's avatar
Arne Gerdes committed
94

tihmels's avatar
tihmels committed
95
show_webcam_and_run(fisher_face, emoticons)