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.
在学习了ML的所有基础知识(包括回归,分类和卷积神经网络)之后,我开始了一些基本的Dataset问题。 。 在这一点上,我有机会从事主要的ML项目-Stress Detector API。
This project has three major parts:
该项目包括三个主要部分:
- Training the dataset using Convolutional Neural Networks 使用卷积神经网络训练数据集
- Testing the model using live stream 使用实时流测试模型
- Deploying the model using a Flask API 使用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.
所使用的数据集是Kaggle Fer-2013开源数据集,该数据集共有33,887张灰度图像,分为7类情感。 这7种情绪是-快乐,愤怒,悲伤,厌恶,惊奇,恐惧和中立。 为了训练该数据集,我使用了5个卷积层和激活层的组合。 这些层是使用顺序模型实现的,包含7个块,每个块分别具有激活,规范化和展平层。
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.
整个训练过程以100个时期进行,批次大小为64,耐心值为10,以便尽早停止和检查站。 用于编译模型的编译器是Adam,学习速率为0.001。 该模型以67.5%的准确度进行了训练,且拟合程度最小。 该模型已保存在JSON文件中。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/44d4371db469a4177428729370e01b0d.png)
![Image for post](https://miro.medium.com/max/9999/1*nKBmW84lTcGo17zlr-X0kg.png)
测试模型 (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.
该模型将从设备的网络摄像机的视频馈送中找到实时压力值,并实施此OpenCV。 使用OpenCVVideoCapture功能,将捕获实时视频源的每一帧。 为了识别人的面部特征,使用具有68标志的面部特征文件。 该程序每一秒钟都会识别出脸,眉毛和嘴唇的形状。 根据规范化公式计算出眉毛和嘴唇的凸起形状,以确定压力水平。 随着嘴唇和眉毛运动的变化,压力水平也会变化。 该程序计算是否通过眉毛和嘴唇运动的累加值找到总应力值,以及它是“高应力”还是“低应力”。 这些计算值显示在与使用OpenCV PutText功能捕获视频相同的帧上。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/15445eda33dc0aa04a8f2764dc16c582.png)
在Flask API中部署模型 (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.
我使用Flask开发了一种基本的API,该API将从测试文件(带有逐帧计算的文件)中调用VideoCamera类,并显示视频以及压力等级。
![Image for post](https://miro.medium.com/max/9999/1*i5QKZQgHB_YNIYs6Zvss7Q.png)
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/c25f3f56843e343a40f7fe67b2db535c.png)
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.
这就是我设法使用Python和Flask制作实时压力检测器API的方式。 整个过程每个步骤都充满了很多学习知识,并增加了我对机器学习和深度学习的热情。
If you found my project interesting please do look into my GitHub repository for the same and leave a Star. Thank you!
如果您发现我的项目很有趣,请查看我的GitHub存储库,并留下一个Star。 谢谢!
GitHub Repository:HarshiniR4
GitHub存储库: HarshiniR4
翻译自: https://medium.com/@harshiniraju4/stress-detector-api-using-python-and-flask-5f54c04c65e3