-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathwebcam.py
71 lines (56 loc) · 2.42 KB
/
webcam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
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.
"""
return [nparray_as_image(cv2.imread('graphics/%s.png' % emotion, -1), mode=None) for emotion in emotions]
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:
print("webcam not found")
return
while read_value:
for normalized_face, (x, y, w, h) in find_faces(webcam_image):
prediction = model.predict(normalized_face) # do prediction
if cv2.__version__ != '3.1.0':
prediction = prediction[0]
image_to_draw = emoticons[prediction]
draw_with_alpha(webcam_image, image_to_draw, (x, y, w, h))
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', 'sadness', 'surprise']
emoticons = _load_emoticons(emotions)
# load model
fisher_face = cv2.face.FisherFaceRecognizer_create()
fisher_face.read('models/emotion_detection_model.xml')
# use learnt model
window_name = 'WEBCAM (press ESC to exit)'
show_webcam_and_run(fisher_face, emoticons, window_size=(1600, 1200), window_name=window_name, update_time=8)