Real-time driver drowsiness detector with two pipelines:
- Facial-landmark EAR/MAR (default): Uses dlib 68-point landmarks to track eye aspect ratio (EAR) and mouth aspect ratio (MAR), triggering alarms when the driver appears drowsy or yawning.
- Legacy CNN: Uses Haar cascades plus a lightweight CNN eye-state classifier for environments without dlib.
- Python 3.9+ recommended
- Install dependencies:
pip install -r requirements.txt - dlib landmark model: Download
shape_predictor_68_face_landmarks.datfrom http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 and place it in the project root. - Optional: Raspberry Pi with a buzzer connected to GPIO pin 11 (or choose another pin via CLI).
python final.py --help # view options
python final.py # run with default camera (index 0)
# Examples:
# python final.py --source path/to/video.mp4
# python final.py --no-sound --gpio --gpio-pin 11
# python final.py --mode either --mar-threshold 0.8 # include yawning triggerKey flags:
--source: camera index or video file path--no-sound: disable audio alarm (sound usesalarm.wav)--gpio: enable GPIO alarm (Raspberry Pi only)--ear-threshold/--mar-threshold: tweak sensitivity--mode:ear(default),either, ormouthto choose triggering signal--no-landmarks: hide landmark overlay
Defaults tuned for fewer false positives: EAR threshold 0.20, 16 consecutive frames, width 480. Increase --ear-frames or lower --ear-threshold further if you still get false alarms with eyes open.
python "drowsiness detection.py" --model models/eye_state_cnn.h5Uses Haar cascades from haar cascade files/ and a trained eye-state CNN. Alarms when both eyes are predicted closed for sustained frames.
Prepare data (eye open/closed) using the Kaggle dataset https://www.kaggle.com/datasets/serenaraju/yawn-eye-dataset-new or your own, with structure:
data/
train/
Open/
Close/
valid/
Open/
Close/
Then train:
python model.pyThe trained model is saved to models/eye_state_cnn.h5.
drowsiness_detector.py: Advanced EAR/MAR detector (used byfinal.py).final.py: Entry point wrapper for the advanced detector.drowsiness detection.py: Legacy CNN-based detector.model.py: CNN training script.alarm.wav: Alarm sound (used if--no-soundnot set).haar cascade files/: Haar classifiers for the legacy pipeline.shape_predictor_68_face_landmarks.dat: Required dlib landmark model (download separately).
- Press
qto quit either detector window. - Tune thresholds to your camera setup and lighting if needed.
- GPIO support gracefully disables if not on Raspberry Pi.
