A computer-vision application for detecting driver drowsiness from images, video files, and live webcam streams. The project uses a YOLO model trained to classify eye state as open or closed, then raises a visual alert when closed-eye detections persist across consecutive frames.
- Real-time webcam drowsiness detection with OpenCV video capture.
- Image and video-file inference workflows through a Tkinter desktop GUI.
- YOLO-based open/closed eye classification using Ultralytics.
- Consecutive-frame alert logic to reduce false positives.
- Dataset preparation script that converts folder-based datasets into YOLO format.
- Training script for fine-tuning YOLO models on custom drowsiness datasets.
- Optional saving of annotated detection results.
- Python
- OpenCV
- Ultralytics YOLO
- PyTorch and Torchvision
- NumPy
- Pillow
- Tkinter
The project is organized around a simple model lifecycle:
- Raw images are grouped into
open/andclosed/folders. prepare_dataset.pyconverts those images into YOLO-compatible image and label splits.train.pyfine-tunes a YOLO model usingdataset.yaml.detect.py,detect_image.py, andapp.pyrun inference for webcam, image, video, and GUI workflows.- Runtime outputs such as datasets, model weights, and training runs are intentionally excluded from Git.
Clone the repository:
git clone https://github.com/<your-username>/drowsiness-detector.git
cd drowsiness-detectorCreate and activate a virtual environment:
python -m venv .venvOn Windows:
.venv\Scripts\activateOn macOS/Linux:
source .venv/bin/activateInstall dependencies:
pip install -r requirements.txtCreate a raw dataset with the following structure:
raw_dataset/
open/
image_001.jpg
closed/
image_001.jpg
Convert it into YOLO format:
python prepare_dataset.py --src raw_datasetpython train.py --batch 8 --imgsz 640 --epochs 50The best model weights are saved under:
runs/train/drowsiness_v1/weights/best.pt
python app.py --weights runs/train/drowsiness_v1/weights/best.ptIf no weights are supplied, the GUI prompts you to choose a .pt model file.
python detect.py --weights runs/train/drowsiness_v1/weights/best.pt --source 0python detect_image.py --weights runs/train/drowsiness_v1/weights/best.pt --source path/to/image.jpg --saveAdd screenshots after running the application:
| GUI Detection | Drowsiness Alert |
|---|---|
screenshots/gui-detection.png |
screenshots/drowsiness-alert.png |
drowsiness-detector/
app.py # Tkinter GUI for image, video, and webcam inference
detect.py # Real-time webcam/video detection script
detect_image.py # Single-image and folder inference script
prepare_dataset.py # Dataset conversion utility
train.py # YOLO training entry point
dataset.yaml # YOLO dataset configuration
requirements.txt # Python dependencies
README.md # Project documentation
LICENSE # Project license
Ignored local folders:
raw_dataset/ # Source dataset
data/ # YOLO-formatted dataset
runs/ # Training outputs
weights/ # Local model weights
detection_results/ # Saved inference outputs
- Add an audible alarm for drowsiness events.
- Export trained weights to ONNX for lighter deployment.
- Add a Streamlit or web-based interface.
- Add automated tests for dataset preparation and inference helpers.
- Improve alert logic with blink duration, face tracking, and temporal smoothing.
- Package the application as a Windows executable.
This project is licensed under the MIT License. See LICENSE for details.