Skip to content

ankit-cybertron/Drowsiness-Detection-Alarm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Drowsiness Detection Alarm System

Drowsiness Detection System Demo

Python OpenCV dlib

A real-time computer vision system that monitors driver drowsiness using facial landmark detection and triggers an audio alarm when drowsiness is detected. This project uses OpenCV, dlib, and machine learning to detect eye closure patterns and prevent accidents caused by driver fatigue.


πŸ“– About

Driver drowsiness is a major cause of road accidents worldwide. This system provides a non-invasive, camera-based solution to detect drowsiness in real-time by analyzing the driver's eye aspect ratio (EAR) and triggering an alarm when prolonged eye closure is detected.

System Workflow

System Workflow Diagram

How It Works

  1. Face Detection: Uses dlib's HOG-based face detector to locate the face in each frame
  2. Facial Landmark Detection: Identifies 68 facial landmarks, focusing on eye regions (landmarks 36-47)
  3. Eye Aspect Ratio (EAR): Calculates the ratio between vertical and horizontal eye distances
  4. Drowsiness Classification: Monitors EAR values over consecutive frames to classify states:
    • Active (eyes open)
    • Drowsy (eyes partially closed)
    • Sleeping (eyes fully closed for extended period)
  5. Alarm Trigger: Plays audio alert when sleeping state is detected

πŸ“ Project Structure

Drowsiness Detection Alarm/
β”‚
β”œβ”€β”€ src/                            # Source code directory
β”‚   β”œβ”€β”€ Sleep_Detect_Alarm.py      # Main script with alarm functionality
β”‚   └── test.py                    # Test script (no alarm, for debugging)
β”‚
β”œβ”€β”€ assets/                         # Assets directory
β”‚   β”œβ”€β”€ images/                    # Project images & diagrams
β”‚   β”‚   β”œβ”€β”€ demo_preview.png      # Demo simulation screenshot
β”‚   β”‚   β”œβ”€β”€ system_workflow.png   # Flowchart of the system
β”‚   β”‚   └── facial_landmarks.png  # Facial landmark explanation
β”‚   β”‚
β”‚   β”œβ”€β”€ Alert.mp3                  # Audio alarm file
β”‚   └── shape_predictor_68_face_landmarks.dat  # Pre-trained facial landmark model
β”‚
β”œβ”€β”€ nk.txt                          # Notes/configuration file
└── README.MD                       # This file

File Descriptions

Python Scripts (src/)

  • Sleep_Detect_Alarm.py: Production-ready script with audio alarm (uses threading for non-blocking audio)
  • test.py: Testing version without audio alarm for debugging and development

Assets (assets/)

  • images/: Contains visual documentation and diagrams created for this project
  • shape_predictor_68_face_landmarks.dat: Pre-trained dlib model for detecting 68 facial landmarks
  • Alert.mp3: Audio file played when drowsiness is detected

✨ Features

  • βœ… Real-time face detection using dlib's frontal face detector
  • βœ… 68-point facial landmark tracking for precise eye monitoring
  • βœ… Eye Aspect Ratio (EAR) calculation for drowsiness detection
  • βœ… Three-state classification: Active, Drowsy, Sleeping
  • βœ… Audio alarm system with threaded playback (non-blocking)
  • βœ… Visual feedback with color-coded status display
  • βœ… Live webcam feed with overlay visualization
  • βœ… Adjustable sensitivity through threshold parameters

πŸ›  Requirements

Python Version

  • Python 3.7 or higher

Libraries

opencv-python       # Computer vision operations
numpy              # Numerical computations
dlib               # Face detection & landmark prediction
imutils            # Image processing utilities
playsound          # Audio playback for alarm

Additional Requirements

  • Webcam: Built-in or USB camera
  • Pre-trained Model: shape_predictor_68_face_landmarks.dat (included in project)

πŸš€ Installation

Step 1: Clone the Repository

git clone <repository-url>
cd "Drowsiness Detection Alarm"

Step 2: Install Dependencies

# Install required Python packages
pip install opencv-python numpy dlib imutils playsound

# On macOS, you might need:
pip install pyobjc-framework-AVFoundation  # For playsound

Step 3: Download Pre-trained Model

The shape_predictor_68_face_landmarks.dat file is already included in this repository. If missing, download it from:

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

Extract and place in the project root directory.


πŸ’» Usage

Running the Main Application

