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)
评论列表
文章目录