webcam.py 3.23 KB
Newer Older
tihmels's avatar
tihmels committed
1
import argparse
2
import os
3 4

import cv2
tihmels's avatar
tihmels committed
5
import cv2.face
Arne Gerdes's avatar
Arne Gerdes committed
6
import numpy as np
7
from RingBuffer import RingBuffer
8
from WebcamVideoStream import WebcamVideoStream
9
from face_detect import extract_faces
10 11
from image_commons import nparray_as_image, draw_with_alpha

tihmels's avatar
tihmels committed
12 13 14
parser = argparse.ArgumentParser(description='ProjectMood Emotion Detection')
parser.add_argument('-b', '--buffer', action='store', dest='buffer', default=12, type=int, help='size of ringbuffer')
parser.add_argument('-m', '--model', action='store', dest='model', default='resources/models/detection_model.xml',
Arne Gerdes's avatar
Arne Gerdes committed
15
                    help='path to model')
tihmels's avatar
tihmels committed
16
arguments = parser.parse_args()
17

18

19 20
def _load_emoticons(emotions):
    """
21 22 23
    Load the emoticons from the emojis folder.
     : param emotions: emotions as an array.
     : return: Array of Emotions graphics.
24
    """
25 26
    return [nparray_as_image(cv2.imread('resources/emojis/{}.png'.format(emotion), -1), mode=None) for emotion in
            emotions]
27 28


tihmels's avatar
tihmels committed
29
def show_webcam_and_run(model, emoticons, window_size=(600, 600), window_name=parser.description, update_time=1):
30
    """
31 32 33 34 35 36
    Shows a webcam image, recognizes faces and emotions in real time and draws emoticons next to the faces.
     : param model: Trained Model
     : param emoticons: list of emoticons.
     : param window_size: Size of the webcam window.
     : param window_name: name of the webcam window.
     : param update_time: Image update time.
37
    """
38
    cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
39 40
    width, height = window_size
    cv2.resizeWindow(window_name, width, height)
41

42
    vc = WebcamVideoStream().start()
43

44
    # a random image from the dataset to determine the image format (important for Fisherface)
tihmels's avatar
tihmels committed
45 46 47
    random = cv2.imread('resources/img_data/dataset/{}/0.jpg'.format(emotions[0]))
    resizefactor = np.size(random, 0)

48
    # The RingBuffer stores the last x Predictions
tihmels's avatar
tihmels committed
49
    buffer = RingBuffer(arguments.buffer)
50

51
    frame = vc.read()
52

53
    while True:
tihmels's avatar
tihmels committed
54
        for normalized_face in extract_faces(frame, resizefactor):
Arne Gerdes's avatar
Arne Gerdes committed
55
            prediction = model.predict(normalized_face)  # do prediction
56

57
            # Save the Predictions
58
            buffer.append(prediction[0])
Arne Gerdes's avatar
Arne Gerdes committed
59

60
            # Get the entries as an array
61
            predictions = buffer.get()
Arne Gerdes's avatar
Arne Gerdes committed
62

63
            # No entry in the ring buffer is None
64
            if not (any(x is None for x in predictions)):
65
                # Counting occurrences of predictions
66
                unique, counts = np.unique(predictions, return_counts=True)
Arne Gerdes's avatar
Arne Gerdes committed
67

68
                # Most frequent value is displayed
Arne Gerdes's avatar
Arne Gerdes committed
69
                image_to_draw = emoticons[unique[0]]
70
                draw_with_alpha(frame, image_to_draw, (40, 40, 200, 200))
71

72 73
        cv2.imshow(window_name, frame)
        frame = vc.read()
74 75
        key = cv2.waitKey(update_time)

76 77
        # exit on ESC
        if key == 27:
78
            vc.stop()
79 80 81 82 83 84
            break

    cv2.destroyWindow(window_name)


if __name__ == '__main__':
85
    # The emotions in the Dataset folder should also be loaded in the application
tihmels's avatar
tihmels committed
86 87
    _, emotions, _ = next(os.walk('resources/img_data/dataset'), (None, [], None))

88 89 90
    emoticons = _load_emoticons(emotions)

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

92
    # Load the trained model
tihmels's avatar
tihmels committed
93
    fisher_face.read(arguments.model)
94
    show_webcam_and_run(fisher_face, emoticons)