python src/Sleep_Detect_Alarm.py

Running the Test Version (No Alarm)

python src/test.py

Controls

  • ESC key: Exit the application
  • Two windows will appear:
    • Frame: Shows status overlay (Active/Drowsy/Sleeping)
    • Detection: Shows facial landmarks and face bounding box

βš™οΈ Configuration

Adjusting File Paths

If you move the model or audio file, update these lines in the script:

# Line 13 in src/Sleep_Detect_Alarm.py
predictor = dlib.shape_predictor("assets/shape_predictor_68_face_landmarks.dat")

# Line 25 in src/Sleep_Detect_Alarm.py  
playsound('assets/Alert.mp3')

Tweaking Detection Thresholds

Eye Aspect Ratio (EAR) Thresholds

# In blinked() function (lines 30-41)
if(ratio > 0.25):       # Eyes open
    return 2
elif(ratio > 0.21):     # Eyes partially closed
    return 1
else:                   # Eyes closed
    return 0

Frame Count Thresholds

# Sleeping detection (line 68)
if(sleep > 8):          # Trigger alarm after 8 frames of closed eyes

# Drowsy detection (line 78)
if(drowsy > 6):         # Show drowsy warning after 6 frames

Note: Higher values = less sensitive (fewer false alarms, but slower detection)


🧠 How Eye Aspect Ratio (EAR) Works

The Eye Aspect Ratio is calculated using 6 facial landmarks per eye:

Facial Landmarks Diagram

     p2    p3
   p1  β€’β€’β€’β€’β€’  p4
     p5    p6

EAR = (||p2 - p6|| + ||p3 - p5||) / (2 * ||p1 - p4||)
  • High EAR (> 0.25): Eyes are open β†’ Active
  • Medium EAR (0.21 - 0.25): Eyes partially closed β†’ Drowsy
  • Low EAR (< 0.21): Eyes closed β†’ Sleeping

When EAR remains low for consecutive frames, the alarm is triggered.


🎯 Technical Details

Facial Landmarks Used

  • Left Eye: Landmarks 36-41
  • Right Eye: Landmarks 42-47

Detection States

State Condition Color Code Action
Active Both eyes open Blue Normal monitoring
Drowsy Eyes partially closed (6+ frames) Red Visual warning
Sleeping Eyes fully closed (8+ frames) Blue Audio alarm + visual alert

Threading Implementation

The alarm uses Python's threading module to play audio without blocking the main detection loop:

t = threading.Thread(target=alert_sound)
t.start()

πŸ”§ Troubleshooting

Camera Not Working

# Change camera index in line 9
cap = cv2.VideoCapture(0)  # Try 1, 2, etc. if 0 doesn't work

Model File Error

Error: Unable to open shape_predictor_68_face_landmarks.dat

Solution: Ensure the .dat file is in the correct location and update the path in line 13

Audio Not Playing

  • macOS: Install pyobjc-framework-AVFoundation
  • Linux: Use python3-gst-1.0 or sox
  • Windows: Should work out of the box with playsound

Low FPS / Laggy Detection

  • Reduce video resolution
  • Use a lighter face detection model
  • Increase frame skip rate

🚦 Differences Between Scripts

Feature Sleep_Detect_Alarm test
Audio Alarm βœ… ❌
Threading βœ… ❌
Full Status Display βœ… βœ…
Facial Landmarks Display βœ… βœ…
Purpose Production Debug

πŸ“ˆ Future Improvements

  • Add mobile app integration for remote monitoring
  • Implement yawning detection using mouth landmarks
  • Add data logging and drowsiness pattern analysis
  • Support for multiple face detection (passenger monitoring)
  • Integration with vehicle systems (steering, speed, etc.)
  • Deep learning model for improved accuracy (CNN/LSTM)
  • Head pose estimation for distraction detection
  • Cloud-based alert system for fleet management

πŸ“š References


🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


πŸ“„ License

This project is open-source and available under the MIT License.


πŸ‘€ Author

Ankit Kumar Tiwari
B.Tech – Electronics & Communication Engineering


⚠️ Disclaimer

This system is intended as a supplementary safety tool and should NOT be relied upon as the sole means of preventing drowsy driving. Always ensure adequate rest before driving long distances.

About

A real-time driver drowsiness detection system using OpenCV and Dlib. Monitors eye aspect ratio (EAR) to detect fatigue and trigger audio alarms.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages