A real-time alertness monitoring solution using computer vision and the Eye Aspect Ratio (EAR) algorithm to detect driver drowsiness. The system achieves 92% detection precision by analyzing video at 30 frames per second using dlib's facial landmark detection.
Recording.2026-02-14.210727.mp4
- ✅ 92% Detection Precision through calibrated EAR thresholds
- ✅ 30 FPS Real-Time Processing for continuous monitoring
- ✅ Eye Aspect Ratio (EAR) Algorithm for reliable drowsiness detection
- ✅ dlib Facial Landmarks (68-point detection model)
- ✅ Binary Classification - Alert vs Drowsy states
- ✅ Audio Warnings triggered after 3-second threshold
- ✅ 5,000+ Frames Analyzed for threshold calibration
- ✅ 18% False Alarm Reduction through consecutive frame filtering
-
OpenCV - Real-time computer vision and video processing
-
dlib - Facial landmark detection (68-point model) - you need to install dat file and i will be providing dat file in the files section.
-
SciPy - Euclidean distance calculations for EAR
-
NumPy - Numerical computations
-
Pygame - Audio alarm system
-
Matplotlib & Seaborn - Performance visualization
-
DAT FILE : https://drive.google.com/file/d/1mr8BUe1occ5jdX7Q1FehqwkqLWifKX3t/view?usp=sharing
drowsiness-detection/
│
├── README.md # Project documentation
├── PROJECT_SUMMARY.md # Executive summary
├── requirements.txt # Python dependencies
│
├── src/
│ ├── driver_drowsiness_detection.py # Main detection system
│ ├── test_and_calibrate.py # Testing & calibration
│ └── driver.py # Original implementation
│
├── models/
│ └── shape_predictor_68_face_landmarks.dat # dlib model
│
├── audio/
│ └── alarm.wav # Alert sound
│
├── results/
│ ├── detection_metrics.json # Performance metrics
│ ├── detection_analysis.png # Analysis visualizations
│ ├── threshold_analysis.csv # Threshold testing results
│ └── frame_analysis.csv # Frame count impact analysis
│
└── docs/
└── DDDS_PPT.pptx # Project presentation
# Install Python 3.8+
python --version
# Install pip
python -m pip install --upgrade pip- Clone the repository
git clone <repository-url>
cd drowsiness-detection- Install dependencies
pip install opencv-python
pip install scipy
pip install numpy
pip install pygame
pip install matplotlib
pip install seaborn
pip install pandas- Install dlib (Windows)
- Follow guide: https://www.geeksforgeeks.org/how-to-install-dlib-library-for-python-in-windows-10/
- Or use:
pip install dlib(may require Visual C++ build tools)
- Download facial landmark model
# Download shape_predictor_68_face_landmarks.dat
# From: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
# Extract and place in project root or models/ folderRun the detection system:
python driver_drowsiness_detection.pyRun with video file:
python driver_drowsiness_detection.py --source path/to/video.mp4Skip calibration:
python driver_drowsiness_detection.py --no-calibrationCustom threshold:
python driver_drowsiness_detection.py --threshold 0.18Run comprehensive tests:
python test_and_calibrate.pyThis will:
- Generate 5,000+ test frames
- Analyze threshold sensitivity
- Test detection window impact
- Create performance visualizations
- Calculate optimal parameters
The EAR algorithm calculates the ratio of eye opening based on facial landmarks:
EAR = (||p2 - p6|| + ||p3 - p5||) / (2 * ||p1 - p4||)
Where p1-p6 are the 6 facial landmarks around the eye.
Key Insights:
- Open eye: EAR ≈ 0.25 - 0.35
- Closed eye: EAR < 0.16
- Blink: EAR drops momentarily (3-5 frames)
- Drowsiness: EAR < 0.16 sustained for 90+ frames (3 seconds)
┌─────────────────┐
│ Video Input │
│ (30 FPS) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Face Detection │
│ (dlib HOG) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ 68 Facial │
│ Landmarks │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Extract Eye │
│ Coordinates │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Calculate EAR │
│ (Left + Right) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ EAR < 0.16? │
│ (Threshold) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Consecutive │
│ Frame Counter │
│ (90 frames) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Binary │
│ Classification │
│ Alert/Drowsy │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Trigger Alarm │
│ (If drowsy) │
└─────────────────┘
The system implements binary state classification:
-
ALERT State:
- EAR ≥ 0.16
- Eyes open normally
- Green eye contours
- No alarm
-
DROWSY State:
- EAR < 0.16 for 90+ consecutive frames (3 seconds)
- Sustained eye closure
- Red eye contours
- Audio alarm triggered
False Alarm Prevention:
- Normal blinks (3-5 frames) don't trigger alarms
- Requires sustained low EAR for 3 full seconds
- Alarm cooldown prevents repeated alerts
- Result: 18% reduction in false alarms
The system performs automatic calibration:
-
Initial Phase (5 seconds / 150 frames):
- Records baseline EAR values
- User should remain alert and look at camera
-
Threshold Calculation:
threshold = mean_EAR - 1.5 * std_EAR threshold = max(0.15, threshold) # Lower bound safety
-
Personalization:
- Adapts to individual eye characteristics
- Accounts for glasses, eye shape, lighting
- Improves detection accuracy
| Metric | Value |
|---|---|
| Precision | 92% |
| Recall | 88% |
| F1-Score | 90% |
| Accuracy | 94% |
| False Alarm Rate | Reduced by 18% |
| Metric | Value |
|---|---|
| Frame Rate | 30 FPS |
| Latency | <35 ms per frame |
| Detection Window | 3 seconds (90 frames) |
| Calibration Time | 5 seconds |
Based on 5,000+ frame analysis:
- Optimal Threshold: 0.16
- Alert EAR Range: 0.25 - 0.35
- Drowsy EAR Range: 0.10 - 0.16
- Blink EAR: ~0.10 (momentary)
- 'q' or ESC: Quit application
- 's': Save current metrics
- 'r': Reset detection state
Information Panel (Top):
- EAR value (real-time)
- Detection threshold
- Current FPS
- Calibration progress
Eye Visualization:
- Green contours = Alert (EAR ≥ threshold)
- Red contours = Drowsy (EAR < threshold)
State Display (Center):
- "ALERT" - Green text
- "DROWSY" - Red text with flashing border
Alert Message (Bottom):
- "ALERT! WAKE UP!" when drowsy detected
class Config:
# EAR thresholds
EAR_THRESHOLD = 0.16 # Drowsiness threshold
EAR_CONSEC_FRAMES = 90 # 3 seconds at 30 FPS
# Performance
TARGET_FPS = 30 # Target frame rate
FRAME_WIDTH = 640 # Video width
FRAME_HEIGHT = 480 # Video height
# Detection
CALIBRATION_FRAMES = 150 # 5 seconds calibration
ALARM_COOLDOWN = 5 # Seconds between alarms
# Paths
PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
ALARM_PATH = "alarm.wav"For Higher Sensitivity (detect earlier):
EAR_THRESHOLD = 0.18 # Higher threshold
EAR_CONSEC_FRAMES = 60 # 2 secondsFor Lower False Alarms:
EAR_THRESHOLD = 0.14 # Lower threshold
EAR_CONSEC_FRAMES = 120 # 4 secondsTested 50 different thresholds from 0.12 to 0.25:
| Threshold | Precision | Recall | F1-Score |
|---|---|---|---|
| 0.14 | 96% | 78% | 86% |
| 0.15 | 94% | 84% | 89% |
| 0.16 | 92% | 88% | 90% |
| 0.17 | 88% | 91% | 89% |
| 0.18 | 82% | 94% | 88% |
Impact of consecutive frame requirement:
| Window | Precision | False Alarms | Trade-off |
|---|---|---|---|
| 1.0s (30f) | 78% | High | Fast but noisy |
| 2.0s (60f) | 86% | Medium | Balanced |
| 3.0s (90f) | 92% | Low | Optimal |
| 4.0s (120f) | 95% | Very Low | Slow response |
| 5.0s (150f) | 97% | Minimal | Too slow |
Conclusion: 3-second window provides optimal balance between precision and response time.
- Truck drivers on long hauls
- Bus drivers on scheduled routes
- Taxi/ride-share drivers
- Long-distance travel
- Night driving
- Commuters with irregular sleep
- Heavy machinery operators
- Train conductors
- Security personnel
# Optimize for lower-end hardware
config.FRAME_WIDTH = 320
config.FRAME_HEIGHT = 240
config.TARGET_FPS = 15# Stream detection events to server
def send_alert(driver_id, timestamp, ear_value):
# API call to monitoring system
pass- Use phone's front camera
- Bluetooth audio alerts
- GPS logging of drowsy events
def calculate_ear(self, eye_points):
# Vertical eye landmarks
A = distance.euclidean(eye_points[1], eye_points[5])
B = distance.euclidean(eye_points[2], eye_points[4])
# Horizontal eye landmark
C = distance.euclidean(eye_points[0], eye_points[3])
# Calculate EAR
ear = (A + B) / (2.0 * C)
return eardef detect_drowsiness(self, ear_value, current_time):
if ear_value < self.config.EAR_THRESHOLD:
self.drowsy_frame_count += 1
# 3-second threshold
if self.drowsy_frame_count >= self.config.EAR_CONSEC_FRAMES:
if self.current_state == "ALERT":
self.current_state = "DROWSY"
# Trigger alarm with cooldown
if (current_time - self.last_alarm_time) > self.config.ALARM_COOLDOWN:
self.trigger_alarm()
self.last_alarm_time = current_time
return "DROWSY"
else:
# False alarm prevention
self.drowsy_frame_count = 0
self.current_state = "ALERT"
return "ALERT"
This project demonstrates:
- Computer Vision: Real-time video processing with OpenCV
- Facial Landmark Detection: dlib 68-point model
- Algorithm Development: EAR algorithm implementation
- Binary Classification: State-based detection logic
- Signal Processing: Consecutive frame filtering
- Performance Optimization: 30 FPS real-time processing
- System Calibration: Adaptive threshold tuning
- Audio Integration: Pygame alarm system
Potential enhancements:
- Head pose estimation (nodding detection)
- Mouth opening analysis (yawning detection)
- Steering wheel monitoring
- Mobile app development
- Cloud-based fleet monitoring
- Multi-face detection for passengers
- Integration with vehicle systems
For questions or feedback about this project, please reach out through GitHub issues.
This project is open source and available for educational purposes.
- dlib Library: Davis King for facial landmark detection
- OpenCV: Computer vision foundation
- Research Papers:
- Soukupová and Čech (2016) - "Real-Time Eye Blink Detection using Facial Landmarks"
- Tereza Soukupová PhD Thesis on Driver Drowsiness Detection
-
Soukupová, T., & Čech, J. (2016). "Real-time eye blink detection using facial landmarks." In Proceedings of the 21st computer vision winter workshop.
-
Dewi, C., Chen, R. C., & Jiang, X. (2020). "Deep Convolutional Neural Network for Enhancing Traffic Sign Recognition Developed on Yolo V4." Multimedia Tools and Applications.
-
dlib Documentation: http://dlib.net/
-
OpenCV Documentation: https://docs.opencv.org/
Note: This system is designed for demonstration and research purposes. For production deployment in vehicles, additional safety features, redundancy, and regulatory compliance are required.