A real-time driver drowsiness detection system built using OpenCV + MediaPipe, without using YOLO or any deep-learning object detector.
This project focuses on understanding AI system design, combining computer vision, geometry, and time-based logic instead of training heavy models.
To detect driver drowsiness by monitoring eye closure over time using facial landmarks.
Core rule:
If the driver's eyes remain closed for more than a fixed duration → trigger an alert.
This project teaches real-world AI concepts:
- AI does NOT always mean deep learning
- Feature engineering can replace training
- Geometry + logic = reliable systems
- Time-based reasoning avoids false alarms
- Explainable AI (easy to understand & debug)
This approach is commonly used in:
- Automotive safety systems
- Fleet monitoring
- Driver assistance systems
✅ Real-time webcam processing
✅ Face and eye landmark detection
✅ Eye Aspect Ratio (EAR) calculation
✅ Time-based drowsiness detection
✅ Visual alert on screen
✅ Optional sound alert (macOS)
✅ No dataset required
✅ No model training required
✅ Runs fully offline on CPU
- Python 3.9+
- OpenCV – video capture & visualization
- MediaPipe Face Mesh – facial landmarks
- NumPy – numerical operations
- SciPy – distance calculations
- Time module – duration tracking
❌ No YOLO
❌ No deep learning training
❌ No GPU required
Webcam Frame
↓
Face Detection (MediaPipe)
↓
Eye Landmark Extraction
↓
Eye Aspect Ratio (EAR)
↓
Time-based Logic
↓
DROWSINESS ALERT
EAR measures how open an eye is using landmark distances.
EAR = (vertical distances) / (horizontal distance)
- Eye open → EAR ≈ 0.30
- Eye closed → EAR ≈ 0.15
If EAR drops below a threshold for enough time, drowsiness is detected.
drowny/
├── drowsiness_detection.py # Main application file
├── venv/ # Python virtual environment
└── README.md # Project documentation
python3 -m venv venv
source venv/bin/activatepip install opencv-python mediapipe numpy scipypip install mediapipe==0.10.8python3 drowsiness_detection.py- Press
qto quit
Inside drowsiness_detection.py:
EAR_THRESHOLD = 0.25 # Eye closed threshold
DROWSY_TIME = 2.0 # Seconds eyes must stay closedTuning tips:
- Lower
EAR_THRESHOLD→ more sensitive - Lower
DROWSY_TIME→ faster alerts
- Blinking ≠ drowsiness
- Time-based logic is critical
- Geometry can replace deep learning
- AI systems need rules, not just predictions
- Debugging environments is part of AI work
- Works best for frontal faces
- Single-person detection
- Glasses may affect accuracy
- No head-pose compensation (future improvement)
- Sound alarm using audio file
- Head pose estimation
- Multiple face handling
- Event logging to file
- Mobile / edge deployment
- Combine with deep learning later
- Driver safety systems
- Fleet monitoring
- Long-haul transport safety
- Automotive research
- AI learning & demos
This project is for educational and learning purposes. Use responsibly in real-world safety systems with proper testing.
This project proves that:
Good AI is about understanding the problem, not just using big models.
You now understand AI system design, not just AI tools.
Happy coding! 🚗💤