Skip to content
Advertisement

Synchronization for video/audio/text message in flask web app framework for facial emotion recognition

I have trained a CNN model in Google Colab for facial expression detection with the FER2013 dataset containing 7 emotion classes (‘Angry’, ‘Disgust’, ‘Fear’, ‘Happy’, ‘Sad’, ‘Surprise’, ‘Neutral’).

Used flask framework to build a web application. OpenCV’s haarcascade_frontalface_default.xml is used to detect faces. With this I’m able to do real-time live streaming of the video using my laptop’s webcam and detect the facial expressions.

But when added the audio corresponding to the expressions together with the text message the live video stream is lagging (getting stuck in between) and video/audio are not in sync. How to make them in sync or how to deliver the text and audio corresponding to the expression every 3 seconds (while the video stream is going on take frames every 3 seconds and deliver the audio message corresponding to that particular frame together with the text message)?

Any help would be much appreciated, thanks in advance.

I referred the below link for app.py and index.html for webapp creation.

https://levelup.gitconnected.com/how-to-build-a-real-time-emotion-detection-web-app-ce7e3ed7b7de

This is having the text messages corresponding to the facial expressions(frame by frame), but I want the audio as well(eg: ‘happy.mp3‘ will play for happy face expression). Audio files which I used have a size between 2-4 kb

my python file : app.py

JavaScript

My html file in templates folder: index.html

JavaScript

Advertisement

Answer

This is because the playsound execution is synchronous.

Before going to the next frame detection, playsound must be executed completely, introducing the lag you experience.

There is the block flag in playsound:

JavaScript

which works on Linux but not in Windows, I’ve tested myself.

If this is your case I would highly suggest to use separate threads/processes for the video processing and audio playback.

Something like this:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement