Skip to content

Techie03/Driver-Drowsiness-Detection-System

Repository files navigation

Driver Drowsiness Detection System

Project Overview

A real-time alertness monitoring solution using computer vision and the Eye Aspect Ratio (EAR) algorithm to detect driver drowsiness. The system achieves 92% detection precision by analyzing video at 30 frames per second using dlib's facial landmark detection.

Project Demo Video -

Recording.2026-02-14.210727.mp4

🎯 Key Achievements

  • 92% Detection Precision through calibrated EAR thresholds
  • 30 FPS Real-Time Processing for continuous monitoring
  • Eye Aspect Ratio (EAR) Algorithm for reliable drowsiness detection
  • dlib Facial Landmarks (68-point detection model)
  • Binary Classification - Alert vs Drowsy states
  • Audio Warnings triggered after 3-second threshold
  • 5,000+ Frames Analyzed for threshold calibration
  • 18% False Alarm Reduction through consecutive frame filtering

🛠️ Technologies Used

  • OpenCV - Real-time computer vision and video processing

  • dlib - Facial landmark detection (68-point model) - you need to install dat file and i will be providing dat file in the files section.

  • SciPy - Euclidean distance calculations for EAR

  • NumPy - Numerical computations

  • Pygame - Audio alarm system

  • Matplotlib & Seaborn - Performance visualization

  • DAT FILE : https://drive.google.com/file/d/1mr8BUe1occ5jdX7Q1FehqwkqLWifKX3t/view?usp=sharing

📁 Project Structure

drowsiness-detection/
│
├── README.md                              # Project documentation
├── PROJECT_SUMMARY.md                     # Executive summary
├── requirements.txt                       # Python dependencies
│
├── src/
│   ├── driver_drowsiness_detection.py    # Main detection system
│   ├── test_and_calibrate.py             # Testing & calibration
│   └── driver.py                          # Original implementation
│
├── models/
│   └── shape_predictor_68_face_landmarks.dat  # dlib model
│
├── audio/
│   └── alarm.wav                          # Alert sound
│
├── results/
│   ├── detection_metrics.json            # Performance metrics
│   ├── detection_analysis.png            # Analysis visualizations
│   ├── threshold_analysis.csv            # Threshold testing results
│   └── frame_analysis.csv                # Frame count impact analysis
│
└── docs/
    └── DDDS_PPT.pptx                     # Project presentation

🚀 Getting Started

Prerequisites

# Install Python 3.8+
python --version

# Install pip
python -m pip install --upgrade pip

Installation

  1. Clone the repository
git clone <repository-url>
cd drowsiness-detection
  1. Install dependencies
pip install opencv-python
pip install scipy
pip install numpy
pip install pygame
pip install matplotlib
pip install seaborn
pip install pandas
  1. Install dlib (Windows)
  1. Download facial landmark model
# Download shape_predictor_68_face_landmarks.dat
# From: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
# Extract and place in project root or models/ folder

Quick Start

Run the detection system:

python driver_drowsiness_detection.py

Run with video file:

python driver_drowsiness_detection.py --source path/to/video.mp4

Skip calibration:

python driver_drowsiness_detection.py --no-calibration

Custom threshold:

python driver_drowsiness_detection.py --threshold 0.18

Testing & Calibration

Run comprehensive tests:

python test_and_calibrate.py

This will:

  • Generate 5,000+ test frames
  • Analyze threshold sensitivity
  • Test detection window impact
  • Create performance visualizations
  • Calculate optimal parameters

📊 How It Works

1. Eye Aspect Ratio (EAR) Algorithm

The EAR algorithm calculates the ratio of eye opening based on facial landmarks:

EAR = (||p2 - p6|| + ||p3 - p5||) / (2 * ||p1 - p4||)

Where p1-p6 are the 6 facial landmarks around the eye.

Key Insights:

  • Open eye: EAR ≈ 0.25 - 0.35
  • Closed eye: EAR < 0.16
  • Blink: EAR drops momentarily (3-5 frames)
  • Drowsiness: EAR < 0.16 sustained for 90+ frames (3 seconds)

2. Detection Pipeline

