"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.
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.
- 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
- 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
- Alarm triggers only when EAR stays below threshold for a configurable duration β no false positives from a single blink
- Uses
simpleaudiofor instant.wavaudio playback - Alarm stops automatically the moment alertness is detected
- 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
- No need to install a desktop app β runs entirely in the browser
- Uses
streamlit-webrtcwith Google STUN + OpenRelay TURN servers for reliable peer connections - Works on Windows, Mac, and Linux
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
|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).
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
| 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+ |
git clone https://github.com/PRERITARYA/Driver-Drowsiness-Detection.git
cd Driver-Drowsiness-Detectionpython -m venv venvActivate it:
# Windows
venv\Scripts\activate
# Mac / Linux
source venv/bin/activatepip install -r requirements.txtstreamlit run app/streamlit_app.pyOpen your browser at http://localhost:8501, allow camera permission, and press START. π¬
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. |
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- 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).
| π€ 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 |
- 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