Skip to content

About

A machine learning and deep learning web application that predicts stress levels (Low, Medium, or High) using eight physiological and sleep-related parameters. The application uses Random Forest and Neural Network models with a Flask backend to provide real-time stress predictions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Stress Detection Predictor

Overview

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.


Features

  • 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

Input Parameters

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.


System Workflow

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

Machine Learning Models

1. Random Forest

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

2. Neural Network

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.


Data Preprocessing

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)

Stress Level Mapping

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"
}

Project Structure

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

Technologies Used

Programming Language

  • Python

Machine Learning

  • Scikit-learn
  • Random Forest

Deep Learning

  • TensorFlow
  • Keras
  • Neural Network

Data Processing

  • NumPy
  • Pickle

Web Development

  • Flask
  • HTML
  • CSS
  • JavaScript

Development Tools

  • Visual Studio Code
  • Git
  • GitHub

Installation

1. Clone the Repository

git clone <your-github-repository-url>
cd stress-detection-predictor

2. Create a Virtual Environment

Windows:

python -m venv venv

Activate it:

venv\Scripts\activate

Linux/macOS:

python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

If you do not have a requirements.txt file yet, the main packages required by app.py are:

Flask
numpy
scikit-learn
tensorflow

Running the Application

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.py

Flask will start the development server.

Open the local address displayed in the terminal in your browser.


Prediction API

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"
}

Flask Routes

Home

GET /

Displays the main HTML interface.

Prediction

POST /predict

Receives user input, performs preprocessing, runs both models, and returns the prediction results.


Error Handling

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"
}

Important Model Files

The application depends on three saved files:

Random Forest Model

random_forest_model.pkl

Contains the trained Random Forest model.

Neural Network Model

stress_level_nn_model.h5

Contains the trained TensorFlow/Keras Neural Network.

Scaler

scaler.pkl

Contains the preprocessing scaler fitted during model training.

These files must be compatible with the feature order used by the application.


Feature Order

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.


Future Improvements

  • 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.

Disclaimer

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.


Author

Mugesh

B.Tech – Artificial Intelligence and Data Science

This project demonstrates practical experience with Python, machine learning, deep learning, Flask, and model deployment.

About

A machine learning and deep learning web application that predicts stress levels (Low, Medium, or High) using eight physiological and sleep-related parameters. The application uses Random Forest and Neural Network models with a Flask backend to provide real-time stress predictions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages