A real-time AI-powered system to detect driver drowsiness and prevent accidents
Features โข Installation โข Usage โข Demo โข Technology
DriveSafe AI is an intelligent driver drowsiness detection system that uses computer vision and machine learning to monitor drivers in real-time. By analyzing facial features, eye movements, and yawning patterns, the system can detect early signs of fatigue and alert drivers before accidents occur.
- 1.35 million people die in road accidents globally each year
- 20% of fatal crashes are caused by drowsy driving
- Drivers are often unaware of early drowsiness signs
Real-time monitoring using:
- Eye Aspect Ratio (EAR) to detect eye closure
- Mouth Aspect Ratio (MAR) to detect yawning
- MediaPipe Face Mesh for precise facial landmark detection
- Instant alerts with audio and visual warnings
- โ Real-time face detection using MediaPipe Face Mesh (468 landmarks)
- โ Eye tracking with EAR calculation to detect prolonged eye closure
- โ Blink detection to identify excessive blinking patterns
- โ Yawn detection using mouth aspect ratio analysis
- โ Drowsiness scoring algorithm (0-100 scale)
- โ Multi-level alerts (Normal/Warning/Alert)
- ๐น Live video feed with facial landmark overlay
- ๐ Real-time statistics (blink count, yawn count, EAR/MAR values)
- ๐ Interactive Chart.js graph showing drowsiness timeline
- ๐จ Premium dark mode UI with glassmorphism effects
- โก Responsive design for all screen sizes
- ๐ Audio alerts when drowsiness detected
- ๐จ Visual alerts with red screen flash
- โก 15+ FPS real-time processing
- ๐ป Offline capability - no cloud dependency
- ๐ฏ High accuracy facial landmark detection
- ๐ Auto pause/resume when tab hidden
- ๐ฑ Responsive across devices
- Python 3.8+ - Core language
- Flask - Web framework
- OpenCV - Computer vision
- MediaPipe - Face mesh detection
- NumPy - Numerical computing
- SciPy - Distance calculations
- HTML5 - Structure
- CSS3 - Premium dark mode styling
- JavaScript (ES6+) - Interactivity
- Chart.js - Real-time visualization
- Python 3.8 or higher
- Webcam (built-in or external)
- Modern web browser (Chrome, Firefox, Edge)
- Windows/Mac/Linux operating system
cd "AI-Based Driver Drowsiness Detection"Windows:
python -m venv venv
venv\Scripts\activateMac/Linux:
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtThis will install:
- Flask & Flask-CORS
- OpenCV
- MediaPipe
- NumPy
- SciPy
python -c "import cv2, mediapipe; print('โ
All dependencies installed successfully!')"-
Navigate to backend directory:
cd backend -
Run the Flask server:
python app.py
-
Open your browser:
- Navigate to:
http://127.0.0.1:5000 - Allow webcam permissions when prompted
- Navigate to:
-
Start monitoring:
- The dashboard will load automatically
- Live video feed will appear on the left
- Statistics and graph on the right
- Press
Ctrl+Cin the terminal to stop the server - The camera will be released automatically
Perfect for hackathon presentations:
- Show the professional dashboard
- Explain the problem (drowsy driving statistics)
- Introduce DriveSafe AI
- Demonstrate live webcam feed with face mesh
- Point out real-time EAR and MAR values
- Show the drowsiness graph updating
- Close eyes for 3+ seconds
- Alert triggers! ๐จ
- Audio beep plays
- Screen flashes red
- Status changes to "ALERT"
- Graph spikes above threshold
- Emphasize instant response time
- Explain EAR algorithm briefly
- Mention MediaPipe's 468 facial landmarks
- Highlight offline capability
- "This system can save lives"
- Mention real-world applications
- Discuss future enhancements
Total Demo: ~2 minutes โฑ๏ธ
EAR = (||p2-p6|| + ||p3-p5||) / (2 * ||p1-p4||)
- EAR > 0.25 โ Eyes open
- EAR < 0.25 โ Eyes closed
- Prolonged closure (>2 seconds) โ Drowsiness detected
MAR = (vertical distance) / (horizontal distance)
- MAR > 0.6 โ Yawning detected
- Frequent yawning indicates fatigue
score = 0
if eye_closed_duration > 2_seconds: score += 40
if blink_rate_excessive: score += 20
if yawn_detected: score += 30
if no_face_detected: score += 10
# Final score: 0-100Alert Levels:
- ๐ข 0-30: Normal
- ๐ก 31-69: Warning
- ๐ด 70-100: Alert (triggers audio/visual alarm)
- Live webcam stream with face mesh overlay
- Real-time EAR and MAR display
- FPS counter
- Status badge (Normal/Warning/Alert)
- Current Status: Visual indicator with emoji
- Drowsiness Meter: Progress bar (0-100%)
- Live Metrics:
- Eye blink count ๐๏ธ
- Yawn count ๐ฅฑ
- EAR value โก
- MAR value ๐ฎ
- Alert History: Total alerts and last alert time
- Rolling 60-second drowsiness graph
- Color-coded line (green/yellow/red)
- Alert threshold line at 70%
- Interactive tooltips
Edit backend/utils/eye_tracker.py:
self.EAR_THRESHOLD = 0.25 # Lower = more sensitive
self.CONSECUTIVE_FRAMES = 20 # Higher = less sensitiveEdit backend/utils/yawn_detector.py:
self.MAR_THRESHOLD = 0.6 # Lower = more sensitive
self.CONSECUTIVE_FRAMES = 15 # Higher = less sensitiveEdit backend/utils/alert_manager.py:
self.alert_cooldown = 5 # seconds between alerts- Start the application
- Look at the camera normally
- Verify status shows "Normal" (green)
- Check EAR value is around 0.25-0.30
- Close your eyes for 3+ seconds
- Verify:
- โ Status changes to "Alert" (red)
- โ Audio beep plays
- โ Screen flashes red
- โ Drowsiness score jumps to 70+
- โ Graph shows spike
- Simulate a yawn (wide mouth opening)
- Verify:
- โ MAR value increases above 0.6
- โ Yawn count increments
- โ Drowsiness score increases
AI-Based Driver Drowsiness Detection/
โโโ backend/
โ โโโ app.py # Flask application
โ โโโ drowsiness_detector.py # Core detection engine
โ โโโ utils/
โ โโโ __init__.py
โ โโโ eye_tracker.py # EAR calculation
โ โโโ yawn_detector.py # MAR calculation
โ โโโ alert_manager.py # Alert logic
โโโ frontend/
โ โโโ index.html # Dashboard
โ โโโ css/
โ โ โโโ style.css # Dark mode styling
โ โโโ js/
โ โ โโโ main.js # Core JavaScript
โ โ โโโ chart-config.js # Chart.js setup
โ โโโ assets/
โ โโโ alert-sound.mp3 # Audio alert
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
- SQLite session logging
- Export session data to CSV
- Head pose detection (tilting/nodding)
- Multiple camera support
- Custom alert sounds
- SMS/Email alerts to emergency contacts
- Mobile app (React Native)
- Cloud dashboard for fleet management
- Historical analytics and reports
- Multi-language support
- Integration with car systems (OBD-II)
- Steering pattern analysis
- Heart rate monitoring (with wearables)
- AI model fine-tuning with custom dataset
- Edge device deployment (Raspberry Pi, Jetson)
# Check available cameras
import cv2
for i in range(5):
cap = cv2.VideoCapture(i)
if cap.isOpened():
print(f"Camera {i} available")
cap.release()- Close other applications using the webcam
- Reduce video resolution in
app.py:camera.set(cv2.CAP_PROP_FRAME_WIDTH, 480) camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 360)
- Check browser permissions
- Click anywhere on the page to enable audio
- Try different browser (Chrome recommended)
pip uninstall mediapipe
pip install mediapipe==0.10.8This project is licensed under the MIT License.
MIT License
Copyright (c) 2025 DriveSafe AI
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- MediaPipe by Google for facial landmark detection
- OpenCV community for computer vision tools
- Chart.js for beautiful visualizations
- Research papers on drowsiness detection algorithms
For questions, suggestions, or contributions:
- ๐จโ๐ป Developer: Ajith Kumar Murugan
- ๐ Report Issues: GitHub Issues
- ๐ก Feature Requests: Open a discussion on GitHub
Made with โค๏ธ for safer roads by Ajith Kumar Murugan
โญ Star this project if you find it useful!