A real-time computer vision system that detects signs of driver drowsiness from a live webcam feed to help prevent fatigue-related accidents. The system analyzes facial landmarks frame by frame using eye aspect ratio (EAR) thresholds and triggers an audio alert when the driver's eyes remain closed for an extended period. A separate Flask demo can also classify uploaded eye images using a local Keras model. This was a five-person team project developed through the AI Student Collective at UC Davis during the Winter 2025 cycle, where it won Best Execution.
Kaggle Dataset (optional, for the image scripts): https://www.kaggle.com/datasets/prasadvpatil/mrl-dataset
This is the path that matches the eye-state / EAR webcam work.
Run from Terminal.app (macOS Camera permission often fails when launched from an IDE):
cd "/path/to/drowsy-driver-detection"
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python setup_live_demo.py # downloads dlib's 68-point landmark model
python SourceCode.py # webcam window; press q to quitIf the camera fails: System Settings → Privacy & Security → Camera → enable Terminal.
Close your eyes for ~70 frames (a few seconds) to trigger DROWSINESS ALERT! and music.wav.
Defaults: EAR threshold 0.25, consecutive-frame check 70.
SourceCode.py— Real-time EAR (eye aspect ratio) detector. Uses a webcam, dlib facial landmarks, and EAR thresholds; plays an alert when eyes stay closed. This is the main live demo and does not needdrowsiness_model.h5.setup_live_demo.py— Downloads and decompressesshape_predictor_68_face_landmarks.datinto the project root.app.py— Separate Flask image-upload demo. Servesindex.html, accepts an uploaded eye image, and classifies it with a Keras model file nameddrowsiness_model.h5. That model file is not shipped in this repo; without it the app starts but classification returns a clear error and the UI explains that the demo needs the model.dataset_processing.py/test_data.py— Optional helpers for exploring or scoring images from a localkaggle_data/tree. They are not required for the live EAR demo.
- Real-Time Eye Blink Detection: Monitors eye aspect ratio (EAR) to detect signs of drowsiness.
- Facial Landmark Detection: Tracks eye landmarks with dlib’s 68-point predictor.
- Optional image classification demo: Flask upload UI when
drowsiness_model.h5is available locally. - Alert Mechanism: Plays a warning sound (
music.wav) when prolonged eye closure is detected. - Efficient Processing: Uses
imutils,scipy, andOpenCVfor the live pipeline.
- Python: Main programming language.
- OpenCV: Video capture, grayscale preprocessing, and convex-hull eye overlays.
- dlib: Frontal face detector and 68-point facial landmark model.
- Pygame: Audio alerts.
- scipy / imutils: EAR Euclidean distances and frame helpers.
- Flask / TensorFlow / Pillow: Optional upload demo and model inference (
requirements-optional.txt).
.
├── SourceCode.py # Live webcam EAR detector (main demo)
├── setup_live_demo.py # Downloads the dlib shape predictor
├── app.py # Flask image-upload demo
├── index.html # Upload UI for app.py
├── test_data.py # Optional model smoke test on sample images
├── dataset_processing.py # Optional Kaggle eye-image explorer
├── music.wav # Alert sound
├── requirements.txt # Live EAR demo dependencies
├── requirements-optional.txt # Flask / TensorFlow extras
├── shape_predictor_68_face_landmarks.dat # Via setup_live_demo.py (gitignored)
├── drowsiness_model.h5 # Optional; not in repo
└── kaggle_data/ # Optional; download from Kaggle if needed
- Eye Aspect Ratio (EAR):
SourceCode.pycomputes EAR from eye landmarks via scipy Euclidean distance. If EAR stays below0.25for70consecutive frames, the driver is treated as drowsy. - Real-Time Alerts: When the threshold is breached, the system overlays a warning, draws convex-hull eye contours, and plays
music.wav. - Missing assets: If the landmark model is missing, the script exits with setup instructions (run
setup_live_demo.py). Missingmusic.wavkeeps visual alerts working. - Optional upload demo:
app.pypreprocesses an uploaded grayscale eye crop and runs a local Keras model if present.
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python setup_live_demo.pyNote: dlib often needs a working C++ toolchain. On macOS, Xcode Command Line Tools help; on Linux, install build essentials and cmake if the pip wheel is unavailable.
- Confirm
music.wavis present (shipped in this repo) and the shape predictor was downloaded. - Run:
python SourceCode.py
- Press
qto quit. Adjustthresh(default0.25) andframe_check(default70) inSourceCode.pyif needed.
- Optionally place a trained
drowsiness_model.h5in the project root (not included here; no training script is in this fork). - Install extras:
pip install -r requirements-optional.txt - Run:
python app.py
- Open the local URL Flask prints and upload an eye image. Without the model file, the page explains what is missing and
/classifyreturns a clear JSON error.
Download the MRL eye dataset (or equivalent) into ./kaggle_data/train/Closed_Eyes and ./kaggle_data/train/Open_Eyes, then run dataset_processing.py or test_data.py.
This project uses: