A real-time computer vision system that monitors driver drowsiness using facial landmark detection and triggers an audio alarm when drowsiness is detected. This project uses OpenCV, dlib, and machine learning to detect eye closure patterns and prevent accidents caused by driver fatigue.
Driver drowsiness is a major cause of road accidents worldwide. This system provides a non-invasive, camera-based solution to detect drowsiness in real-time by analyzing the driver's eye aspect ratio (EAR) and triggering an alarm when prolonged eye closure is detected.
- Face Detection: Uses dlib's HOG-based face detector to locate the face in each frame
- Facial Landmark Detection: Identifies 68 facial landmarks, focusing on eye regions (landmarks 36-47)
- Eye Aspect Ratio (EAR): Calculates the ratio between vertical and horizontal eye distances
- Drowsiness Classification: Monitors EAR values over consecutive frames to classify states:
- Active (eyes open)
- Drowsy (eyes partially closed)
- Sleeping (eyes fully closed for extended period)
- Alarm Trigger: Plays audio alert when sleeping state is detected
Drowsiness Detection Alarm/
β
βββ src/ # Source code directory
β βββ Sleep_Detect_Alarm.py # Main script with alarm functionality
β βββ test.py # Test script (no alarm, for debugging)
β
βββ assets/ # Assets directory
β βββ images/ # Project images & diagrams
β β βββ demo_preview.png # Demo simulation screenshot
β β βββ system_workflow.png # Flowchart of the system
β β βββ facial_landmarks.png # Facial landmark explanation
β β
β βββ Alert.mp3 # Audio alarm file
β βββ shape_predictor_68_face_landmarks.dat # Pre-trained facial landmark model
β
βββ nk.txt # Notes/configuration file
βββ README.MD # This file
Sleep_Detect_Alarm.py: Production-ready script with audio alarm (uses threading for non-blocking audio)test.py: Testing version without audio alarm for debugging and development
images/: Contains visual documentation and diagrams created for this projectshape_predictor_68_face_landmarks.dat: Pre-trained dlib model for detecting 68 facial landmarksAlert.mp3: Audio file played when drowsiness is detected
- β Real-time face detection using dlib's frontal face detector
- β 68-point facial landmark tracking for precise eye monitoring
- β Eye Aspect Ratio (EAR) calculation for drowsiness detection
- β Three-state classification: Active, Drowsy, Sleeping
- β Audio alarm system with threaded playback (non-blocking)
- β Visual feedback with color-coded status display
- β Live webcam feed with overlay visualization
- β Adjustable sensitivity through threshold parameters
- Python 3.7 or higher
opencv-python # Computer vision operations
numpy # Numerical computations
dlib # Face detection & landmark prediction
imutils # Image processing utilities
playsound # Audio playback for alarm- Webcam: Built-in or USB camera
- Pre-trained Model:
shape_predictor_68_face_landmarks.dat(included in project)
git clone <repository-url>
cd "Drowsiness Detection Alarm"# Install required Python packages
pip install opencv-python numpy dlib imutils playsound
# On macOS, you might need:
pip install pyobjc-framework-AVFoundation # For playsoundThe shape_predictor_68_face_landmarks.dat file is already included in this repository. If missing, download it from:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
Extract and place in the project root directory.
python src/Sleep_Detect_Alarm.pypython src/test.py- ESC key: Exit the application
- Two windows will appear:
- Frame: Shows status overlay (Active/Drowsy/Sleeping)
- Detection: Shows facial landmarks and face bounding box
If you move the model or audio file, update these lines in the script:
# Line 13 in src/Sleep_Detect_Alarm.py
predictor = dlib.shape_predictor("assets/shape_predictor_68_face_landmarks.dat")
# Line 25 in src/Sleep_Detect_Alarm.py
playsound('assets/Alert.mp3')# In blinked() function (lines 30-41)
if(ratio > 0.25): # Eyes open
return 2
elif(ratio > 0.21): # Eyes partially closed
return 1
else: # Eyes closed
return 0# Sleeping detection (line 68)
if(sleep > 8): # Trigger alarm after 8 frames of closed eyes
# Drowsy detection (line 78)
if(drowsy > 6): # Show drowsy warning after 6 framesNote: Higher values = less sensitive (fewer false alarms, but slower detection)
The Eye Aspect Ratio is calculated using 6 facial landmarks per eye:
p2 p3
p1 β’β’β’β’β’ p4
p5 p6
EAR = (||p2 - p6|| + ||p3 - p5||) / (2 * ||p1 - p4||)
- High EAR (> 0.25): Eyes are open β Active
- Medium EAR (0.21 - 0.25): Eyes partially closed β Drowsy
- Low EAR (< 0.21): Eyes closed β Sleeping
When EAR remains low for consecutive frames, the alarm is triggered.
- Left Eye: Landmarks 36-41
- Right Eye: Landmarks 42-47
| State | Condition | Color Code | Action |
|---|---|---|---|
| Active | Both eyes open | Blue | Normal monitoring |
| Drowsy | Eyes partially closed (6+ frames) | Red | Visual warning |
| Sleeping | Eyes fully closed (8+ frames) | Blue | Audio alarm + visual alert |
The alarm uses Python's threading module to play audio without blocking the main detection loop:
t = threading.Thread(target=alert_sound)
t.start()# Change camera index in line 9
cap = cv2.VideoCapture(0) # Try 1, 2, etc. if 0 doesn't workError: Unable to open shape_predictor_68_face_landmarks.dat
Solution: Ensure the .dat file is in the correct location and update the path in line 13
- macOS: Install
pyobjc-framework-AVFoundation - Linux: Use
python3-gst-1.0orsox - Windows: Should work out of the box with
playsound
- Reduce video resolution
- Use a lighter face detection model
- Increase frame skip rate
| Feature | Sleep_Detect_Alarm | test |
|---|---|---|
| Audio Alarm | β | β |
| Threading | β | β |
| Full Status Display | β | β |
| Facial Landmarks Display | β | β |
| Purpose | Production | Debug |
- Add mobile app integration for remote monitoring
- Implement yawning detection using mouth landmarks
- Add data logging and drowsiness pattern analysis
- Support for multiple face detection (passenger monitoring)
- Integration with vehicle systems (steering, speed, etc.)
- Deep learning model for improved accuracy (CNN/LSTM)
- Head pose estimation for distraction detection
- Cloud-based alert system for fleet management
- dlib Face Detection: http://dlib.net/face_detector.py.html
- Facial Landmarks: http://dlib.net/face_landmark_detection.py.html
- Eye Aspect Ratio Paper: "Real-Time Eye Blink Detection using Facial Landmarks" (SoukupovΓ‘ & Δech, 2016)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open-source and available under the MIT License.
Ankit Kumar Tiwari
B.Tech β Electronics & Communication Engineering
This system is intended as a supplementary safety tool and should NOT be relied upon as the sole means of preventing drowsy driving. Always ensure adequate rest before driving long distances.


