Stress Detection Predictor is a machine learning and deep learning web application that predicts a person's stress level from sleep-related and physiological parameters.
The application uses Flask as the backend and makes predictions using two trained models:
- Random Forest Classifier
- TensorFlow/Keras Neural Network
The input data is transformed using a previously fitted scaler before being passed to the models. The predictions are then mapped to three stress categories:
- Low
- Medium
- High
Disclaimer: This project is intended for educational and research purposes. It is not a medical diagnostic system and should not be used as a substitute for professional medical advice.
- Flask-based web application
- Accepts eight user parameters
- Input preprocessing using a saved scaler
- Random Forest stress prediction
- Neural Network stress prediction
- Returns predictions as JSON
- Supports comparison of predictions from two different models
- Simple HTML frontend through Flask templates
The application accepts the following eight parameters:
| Parameter | Form Field | Description |
|---|---|---|
| Snoring Rate | snoring_rate |
Measured snoring-related value |
| Respiration Rate | respiration_rate |
Respiration/breathing rate |
| Body Temperature | body_temperature |
Body temperature measurement |
| Limb Movement | limb_movement |
Limb movement measurement |
| Blood Oxygen | blood_oxygen |
Blood oxygen/SpO2-related value |
| Eye Movement | eye_movement |
Eye movement measurement |
| Sleeping Hours | sleeping_hours |
Number of hours slept |
| Heart Rate | heart_rate |
Heart rate measurement |
The exact units and acceptable ranges should match the dataset used to train the models.
User Input
|
v
Flask Web Application
|
v
Read 8 Parameters
|
v
NumPy Array
|
v
Saved Scaler
|
+-----------------------+
| |
v v
Random Forest Neural Network
| |
+-----------+-----------+
|
v
Stress Classification
|
+--------+--------+
| | |
Low Medium High
The application loads the trained Random Forest model from:
random_forest_model.pkl
The model predicts one of the three numerical classes:
0 → Low
1 → Medium
2 → High
The TensorFlow/Keras model is loaded from:
stress_level_nn_model.h5
The Neural Network produces class probabilities, and argmax is used to select the class with the highest predicted probability.
The same scaler used during model training is loaded from:
scaler.pkl
Before prediction, the eight input values are converted into a NumPy array and transformed using the saved scaler.
This is important because the models must receive input data in the same feature scale used during training.
input_array = np.array([input_data])
input_data_scaled = scaler.transform(input_array)The model output is converted into readable labels using:
stress_levels = {
0: "Low",
1: "Medium",
2: "High"
}The application returns predictions from both models.
Example response:
{
"Random Forest Prediction": "Medium",
"Neural Network Prediction": "High"
}stress-detection-predictor/
│
├── app.py
│
├── random_forest_model.pkl
├── stress_level_nn_model.h5
├── scaler.pkl
│
├── templates/
│ └── index.html
│
├── static/
│ ├── style.css
│ └── script.js
│
├── requirements.txt
├── README.md
└── .gitignore
- Python
- Scikit-learn
- Random Forest
- TensorFlow
- Keras
- Neural Network
- NumPy
- Pickle
- Flask
- HTML
- CSS
- JavaScript
- Visual Studio Code
- Git
- GitHub
git clone <your-github-repository-url>
cd stress-detection-predictorWindows:
python -m venv venvActivate it:
venv\Scripts\activateLinux/macOS:
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtIf you do not have a requirements.txt file yet, the main packages required by app.py are:
Flask
numpy
scikit-learn
tensorflow
Make sure the following files are present in the project directory:
app.py
random_forest_model.pkl
stress_level_nn_model.h5
scaler.pkl
templates/index.html
Then run:
python app.pyFlask will start the development server.
Open the local address displayed in the terminal in your browser.
The application provides the following endpoint:
POST /predict
The endpoint expects form data containing:
snoring_rate
respiration_rate
body_temperature
limb_movement
blood_oxygen
eye_movement
sleeping_hours
heart_rate
The endpoint returns JSON containing predictions from both models.
Example:
{
"Random Forest Prediction": "Low",
"Neural Network Prediction": "Low"
}GET /
Displays the main HTML interface.
POST /predict
Receives user input, performs preprocessing, runs both models, and returns the prediction results.
The prediction route is wrapped in a try/except block. If an invalid input, missing form field, model-loading issue, or other runtime error occurs, the application returns an error response in JSON format.
Example:
{
"error": "error message"
}The application depends on three saved files:
random_forest_model.pkl
Contains the trained Random Forest model.
stress_level_nn_model.h5
Contains the trained TensorFlow/Keras Neural Network.
scaler.pkl
Contains the preprocessing scaler fitted during model training.
These files must be compatible with the feature order used by the application.
The order of the input features is important. The application sends the features to the scaler and models in this exact order:
1. snoring_rate
2. respiration_rate
3. body_temperature
4. limb_movement
5. blood_oxygen
6. eye_movement
7. sleeping_hours
8. heart_rate
Changing this order without retraining or updating the preprocessing pipeline can produce incorrect predictions.
- Add model accuracy and evaluation results to the application.
- Display prediction probabilities.
- Add data visualization for input parameters.
- Add prediction history.
- Add user authentication.
- Deploy the application using a production WSGI server.
- Add SHAP-based model explainability.
- Compare model performance using accuracy, precision, recall, and F1-score.
- Add automated input validation and parameter range checks.
- Build a responsive frontend dashboard.
This application is an educational machine learning project. Stress prediction from physiological or sleep-related parameters can be affected by many factors, and model predictions should not be interpreted as a medical diagnosis.
Mugesh
B.Tech – Artificial Intelligence and Data Science
This project demonstrates practical experience with Python, machine learning, deep learning, Flask, and model deployment.