A machine learning-based stress detection system that analyzes voice patterns to identify stress levels in audio recordings. The system uses acoustic feature extraction and RandomForest classification to predict stress from voice signals.
- Voice Pattern Analysis: Extracts 21 acoustic features from audio recordings
- Real-time Prediction: Fast API-based backend for stress detection
- Interactive Frontend: Modern React interface with:
- File upload support
- Voice recording capability
- Real-time waveform visualization
- Pre-loaded test samples
- Multi-format Support: Accepts .wav, .m4a, .mp3, and .webm audio files
- Dataset Integration: Uses CREMA-D Dataset for training
- Installation
- Quick Start
- Project Structure
- API Documentation
- Dataset Information
- Usage
- Features
- Technology Stack
- Python 3.12+
- Node.js 16+ and npm
- ffmpeg (optional, for .m4a file support)
- Navigate to the backend directory:
cd backend- Create and activate virtual environment:
python -m venv venv
# Windows
.\venv\Scripts\Activate.ps1
# Linux/Mac
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Prepare the dataset:
python prepare_crema_d.py- Train the model:
python train_model.py- Start the API server:
uvicorn main:app --reloadThe API will be available at http://localhost:8000
- Navigate to the frontend directory:
cd frontend- Install dependencies:
npm install- Start the development server:
npm run devThe frontend will be available at http://localhost:5173
-
Start Backend:
cd backend .\venv\Scripts\Activate.ps1 # Windows uvicorn main:app --reload
-
Start Frontend (in a new terminal):
cd frontend npm run dev -
Open Browser: Navigate to
http://localhost:5173 -
Test the System:
- Upload an audio file (.wav, .m4a, or .mp3)
- Record your voice using the Record tab
- Try pre-loaded test samples
stress_detection/
βββ backend/
β βββ main.py # FastAPI server
β βββ feature_extraction.py # Feature extraction functions
β βββ audio_utils.py # Audio processing utilities
β βββ train_model.py # Model training script
β βββ prepare_crema_d.py # CREMA-D dataset preparation script
β βββ requirements.txt # Python dependencies
β βββ model.pkl # Trained model (generated)
β βββ dataset/ # Prepared dataset (generated)
β β
βββ CREMA-D-download/ # CREMA-D dataset (in project root)
β βββ AudioWAV/ # Audio recordings (7,442 files)
β βββ docs/ # Dataset documentation
β
βββ frontend/
β βββ src/
β β βββ App.jsx # Main React component
β β βββ App.css # Styles
β β βββ api.js # API client
β β βββ AudioVisualizer.jsx # Waveform visualization
β β βββ main.jsx # Entry point
β βββ public/
β β βββ test-samples/ # Test audio files
β βββ package.json
β βββ vite.config.js
β
βββ API_DOCUMENTATION.md # Complete API documentation
βββ DATASET.md # Dataset information
βββ SYSTEM_DESIGN.md # System design document
βββ README.md # This file
- API Documentation: Complete API reference, endpoints, and data flow
- Dataset Documentation: Dataset structure, files, and usage information
- System Design: System architecture and design decisions
- Click on the Upload tab
- Drag and drop an audio file or click to browse
- View the waveform visualization
- Click Analyze Stress to get prediction
- Click on the Record tab
- Click Start Recording
- Speak into your microphone
- Click Stop Recording when done
- Click Analyze Stress to get prediction
- Click on the Test Samples tab
- Select a pre-loaded sample
- View automatic prediction and waveform
The system extracts 21 acoustic features from audio:
- Pitch Features (2): Mean and standard deviation
- Jitter (1): Pitch instability
- Shimmer (1): Amplitude instability
- Energy (1): RMS energy
- MFCC (13): Mel-frequency cepstral coefficients
- Spectral Features (3): Centroid, bandwidth, rolloff
See API_DOCUMENTATION.md for detailed feature descriptions.
- FastAPI: Web framework
- scikit-learn: Machine learning (RandomForestClassifier)
- librosa: Audio processing and feature extraction
- numpy: Numerical computations
- pandas: Data manipulation
- React: UI framework
- Vite: Build tool and dev server
- Axios: HTTP client
- Web Audio API: Audio recording and visualization
The system uses the CREMA-D (Crowd-sourced Emotional Multimodal Actors Dataset):
- 7,442 audio recordings (.wav format)
- 91 actors (48 male, 43 female, ages 20-74)
- 6 emotions mapped to binary stress labels
- Emotion codes embedded in filenames
- High-quality professional recordings
Label Mapping:
- Stressed: Anger, Disgust, Fear, Sad
- Not Stressed: Happy, Neutral
For detailed dataset information, see DATASET.md.
GET http://localhost:8000/
POST http://localhost:8000/predict
Content-Type: multipart/form-data
Body: audio file (.wav, .m4a, .mp3, or .webm)
Response:
{
"label": "stressed",
"confidence": 85.42
}See API_DOCUMENTATION.md for complete API reference.
To train the model:
-
Prepare the dataset (organize CREMA-D files):
cd backend python prepare_crema_d.py -
Train the model:
python train_model.py
The trained model will be saved as backend/model.pkl and training results in backend/training_result.txt.
- The model is trained on CREMA-D dataset (7,442 samples) for robust performance
- Model Performance: 92.15% accuracy, 95.11% F1-score, 96.34% precision for stressed detection
- Model Configuration: 500 trees, max_depth=30, GridSearchCV optimized hyperparameters
- Class imbalance is handled automatically with class_weight='balanced'
- All audio files are in .wav format (standardized)
- The system supports real-time recording via browser microphone
- Training results are automatically saved to
training_result.txt - Frontend includes 8 test samples from CREMA-D dataset
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
The CREMA-D dataset is licensed under the Open Database License (ODbL) 1.0. Please refer to the dataset documentation for usage terms: http://opendatacommons.org/licenses/odbl/1.0/
- CREMA-D Dataset (Cao et al., 2014) for providing the emotional speech dataset
- librosa for audio processing capabilities
- FastAPI and React communities
For questions or issues, please open an issue on the repository.
Built with β€οΈ for stress detection research