Skip to content

PRERITARYA/Driver-Drowsiness-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


"Every 25 seconds, a person dies in a road accident. Drowsy driving is a leading cause."

A real-time computer vision system that watches your eyes so you don't have to β€” detecting drowsiness the moment it starts, and sounding the alarm before it's too late.


πŸ› Report Bug Β Β·Β  ✨ Request Feature


🎯 What It Does

This application uses your webcam to monitor your eyes in real time. Using MediaPipe FaceMesh to track 478 facial landmarks and the Eye Aspect Ratio (EAR) algorithm to measure eye openness β€” the moment your eyes stay closed too long, a loud alarm fires to wake you up.

No cloud. No lag. No subscriptions. Just pure computer vision running locally in your browser via Streamlit + WebRTC.


✨ Features

πŸ‘οΈ Real-Time Eye Tracking

  • Detects 478 facial landmarks using MediaPipe FaceMesh
  • Tracks 6 precise landmarks per eye for accurate EAR computation
  • Renders live green/red eye landmark overlays directly on the video feed

πŸ“ Eye Aspect Ratio (EAR) Algorithm

  • Mathematically computes eye openness using the ratio of vertical to horizontal eye distances
  • Displays live EAR score on the video frame
  • Tracks cumulative drowsy time in seconds with millisecond precision

🚨 Intelligent Alarm System

  • Alarm triggers only when EAR stays below threshold for a configurable duration β€” no false positives from a single blink
  • Uses simpleaudio for instant .wav audio playback
  • Alarm stops automatically the moment alertness is detected

πŸŽ›οΈ Fully Adjustable Thresholds

  • EAR Threshold slider (0.0 – 0.4) β€” tune sensitivity to your eye shape
  • Wait Time slider (0.0 – 5.0s) β€” control how long before the alarm fires

🌐 Browser-Based Live Video via WebRTC

  • No need to install a desktop app β€” runs entirely in the browser
  • Uses streamlit-webrtc with Google STUN + OpenRelay TURN servers for reliable peer connections
  • Works on Windows, Mac, and Linux

🧠 How It Works

Webcam Feed (BGR)
      β”‚
      β–Ό
MediaPipe FaceMesh  ──►  478 Facial Landmarks Detected
      β”‚
      β–Ό
Extract 6 Eye Landmarks (Left + Right)
      β”‚
      β–Ό
Calculate EAR = (P2–P6 + P3–P5) / (2 Γ— P1–P4)
      β”‚
      β”œβ”€β”€ EAR β‰₯ Threshold  ──►  Eyes Open  β†’  Reset Timer  β†’  GREEN overlay
      β”‚
      └── EAR < Threshold  ──►  Eyes Closed  β†’  Start Timer
                                      β”‚
                                      └── Timer β‰₯ Wait Time  β†’  🚨 ALARM  β†’  RED overlay

EAR Formula

        |P2-P6| + |P3-P5|
EAR  =  ─────────────────
             2 |P1-P4|

Where P1–P6 are the 6 eye landmark coordinates. A fully open eye has EAR β‰ˆ 0.25–0.30. A closed eye drops below 0.18 (default threshold).


πŸ—οΈ Project Structure

Driver-Drowsiness-Detection/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ streamlit_app.py        # 🌐 Main Streamlit UI + WebRTC streamer
β”‚   β”œβ”€β”€ drowsy_detection.py     # 🧠 Core EAR logic + MediaPipe FaceMesh
β”‚   └── video_processor.py      # πŸŽ₯ WebRTC VideoProcessor wrapper
β”‚
β”œβ”€β”€ audio/
β”‚   └── wake_up.wav             # πŸ”Š Alarm sound file
β”‚
β”œβ”€β”€ requirements.txt            # πŸ“¦ All dependencies
└── README.md

πŸ› οΈ Tech Stack

Component Technology
UI Framework Streamlit 1.29.0
Live Video streamlit-webrtc 0.47.1 + WebRTC
Face Landmark Detection MediaPipe FaceMesh
Computer Vision OpenCV 4.8.1
Video Frame Processing PyAV (av 10.0.0)
Alarm Audio simpleaudio 1.0.4
Numerical Computing NumPy 1.26.4
Language Python 3.10+

βš™οΈ Installation

1. Clone the repository

git clone https://github.com/PRERITARYA/Driver-Drowsiness-Detection.git
cd Driver-Drowsiness-Detection

2. Create a virtual environment

python -m venv venv

Activate it:

# Windows
venv\Scripts\activate

# Mac / Linux
source venv/bin/activate

3. Install dependencies

pip install -r requirements.txt

4. Run the app

streamlit run app/streamlit_app.py

Open your browser at http://localhost:8501, allow camera permission, and press START. 🎬


πŸŽ›οΈ Configuring Thresholds

Once the app is running, use the two sliders in the sidebar to tune detection:

Slider Default What it controls
EAR Threshold 0.18 How closed the eyes must be to count as drowsy. Lower = less sensitive.
Wait Time 1.0s How many seconds of closed eyes before the alarm fires.

πŸ“¦ Requirements

streamlit==1.29.0
streamlit-webrtc==0.47.1
opencv-python==4.8.1.78
mediapipe==0.10.9
av==10.0.0
numpy==1.26.4
simpleaudio==1.0.4
pyarrow==14.0.2

⚠️ Important Notes

  • Alarm sound plays on the machine where Streamlit is running (local dev). If deployed to a cloud server, the alarm won't play in the browser without additional client-side audio handling.
  • Camera access must be granted in the browser for WebRTC to work.
  • Lighting matters β€” ensure your face is well-lit for optimal landmark detection accuracy.
  • MediaPipe FaceMesh works best with a single face in the frame (max_num_faces=1).

πŸš€ Use Cases

πŸ‘€ Who πŸ’‘ How
Long-haul truck drivers Mount a laptop/tablet running the app for continuous monitoring
Night shift drivers Detect micro-sleeps before they become fatal
Researchers Use EAR data for fatigue and attention studies
Fleet operators Integrate into driver monitoring systems

πŸ‘¨β€πŸ’» Author

Prerit Arya B.Tech Computer Science & Engineering

GitHub


πŸ™ Acknowledgements

  • Core EAR algorithm adapted from the LearnOpenCV tutorial on driver drowsiness detection
  • Face landmark detection powered by Google MediaPipe
  • WebRTC streaming via streamlit-webrtc by Yuichiro Tachibana

⭐ If this project helped you, please give it a star!

Stay alert. Stay safe. πŸš—πŸ’¨

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages