An AI-powered application that detects stress levels through real-time facial expression analysis using deep learning. The system leverages a Convolutional Neural Network (CNN) trained on the FER2013 dataset to provide instant stress detection feedback with visual indicators.
- Overview
- Features
- Demo
- Dataset
- Model Architecture
- Installation
- Usage
- Project Structure
- Performance Metrics
- Troubleshooting
- Future Enhancements
- Contributing
- License
The Real-Time Facial Stress Detection System is an intelligent application designed to monitor stress levels through facial expression analysis. Using computer vision and deep learning, the system processes live video feeds, detects faces, analyzes facial features, and provides instant feedback with color-coded visual indicators showing the probability of stress detection.
Key Highlights:
- โ Real-time stress detection from webcam feed
- โ Binary classification: Stress vs. No-Stress
- โ 79% validation accuracy with 0.88 AUC
- โ Temporal smoothing for stable predictions
- โ Privacy-focused offline processing
- โ Suitable for mental health monitoring and workplace wellness
- Real-Time Processing: Analyzes facial expressions frame-by-frame with minimal latency
- Visual Feedback: Color-coded bounding boxes (red for stress, green for calm) with confidence scores
- Temporal Smoothing: Moving average over 10 frames for stable and reliable predictions
- Offline Operation: All processing happens locally, ensuring complete privacy
- Easy Setup: Simple installation with minimal dependencies
- Robust Detection: Uses Haar Cascade for efficient face detection
Stress Detection (71.3%)
The system detects high stress levels with a red bounding box and confidence score of 71.3%
Stress Detection (50.2%)
Marginal stress detection at 50.2% threshold with red indicator
Calm State Detection (3.6%)
The system identifies a calm state with a green bounding box, showing only 3.6% stress probability
The application displays:
- Number of faces detected in the frame
- Real-time stress percentage
- Visual progress bar indicating confidence level
- Color-coded bounding box (Red = Stress, Green = Calm)
The project utilizes the FER2013 (Facial Expression Recognition 2013) dataset, a widely recognized benchmark for facial expression recognition tasks.
Dataset Specifications:
- Total Images: 35,887 grayscale images
- Image Size: 48ร48 pixels
- Original Classes: 7 emotions (angry, disgust, fear, happy, sad, surprise, neutral)
- Binary Mapping:
- Stress (Class 1): Angry, Disgust, Fear, Sad (46.5%)
- No-Stress (Class 0): Happy, Surprise, Neutral (53.5%)
Data Split:
- Training Set: 28,709 images (80%)
- Validation/Test Set: 7,178 images (20%)
- Stratified sampling to maintain class distribution
Acquisition:
The dataset was downloaded from Kaggle using the kagglehub library, providing programmatic access to ensure reproducibility.
The system employs a Convolutional Neural Network (CNN) specifically designed for grayscale facial expression classification.
Architecture Overview:
Input Layer (48ร48ร1)
โ
Conv Block 1: 32 filters (3ร3) โ BatchNorm โ MaxPool โ Dropout(0.3)
โ
Conv Block 2: 64 filters (3ร3) โ BatchNorm โ MaxPool โ Dropout(0.3)
โ
Conv Block 3: 128 filters (3ร3) โ BatchNorm โ MaxPool โ Dropout(0.3)
โ
Flatten Layer
โ
Dense Layer: 128 neurons โ BatchNorm โ Dropout(0.5)
โ
Output Layer: 1 neuron (Sigmoid activation)
Key Features:
- Activation: ReLU activation functions for non-linearity
- Initialization: He uniform weight initialization
- Regularization: Batch normalization and dropout layers to prevent overfitting
- Loss Function: Binary crossentropy
- Optimizer: Adam optimizer with initial learning rate of 0.001
- Learning Rate Scheduling: ReduceLROnPlateau (factor=0.5, patience=3)
- Python 3.8 or higher
- Webcam (for real-time detection)
- Windows, macOS, or Linux
- Clone the Repository
git clone https://github.com/yourusername/stress-detection-system.git
cd stress-detection-system- Create Virtual Environment (Recommended)
Windows:
python -m venv venv
venv\Scripts\activatemacOS/Linux:
python3 -m venv venv
source venv/bin/activate- Install Dependencies
pip install -r requirements.txtrequirements.txt:
tensorflow>=2.20.0
opencv-python>=4.8.0
numpy>=1.24.0
- Download the Trained Model
Download stress_detection_model.h5 from the Releases page and place it in the project root directory.
-
Ensure your webcam is connected and accessible
-
Activate the virtual environment (if not already active)
-
Run the stress detector
python stress_detector.py- Interact with the application:
- The webcam feed will open in a new window
- Face detection and stress analysis happen automatically
- Press 'q' to quit the application
# Use default model path
python stress_detector.py
# Specify custom model path
python stress_detector.py --model_path /path/to/your/model.h5- Green Bounding Box: Calm state (stress probability < 50%)
- Red Bounding Box: Stress detected (stress probability โฅ 50%)
- Percentage: Real-time confidence score
- Progress Bar: Visual representation of stress level
- Faces Detected: Number of faces in the current frame
stress-detection-system/
โ
โโโ stress_detector.py # Main application script
โโโ requirements.txt # Python dependencies
โโโ stress_detection_model.h5 # Trained CNN model (download separately)
โโโ README.md # Project documentation
โโโ .gitignore # Git ignore file
โโโ LICENSE # License information
โ
โโโ screenshots/ # Demo images
โ โโโ stress_detection_71.png
โ โโโ stress_detection_50.png
โ โโโ calm_detection.png
โ
โโโ venv/ # Virtual environment (not in repo)
The model was trained using Google Colab with GPU acceleration over 29 epochs.
Training Results:
- Training Accuracy: 84%
- Validation Accuracy: 79%
- Validation AUC: 0.88
- Training Strategy:
- Class weights applied (stress: 1.3, no-stress: 1.0)
- Early stopping with patience of 7 epochs
- Learning rate reduction on plateau
- Final learning rate: 0.000125
Model Performance:
- Excellent discrimination capability between stress and no-stress states
- Good generalization without significant overfitting
- Balanced performance across both classes
1. Model File Not Found
Error: stress_detection_model.h5 not found
Solution: Verify the model file is in the same directory as stress_detector.py and the filename matches exactly.
2. Webcam Access Issues
Error: Cannot access webcam
Solution:
- Ensure no other application is using the camera
- Check camera permissions in your OS settings
- Try running the script with administrator/sudo privileges
3. Slow Performance
System running slowly or low FPS
Solution:
- Close resource-intensive applications
- Enable GPU acceleration for TensorFlow (if available)
- Reduce temporal smoothing window from 10 to 5 frames in the code
4. TensorFlow Version Conflicts
Error: Module compatibility issues
Solution:
pip install --upgrade tensorflow opencv-python numpy5. Import Errors
Error: No module named 'cv2' or 'tensorflow'
Solution: Ensure virtual environment is activated and dependencies are installed:
pip install -r requirements.txt- Multi-class emotion detection (7 emotions)
- Mobile application development (iOS/Android)
- Historical stress tracking and analytics dashboard
- Integration with wearable devices
- Cloud-based deployment for remote monitoring
- Multi-face detection and tracking
- Audio-based stress detection fusion
- Personalized stress threshold calibration
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please ensure your code follows PEP 8 style guidelines and includes appropriate documentation.
This project is licensed under the MIT License - see the LICENSE file for details.
- FER2013 Dataset: Facial Expression Recognition 2013 dataset from Kaggle
- TensorFlow: For providing robust deep learning framework
- OpenCV: For efficient computer vision operations
- Kaggle: For hosting the FER2013 dataset
For questions, suggestions, or collaboration opportunities:
- GitHub: @AmoghPuthanikar
- Email: apputhanikar04@gmail.com
- LinkedIn: Amogh Puthanikar
If you find this project useful, please consider giving it a star! โญ
Built with โค๏ธ for mental health and wellness applications