┌─────────────────┐
│  Video Input    │
│  (30 FPS)       │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Face Detection │
│  (dlib HOG)     │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  68 Facial      │
│  Landmarks      │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Extract Eye    │
│  Coordinates    │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Calculate EAR  │
│  (Left + Right) │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  EAR < 0.16?    │
│  (Threshold)    │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Consecutive    │
│  Frame Counter  │
│  (90 frames)    │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Binary         │
│  Classification │
│  Alert/Drowsy   │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│  Trigger Alarm  │
│  (If drowsy)    │
└─────────────────┘

3. Binary Classification Logic

The system implements binary state classification:

  • ALERT State:

    • EAR ≥ 0.16
    • Eyes open normally
    • Green eye contours
    • No alarm
  • DROWSY State:

    • EAR < 0.16 for 90+ consecutive frames (3 seconds)
    • Sustained eye closure
    • Red eye contours
    • Audio alarm triggered

False Alarm Prevention:

  • Normal blinks (3-5 frames) don't trigger alarms
  • Requires sustained low EAR for 3 full seconds
  • Alarm cooldown prevents repeated alerts
  • Result: 18% reduction in false alarms

4. Calibration Process

The system performs automatic calibration:

  1. Initial Phase (5 seconds / 150 frames):

    • Records baseline EAR values
    • User should remain alert and look at camera
  2. Threshold Calculation:

    threshold = mean_EAR - 1.5 * std_EAR
    threshold = max(0.15, threshold)  # Lower bound safety
  3. Personalization:

    • Adapts to individual eye characteristics
    • Accounts for glasses, eye shape, lighting
    • Improves detection accuracy

📈 Performance Metrics

Detection Accuracy

Metric Value
Precision 92%
Recall 88%
F1-Score 90%
Accuracy 94%
False Alarm Rate Reduced by 18%

Processing Performance

Metric Value
Frame Rate 30 FPS
Latency <35 ms per frame
Detection Window 3 seconds (90 frames)
Calibration Time 5 seconds

Threshold Analysis

Based on 5,000+ frame analysis:

  • Optimal Threshold: 0.16
  • Alert EAR Range: 0.25 - 0.35
  • Drowsy EAR Range: 0.10 - 0.16
  • Blink EAR: ~0.10 (momentary)

🎮 Controls & Interface

Keyboard Controls

  • 'q' or ESC: Quit application
  • 's': Save current metrics
  • 'r': Reset detection state

Visual Interface

Information Panel (Top):

  • EAR value (real-time)
  • Detection threshold
  • Current FPS
  • Calibration progress

Eye Visualization:

  • Green contours = Alert (EAR ≥ threshold)
  • Red contours = Drowsy (EAR < threshold)

State Display (Center):

  • "ALERT" - Green text
  • "DROWSY" - Red text with flashing border

Alert Message (Bottom):

  • "ALERT! WAKE UP!" when drowsy detected

🔧 Configuration Options

Config Class Parameters

class Config:
    # EAR thresholds
    EAR_THRESHOLD = 0.16        # Drowsiness threshold
    EAR_CONSEC_FRAMES = 90      # 3 seconds at 30 FPS
    
    # Performance
    TARGET_FPS = 30             # Target frame rate
    FRAME_WIDTH = 640           # Video width
    FRAME_HEIGHT = 480          # Video height
    
    # Detection
    CALIBRATION_FRAMES = 150    # 5 seconds calibration
    ALARM_COOLDOWN = 5          # Seconds between alarms
    
    # Paths
    PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
    ALARM_PATH = "alarm.wav"

Tuning Parameters

For Higher Sensitivity (detect earlier):

EAR_THRESHOLD = 0.18           # Higher threshold
EAR_CONSEC_FRAMES = 60         # 2 seconds

For Lower False Alarms:

EAR_THRESHOLD = 0.14           # Lower threshold
EAR_CONSEC_FRAMES = 120        # 4 seconds

📊 Test Results

Threshold Sensitivity Analysis

Tested 50 different thresholds from 0.12 to 0.25:

Threshold Precision Recall F1-Score
0.14 96% 78% 86%
0.15 94% 84% 89%
0.16 92% 88% 90%
0.17 88% 91% 89%
0.18 82% 94% 88%

Detection Window Analysis

Impact of consecutive frame requirement:

Window Precision False Alarms Trade-off
1.0s (30f) 78% High Fast but noisy
2.0s (60f) 86% Medium Balanced
3.0s (90f) 92% Low Optimal
4.0s (120f) 95% Very Low Slow response
5.0s (150f) 97% Minimal Too slow

