webcam.py 2.44 KB
Newer Older
Arne Gerdes's avatar
Arne Gerdes committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""
This module is the main module in this package. It loads emotion recognition model from a file,
shows a webcam image, recognizes face and it's emotion and draw emotion on the image.
"""


from cv2 import WINDOW_NORMAL

import cv2


from face_detect import find_faces
from image_commons import nparray_as_image, draw_with_alpha


def _load_emoticons(emotions):
    """
    Loads emotions images from graphics folder.
    :param emotions: Array of emotions names.
    :return: Array of emotions graphics.
    """
tihmels's avatar
tihmels committed
22
    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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


def show_webcam_and_run(model, emoticons, window_size=None, window_name='webcam', update_time=10):
    """
    Shows webcam image, detects faces and its emotions in real time and draw emoticons over those faces.
    :param model: Learnt emotion detection model.
    :param emoticons: List of emotions images.
    :param window_size: Size of webcam image window.
    :param window_name: Name of webcam image window.
    :param update_time: Image update time interval.
    """
    cv2.namedWindow(window_name, WINDOW_NORMAL)
    if window_size:
        width, height = window_size
        cv2.resizeWindow(window_name, width, height)

    vc = cv2.VideoCapture(0)
    if vc.isOpened():
        read_value, webcam_image = vc.read()
    else:
tihmels's avatar
tihmels committed
43
        print("Webcam nicht gefunden")
Arne Gerdes's avatar
Arne Gerdes committed
44
45
46
47
48
49
50
        return

    while read_value:
        for normalized_face, (x, y, w, h) in find_faces(webcam_image):
            prediction = model.predict(normalized_face)  # do prediction

            image_to_draw = emoticons[(prediction[0])]
tihmels's avatar
tihmels committed
51
52
            if x - 150 > 0 and y - 50 > 0 and w - 150 > 0 and h - 150 > 0:
                draw_with_alpha(webcam_image, image_to_draw, (x-150, y-50, w-150, h-150))
Arne Gerdes's avatar
Arne Gerdes committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

        cv2.imshow(window_name, webcam_image)
        read_value, webcam_image = vc.read()
        key = cv2.waitKey(update_time)

        if key == 27:  # exit on ESC
            break

    cv2.destroyWindow(window_name)


if __name__ == '__main__':
    emotions = ['neutral', 'anger', 'disgust', 'happy', 'surprise']
    emoticons = _load_emoticons(emotions)


    # load mode
    fisher_face = cv2.face.FisherFaceRecognizer_create()
tihmels's avatar
tihmels committed
71
    fisher_face.read('basis_data/models/detection_model.xml')
Arne Gerdes's avatar
Arne Gerdes committed
72
73
    # use learnt model
    window_name = 'WEBCAM (press ESC to exit)'
tihmels's avatar
tihmels committed
74
show_webcam_and_run(fisher_face, emoticons, window_size=(800, 800), window_name=window_name, update_time=1)