A real-time AI-powered driver monitoring system built with Flask, OpenCV, and YOLO that detects various driver behaviors and safety violations to enhance road safety.
- Features
- Demo
- Architecture
- Installation
- Usage
- Project Structure
- Detection Modules
- Alert System
- Configuration
- Models
- Contributing
- License
- ๐๏ธ Blink Detection - Monitors eye aspect ratio (EAR) to detect blinking and drowsiness
- ๐ท Face Mask Detection - Detects whether the driver is wearing a mask
- ๐ฑ Phone Usage Detection - Identifies when the driver is using a mobile phone
- ๐ฌ Smoking Detection - Detects smoking behavior while driving
- โ Hand-on-Mouth Detection - Identifies potential distraction from eating/drinking
- Real-time WhatsApp Alerts - Sends instant notifications via Twilio
- Cloud Image Upload - Captures and uploads violation snapshots to Cloudinary
- Intelligent Cooldown - Prevents alert spam with configurable cooldown periods
- Multi-Behavior Tracking - Monitors multiple safety violations simultaneously
- Live Camera Feed - Real-time video analysis through webcam
- Image Upload - Analyze static images for violations
- Interactive Dashboard - Visual feedback with bounding boxes and confidence scores
- Responsive Design - Bootstrap-based UI that works on all devices
The system provides two modes of operation:
- Live Analysis Mode: Real-time monitoring using your webcam
- Image Upload Mode: Analyze pre-recorded images or screenshots
- Drowsiness alerts when eyes remain closed for extended periods
- Phone detection with hand-phone overlap verification
- Mask compliance monitoring
- Smoking behavior identification
โโโโโโโโโโโโโโโโโโโ
โ Web Browser โ
โ (HTML/JS UI) โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Flask Server โ
โ (app.py) โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โโโโโโดโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโ
โผ โผ โผ โผ โผ
โโโโโโโโโ โโโโโโโโโ โโโโโโโโโ โโโโโโโโโโ โโโโโโโโโโ
โ Blink โ โ Mask โ โ Phone โ โ Smoke โ โ Hand โ
โModule โ โModule โ โModule โ โ Module โ โ Module โ
โโโโโฌโโโโ โโโโโฌโโโโ โโโโโฌโโโโ โโโโโโฌโโโโ โโโโโโฌโโโโ
โ โ โ โ โ
โโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโ
โ Alert System โ
โ - Cloudinary โ
โ - Twilio โ
โโโโโโโโโโโโโโโโโโ
- Python 3.8 or higher
- Webcam (for live detection)
- Internet connection (for alerts)
git clone https://github.com/yourusername/Driver_Monitoring_System.git
cd Driver_Monitoring_System# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtPlace the following model files in the models/ directory:
best.pt- Face mask detection modelyolov8n.pt- YOLO nano model for general object detectionhanddsa.pt- Hand detection modeldetection_module.pt- Smoking detection model (optional)
Edit alert_system.py and add your credentials:
# Cloudinary Configuration
CLOUDINARY_NAME = "your_cloud_name"
CLOUDINARY_KEY = "your_api_key"
CLOUDINARY_SECRET = "your_api_secret"
# Twilio Configuration
TWILIO_SID = "your_account_sid"
TWILIO_TOKEN = "your_auth_token"
TWILIO_FROM_NUMBER = "whatsapp:+14155238886"
# Your WhatsApp Number
YOUR_WHATSAPP_NUMBER = "whatsapp:+1234567890"- Start the Flask Server
python app.py- Access the Web Interface Open your browser and navigate to:
http://localhost:5000
- Choose Detection Mode
- Live Analysis: Click "Start Camera" to begin real-time monitoring
- Image Upload: Upload an image for analysis
# Run on a specific port
python app.py --port 8080
# Run in debug mode
python app.py --debugDriver_Monitoring_System/
โ
โโโ app.py # Main Flask application
โโโ requirements.txt # Python dependencies
โโโ train.ipynb # Model training notebook
โ
โโโ detectors/ # Detection modules
โ โโโ __pycache__/
โ
โโโ models/ # YOLO model files
โ โโโ best.pt # Mask detection model
โ โโโ yolov8n.pt # General YOLO model
โ โโโ handdsa.pt # Hand detection model
โ โโโ detection_module.pt # Smoking detection model
โ
โโโ templates/ # HTML templates
โ โโโ index.html # Main web interface
โ
โโโ alert_system.py # WhatsApp alert integration
โโโ blink_detection.py # Eye blink/drowsiness detection
โโโ hand_detection.py # Hand detection module
โโโ mask_detection.py # Face mask detection
โโโ phone_detection.py # Mobile phone detection
โโโ smoke_detection.py # Smoking detection
โโโ utils.py # Utility functions (IoU, bbox operations)
Uses MediaPipe Face Mesh to calculate Eye Aspect Ratio (EAR):
- EAR Threshold: 0.25 (indicates closed eyes)
- Drowsiness Threshold: 2.0 seconds of continuous eye closure
- Blink Counting: Tracks total blinks per session
Algorithm:
EAR = (|p2 - p6| + |p3 - p5|) / (2 * |p1 - p4|)YOLO-based face mask classification:
- Detects:
with_mask,without_mask,mask_weared_incorrect - Hand-on-mouth overlap detection to avoid false negatives
- Configurable overlap threshold: 40%
Context-aware phone detection:
- Requires hand-phone overlap (5% threshold)
- Fallback to person-phone overlap (2% threshold)
- Minimum phone size filtering to reduce false positives
- Confidence threshold: 30%
Detects smoking behavior:
- YOLO-based cigarette/smoking detection
- Real-time visual feedback with bounding boxes
- Integrated with alert system
Hand position tracking:
- Used for context in phone and mask detection
- Detects hand-on-mouth scenarios
- Improves overall system accuracy
The system uses Twilio and Cloudinary for real-time alerts:
- Automated Snapshots: Captures frame when violation is detected
- Cloud Storage: Uploads images to Cloudinary for permanent storage
- WhatsApp Notifications: Sends instant alerts with image links
- Smart Cooldown: 60-second cooldown between alerts to prevent spam
- ๐จ Drowsiness detected (eyes closed > 2 seconds)
- ๐ฑ Phone usage while driving
- ๐ฌ Smoking behavior
- ๐ท Mask violations (when applicable)
- Create a Cloudinary account
- Create a Twilio account
- Enable Twilio WhatsApp sandbox
- Configure credentials in
alert_system.py
# Detection Thresholds
HAND_ON_MOUTH_THRESHOLD = 0.40 # Hand-mouth overlap ratio
EAR_BLINK_THRESHOLD = 0.25 # Eye closure threshold
DROWSY_SECONDS_THRESHOLD = 2.0 # Drowsiness time limit
# Alert Settings
ALERT_COOLDOWN_SECONDS = 60 # Cooldown between alerts
DETECT_HEAVY_EVERY_N = 3 # Process every Nth frame
# Phone Detection
PHONE_CONF = 0.30 # Phone detection confidence
PHONE_IOU = 0.45 # IoU threshold for NMS| Model | Purpose | Size | Download |
|---|---|---|---|
best.pt |
Face mask detection | ~6 MB | Custom trained |
yolov8n.pt |
General object detection | ~6 MB | Ultralytics |
handdsa.pt |
Hand detection | ~6 MB | Custom trained |
detection_module.pt |
Smoking detection | Variable | Custom trained |
See train.ipynb for training pipeline examples. The notebook includes:
- Data preprocessing
- Model training with YOLOv8
- Validation and testing
- Export for deployment
Contributions are welcome! Here's how you can help:
- 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
- ๐ฏ Improve detection accuracy
- ๐ Add new detection modules
- ๐จ Enhance UI/UX
- ๐ Improve documentation
- ๐งช Add unit tests
- ๐ Add internationalization
This project is licensed under the MIT License - see the LICENSE file for details.
- Ultralytics YOLOv8 - Object detection framework
- MediaPipe - Face mesh detection
- Flask - Web framework
- OpenCV - Computer vision library
- Twilio - WhatsApp API
- Cloudinary - Cloud image storage
- Multi-driver support
- Historical data analytics dashboard
- GPS integration for location tracking
- Voice alerts in addition to WhatsApp
- Mobile app for monitoring
- Advanced fatigue detection using head pose
- Integration with vehicle CAN bus
- Cloud deployment (AWS/GCP/Azure)
- Docker containerization
- API for third-party integrations
โญ If you find this project useful, please consider giving it a star! โญ