Conclusion: 3-second window provides optimal balance between precision and response time.

💼 Use Cases

1. Commercial Transportation

  • Truck drivers on long hauls
  • Bus drivers on scheduled routes
  • Taxi/ride-share drivers

2. Personal Vehicles

  • Long-distance travel
  • Night driving
  • Commuters with irregular sleep

3. Industrial Applications

  • Heavy machinery operators
  • Train conductors
  • Security personnel

🚗 Deployment Scenarios

Embedded System (Raspberry Pi)

# Optimize for lower-end hardware
config.FRAME_WIDTH = 320
config.FRAME_HEIGHT = 240
config.TARGET_FPS = 15

Cloud-based Monitoring

# Stream detection events to server
def send_alert(driver_id, timestamp, ear_value):
    # API call to monitoring system
    pass

Mobile Application

  • Use phone's front camera
  • Bluetooth audio alerts
  • GPS logging of drowsy events

📝 Code Highlights

EAR Calculation

def calculate_ear(self, eye_points):
    # Vertical eye landmarks
    A = distance.euclidean(eye_points[1], eye_points[5])
    B = distance.euclidean(eye_points[2], eye_points[4])
    
    # Horizontal eye landmark
    C = distance.euclidean(eye_points[0], eye_points[3])
    
    # Calculate EAR
    ear = (A + B) / (2.0 * C)
    return ear

Drowsiness Detection Logic

def detect_drowsiness(self, ear_value, current_time):
    if ear_value < self.config.EAR_THRESHOLD:
        self.drowsy_frame_count += 1
        
        # 3-second threshold
        if self.drowsy_frame_count >= self.config.EAR_CONSEC_FRAMES:
            if self.current_state == "ALERT":
                self.current_state = "DROWSY"
            
            # Trigger alarm with cooldown
            if (current_time - self.last_alarm_time) > self.config.ALARM_COOLDOWN:
                self.trigger_alarm()
                self.last_alarm_time = current_time
            
            return "DROWSY"
    else:
        # False alarm prevention
        self.drowsy_frame_count = 0
        self.current_state = "ALERT"
    
    return "ALERT"

Result

detection_analysis

🎓 Learning Outcomes

This project demonstrates:

  • Computer Vision: Real-time video processing with OpenCV
  • Facial Landmark Detection: dlib 68-point model
  • Algorithm Development: EAR algorithm implementation
  • Binary Classification: State-based detection logic
  • Signal Processing: Consecutive frame filtering
  • Performance Optimization: 30 FPS real-time processing
  • System Calibration: Adaptive threshold tuning
  • Audio Integration: Pygame alarm system

🤝 Contributing

Potential enhancements:

  • Head pose estimation (nodding detection)
  • Mouth opening analysis (yawning detection)
  • Steering wheel monitoring
  • Mobile app development
  • Cloud-based fleet monitoring
  • Multi-face detection for passengers
  • Integration with vehicle systems

📞 Contact

For questions or feedback about this project, please reach out through GitHub issues.

📄 License

This project is open source and available for educational purposes.

🙏 Acknowledgments

  • dlib Library: Davis King for facial landmark detection
  • OpenCV: Computer vision foundation
  • Research Papers:
    • Soukupová and Čech (2016) - "Real-Time Eye Blink Detection using Facial Landmarks"
    • Tereza Soukupová PhD Thesis on Driver Drowsiness Detection

📚 References

  1. Soukupová, T., & Čech, J. (2016). "Real-time eye blink detection using facial landmarks." In Proceedings of the 21st computer vision winter workshop.

  2. Dewi, C., Chen, R. C., & Jiang, X. (2020). "Deep Convolutional Neural Network for Enhancing Traffic Sign Recognition Developed on Yolo V4." Multimedia Tools and Applications.

  3. dlib Documentation: http://dlib.net/

  4. OpenCV Documentation: https://docs.opencv.org/


Note: This system is designed for demonstration and research purposes. For production deployment in vehicles, additional safety features, redundancy, and regulatory compliance are required.


About

Driver Drowsiness Detection System is an AI-based safety solution that monitors a driver’s eye movements and facial features in real time using a camera and computer vision. It detects signs of fatigue like eye closure and yawning, and triggers alerts to prevent accidents and improve road safety.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages