Real-time driver drowsiness detection system running on Raspberry Pi 4 using Eye Aspect Ratio (EAR) algorithm with OpenCV and facial landmark detection.
- About the Project
- How It Works
- Hardware Required
- Project Structure
- Installation
- WiFi Streaming Mode
- Usage
- Configuration
- Tech Stack
- Author
This project implements a real-time drowsiness detection system designed as an IoT device using Raspberry Pi 4. It monitors the driver's eyes using a camera, calculates the Eye Aspect Ratio (EAR) in real time, and triggers an audio alarm when drowsiness is detected.
Unlike most implementations that require powerful GPUs, this system is optimized to run efficiently on resource-constrained embedded hardware (Raspberry Pi 4).
Key Highlights:
- Runs entirely on Raspberry Pi 4 โ no cloud, no GPU required
- Real-time face and landmark detection using OpenCV LBF model
- Audio alarm via pygame when drowsiness is detected
- Supports both Pi Camera and WiFi-based laptop camera streaming (for testing)
Camera Feed
โ
Face Detection (Haar Cascade)
โ
Facial Landmark Detection (LBF Model โ 68 landmarks)
โ
Eye Aspect Ratio (EAR) Calculation
โ
EAR < 0.22 for 17 consecutive frames?
โ YES
๐ ALARM TRIGGERED
|p2-p6| + |p3-p5|
EAR = โโโโโโโโโโโโโโโโโโโโโ
2 * |p1-p4|
- EAR ~0.25โ0.30 โ Eyes open (normal)
- EAR < 0.22 consistently โ Eyes closing (drowsy)
| Component | Details |
|---|---|
| Raspberry Pi 4 | Model B, 4GB RAM recommended |
| Pi Camera Module | Or USB webcam |
| Speaker / Buzzer | For alarm output |
| Power Supply | 5V 3A USB-C |
| MicroSD Card | 16GB+ (Class 10) |
driver-drowsiness-iot/
โ
โโโ app/
โ โโโ drowsy_detection.py # Main detection script
โ โโโ alarm.wav # Alarm sound file
โ โโโ lbfmodel.yaml # Face landmark model (download separately)
โ
โโโ streaming/
โ โโโ stream_server.py # Laptop webcam stream server (for testing)
โ
โโโ assets/
โ โโโ demo.png # Demo screenshot
โ
โโโ run.sh # Quick run script
โโโ .gitignore
โโโ requirements.txt
โโโ README.md
- Raspberry Pi 4 running Raspberry Pi OS (64-bit recommended)
- Python 3.10+
- Internet connection for first-time setup
git clone https://github.com/PRERITARYA/driver-drowsiness-iot.git
cd driver-drowsiness-iotpython -m venv venv
source venv/bin/activatepip install -r requirements.txtThe lbfmodel.yaml file is too large (55MB) to be stored in the repository. Download it from the Releases page and place it inside the app/ folder:
# Place the downloaded file here:
app/lbfmodel.yamlIf your Pi camera is unavailable, you can stream your laptop webcam to the Pi over WiFi.
# Install dependencies
pip install flask opencv-python
# Run the stream server
python streaming/stream_server.pyNote your laptop's IP address:
ipconfig # Windows
ifconfig # Linux/MacEdit app/drowsy_detection.py and update this line:
# Replace this:
cap = cv2.VideoCapture(0)
# With this (use your laptop's IP):
cap = cv2.VideoCapture("http://YOUR_LAPTOP_IP:5000/video")Make sure both devices are on the same WiFi network.
chmod +x run.sh
./run.shsource venv/bin/activate
cd app
python drowsy_detection.pyloading data from : /home/pi/AI_Project/lbfmodel.yaml
EAR: 0.28 | Counter: 0 | Alarm: False โ Eyes open
EAR: 0.21 | Counter: 1 | Alarm: False โ Eyes closing
EAR: 0.10 | Counter: 17 | Alarm: True โ ALARM TRIGGERED
Press ESC to exit.
You can tune these values in drowsy_detection.py:
| Parameter | Default | Description |
|---|---|---|
EAR_THRESHOLD |
0.22 |
EAR value below which eye is considered closed |
EAR_CONSEC_FRAMES |
17 |
Consecutive frames before alarm triggers |
| Frame Width | 320 |
Camera frame width (lower = faster on Pi) |
| Frame Height | 240 |
Camera frame height |
| Technology | Purpose |
|---|---|
| Python 3.13 | Core language |
| OpenCV 4.12 | Face detection & landmark detection |
| SciPy | Euclidean distance for EAR calculation |
| Pygame | Audio alarm playback |
| Flask | WiFi camera streaming server |
| Raspberry Pi 4 | Embedded IoT hardware |
opencv-contrib-python==4.12.0.88
scipy
pygame
numpy
flask
- Upgrade to MediaPipe for 468 landmarks
- Add yawn detection (MAR โ Mouth Aspect Ratio)
- Add head pose estimation (nodding detection)
- Adaptive EAR threshold per driver calibration
- GPIO buzzer integration instead of speaker
- Web dashboard for monitoring
Prerit Arya
- GitHub: @PRERITARYA
โญ If you found this project helpful, please give it a star on GitHub!