Skip to content

Repository files navigation

Drowsiness Detector

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.

Features

  • 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.

Technologies Used

  • Python
  • OpenCV
  • Ultralytics YOLO
  • PyTorch and Torchvision
  • NumPy
  • Pillow
  • Tkinter

Project Architecture

The project is organized around a simple model lifecycle:

  1. Raw images are grouped into open/ and closed/ folders.
  2. prepare_dataset.py converts those images into YOLO-compatible image and label splits.
  3. train.py fine-tunes a YOLO model using dataset.yaml.
  4. detect.py, detect_image.py, and app.py run inference for webcam, image, video, and GUI workflows.
  5. Runtime outputs such as datasets, model weights, and training runs are intentionally excluded from Git.

Installation

Clone the repository:

git clone https://github.com/<your-username>/drowsiness-detector.git
cd drowsiness-detector

Create and activate a virtual environment:

python -m venv .venv

On Windows:

.venv\Scripts\activate

On macOS/Linux:

source .venv/bin/activate

Install dependencies:

pip install -r requirements.txt

Usage

Prepare a Dataset

Create 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_dataset

Train the Model

python train.py --batch 8 --imgsz 640 --epochs 50

The best model weights are saved under:

runs/train/drowsiness_v1/weights/best.pt

Run the Desktop GUI

python app.py --weights runs/train/drowsiness_v1/weights/best.pt

If no weights are supplied, the GUI prompts you to choose a .pt model file.

Run Webcam Detection

python detect.py --weights runs/train/drowsiness_v1/weights/best.pt --source 0

Run Image Detection

python detect_image.py --weights runs/train/drowsiness_v1/weights/best.pt --source path/to/image.jpg --save

Screenshots

Add screenshots after running the application:

GUI Detection Drowsiness Alert
screenshots/gui-detection.png screenshots/drowsiness-alert.png

Folder Structure

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

Future Improvements

  • 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.

License

This project is licensed under the MIT License. See LICENSE for details.

About

Real-time driver drowsiness detection app using Python, OpenCV, Tkinter, and Ultralytics YOLO for image, video, and webcam inference.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages