Stress Detector API Using Python and Flask

Harshini Raju
The Startup
Published in
3 min readAug 26, 2020

I’ve always had a flair for mathematics and was looking for every opportunity to implement it in all my activities, especially in coding. Machine Learning seemed like the perfect solution to combine coding with mathematical concepts. I had some hesitations too, as it all felt too complicated for a mere first-year college student, but I’m glad I decided to get in with an open mind.

After learning all basics of ML- including regression, classification and convolutional neural networks, I began with some basic Dataset problems. . At this point, I was given the opportunity to work on a major ML project- Stress Detector API.

This project has three major parts:

  1. Training the dataset using Convolutional Neural Networks
  2. Testing the model using live stream
  3. Deploying the model using a Flask API

Training the Model

The dataset used is the Kaggle Fer-2013 open-source dataset that has a total of 33,887 grayscale images classified into 7 classes of emotions. the 7 emotions are — Happy, Angry, Sad, Disgust, Surprise, Fear and Neutral. To train this dataset I used 5 convolutional layers with a combination of activation layers. These layers were implemented using Sequential model and contained 7 blocks each with respective Activation, Normalisation and Flattening layers.

The entire training process ran in 100 epochs with a batch size of 64 and a patience value of 10 for early stopping and checkpoint. The compiler used to compile the model is Adam with a learning rate of 0.001. The model trained with an accuracy of 67.5% with minimal overfitting. The model was saved in a JSON file.

Training the model with sequential layers and 100 epochs

Testing the model

This model will find live stress values from the video feed from the web camera of the device and to implement this OpenCV was utilised. Using the VideoCapture feature of OpenCV, every frame of the live video feed is taken. To identify the facial features of the person, the 68-landmark facial features file is used. For every second the program identifies the face, eyebrows and lip shape. The convex shape of the eyebrows and lip is calculated against a normalisation formula to determine the stress levels. As the lip and eyebrow movement change the stress levels also change. The program calculates whether the cumulative value from the eyebrow and lip movement to find total stress value and whether it is ‘High Stress’ or ‘Low Stress’. These calculated values are displayed on the same frame as the video is captured using the OpenCV PutText feature.

Stress Level calculation formula

Deploying the model in Flask API

I developed a basic API using Flask which will call the VideoCamera class from the test file (the one with the frame by frame calculations) and display the video along with the Stress Levels.

Screenshot for High Stress
Screenshot for Low Stress

This is how I managed to make a live Stress Detector API using Python and Flask. The whole process was filled with so much learning at each step and increased my passion for Machine Learning and Deep Learning.

If you found my project interesting please do look into my GitHub repository for the same and leave a Star. Thank you!

GitHub Repository:HarshiniR4

Reference: https://github.com/Geek-ubaid/Stress-Detection

--

--

Harshini Raju
The Startup

I’m a computer science student interested in Data Science and IoT. I also have a passion for writing and am a budding content writer.