Skip to content

ManishKajla/Drowsiness-Detection-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drowsiness Detection System

A real-time drowsiness detection system designed for Raspberry Pi that monitors driver alertness using computer vision and machine learning. The system uses facial landmark detection to calculate Eye Aspect Ratio (EAR) and triggers an audible alarm when drowsiness is detected.

Overview

This IoT-based drowsiness detection system captures live video feed from a Raspberry Pi camera, analyzes facial features to detect eye closure patterns, and activates a buzzer alarm when drowsiness is detected. The system is designed to help prevent accidents caused by driver fatigue.

Features

  • Real-time face detection and tracking
  • Eye Aspect Ratio (EAR) calculation for drowsiness detection
  • Audible alarm system using buzzer
  • Visual feedback with on-screen alerts
  • Drowsiness event logging
  • Configurable sensitivity thresholds
  • Histogram equalization for improved detection in varying lighting conditions

System Requirements

Hardware

  • Raspberry Pi (any model with GPIO support)
  • Raspberry Pi Camera Module (PiCamera2 compatible)
  • Buzzer connected to GPIO pin 17
  • Monitor or display for visual output

Software

  • Python 3.11
  • Raspberry Pi OS (or compatible Linux distribution)

Dependencies

The project requires the following Python packages:

  • opencv-python (4.11.0.86)
  • dlib (19.24.8)
  • imutils (0.5.4)
  • scipy (1.15.2)
  • RPi.GPIO (0.7.1)
  • picamera2
  • numpy

Installation

1. Clone or Download the Project

cd /path/to/project/Drowsiness_Detection

2. Create Virtual Environment

A virtual environment is already included in the myenv directory. To activate it:

source myenv/bin/activate

Alternatively, create a new virtual environment:

python3 -m venv myenv
source myenv/bin/activate

3. Install Dependencies

pip install opencv-python
pip install dlib
pip install imutils
pip install scipy
pip install RPi.GPIO
pip install picamera2

4. Download Face Landmark Model

The system requires the dlib 68-point facial landmark predictor model. Ensure the following file is present in the models directory:

models/shape_predictor_68_face_landmarks.dat

If not present, download it from:

http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

Extract and place in the models directory.

Hardware Setup

GPIO Configuration

Connect the buzzer to Raspberry Pi GPIO:

  • Buzzer positive terminal: GPIO 17 (BCM numbering)
  • Buzzer negative terminal: GND

Camera Setup

Ensure the Raspberry Pi Camera Module is properly connected and enabled:

sudo raspi-config
# Navigate to Interface Options -> Camera -> Enable

Usage

Running the Drowsiness Detector

python drowsiness_detector.py

The system will:

  1. Initialize the camera (2-second warm-up period)
  2. Load the facial landmark predictor
  3. Start real-time video processing
  4. Display live video feed with detection overlays
  5. Trigger buzzer alarm when drowsiness is detected

Stopping the System

Press q key in the video window or use Ctrl+C to stop the system gracefully.

Testing Camera

To verify camera functionality:

python test.py

This will capture a test frame and display its dimensions.

How It Works

Eye Aspect Ratio (EAR)

The system uses the Eye Aspect Ratio algorithm to detect eye closure:

  1. Face Detection: Uses dlib's frontal face detector to locate faces in the frame
  2. Landmark Detection: Identifies 68 facial landmarks, including eye coordinates
  3. EAR Calculation: Computes the ratio between eye height and width
    • Formula: EAR = (||p2-p6|| + ||p3-p5||) / (2 * ||p1-p4||)
    • Where p1-p6 are the eye landmark points
  4. Drowsiness Detection: When EAR falls below threshold for consecutive frames, drowsiness is detected

Detection Parameters

  • EAR Threshold: 0.24 (configurable in code)
  • Consecutive Frames: 3 frames (configurable in code)
  • Camera Resolution: 640x480 pixels
  • Display Resolution: 1280x720 pixels (upscaled)

Alarm Logic

  • Buzzer activates when eyes remain closed for 3 consecutive frames
  • Buzzer deactivates when eyes reopen
  • Alarm also deactivates if no face is detected

Project Structure

Drowsiness_Detection/
├── drowsiness_detector.py      # Main drowsiness detection script
├── drowsiness_detection.ipynb  # Jupyter notebook for development/testing
├── test.py                     # Camera testing script
├── drowsiness_log.csv          # Log file for drowsiness events
├── README.md                   # This file
├── assets/                     # Sample eye images
│   ├── eye1.jpg
│   ├── eye2.png
│   └── eye3.jpg
├── models/                     # Machine learning models
│   └── shape_predictor_68_face_landmarks.dat
└── myenv/                      # Python virtual environment

Logging

The system logs drowsiness events to drowsiness_log.csv with the following format:

timestamp,ear_value,alarm_status
2025-05-08 14:44:46.477,0.1664,0
  • timestamp: Date and time of the event
  • ear_value: Calculated Eye Aspect Ratio
  • alarm_status: 0 (off) or 1 (on)

Configuration

Adjusting Sensitivity

Modify these constants in drowsiness_detector.py:

EYE_AR_THRESH = 0.24          # Lower = more sensitive
EYE_AR_CONSEC_FRAMES = 3      # Higher = less sensitive

Changing GPIO Pin

Modify the buzzer pin number:

BUZZER_PIN = 17  # Change to your GPIO pin

Camera Resolution

Adjust camera configuration:

config = cam.create_preview_configuration(
    main={"format": 'BGR888', "size": (640, 480)}
)

Performance Optimization

  • System processes frames in real-time
  • Histogram equalization improves detection in varying light
  • Face detection uses single-scale processing (parameter 0)
  • Display resolution upscaled for better visibility

Limitations

  • Requires clear view of the face
  • Performance depends on lighting conditions
  • Single face detection (processes first detected face)
  • Requires Raspberry Pi hardware with GPIO
  • Not suitable for multiple simultaneous users

Future Enhancements

  • Multi-face detection support
  • Cloud-based logging and analytics
  • Mobile app integration for alerts
  • Head pose estimation for attention tracking
  • Night vision support with IR camera
  • Yawn detection algorithm
  • Integration with vehicle systems

Safety Notice

This system is designed as an educational project and assistive tool. It should not be relied upon as the sole safety mechanism for preventing drowsy driving. Always ensure adequate rest before operating vehicles or machinery.

License

This project is provided as-is for educational and research purposes.

Technical Details

Facial Landmark Points

The system uses dlib's 68-point facial landmark model:

  • Points 36-41: Left eye
  • Points 42-47: Right eye

Processing Pipeline

  1. Capture frame from PiCamera2
  2. Convert to grayscale
  3. Apply histogram equalization
  4. Detect faces using dlib
  5. Extract facial landmarks
  6. Calculate EAR for both eyes
  7. Average EAR values
  8. Compare against threshold
  9. Track consecutive closed frames
  10. Trigger alarm if threshold exceeded
  11. Display annotated frame

Contact and Support

For issues, questions, or contributions, please refer to the project repository or documentation.

Acknowledgments

  • dlib library for facial landmark detection
  • OpenCV for computer vision operations
  • Raspberry Pi Foundation for hardware platform
  • Eye Aspect Ratio algorithm based on computer vision research

About

A Raspberry Pi–based drowsiness detection system that watches for eye closure using computer vision and sounds an alarm when the driver appears sleepy. Built as a simple, real-time safety project to help reduce fatigue-related accidents.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors