Skip to content

PRERITARYA/driver-drowsiness-iot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Driver Drowsiness Detection โ€” IoT (Raspberry Pi)

Real-time driver drowsiness detection system running on Raspberry Pi 4 using Eye Aspect Ratio (EAR) algorithm with OpenCV and facial landmark detection.


๐Ÿ“Œ Table of Contents


๐Ÿ“– About the Project

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)

โš™๏ธ How It Works

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

Eye Aspect Ratio (EAR) Formula

       |p2-p6| + |p3-p5|
EAR = โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
           2 * |p1-p4|
  • EAR ~0.25โ€“0.30 โ†’ Eyes open (normal)
  • EAR < 0.22 consistently โ†’ Eyes closing (drowsy)

๐Ÿ”ง Hardware Required

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)

๐Ÿ“ Project Structure

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

๐Ÿš€ Installation

Prerequisites

  • Raspberry Pi 4 running Raspberry Pi OS (64-bit recommended)
  • Python 3.10+
  • Internet connection for first-time setup

Step 1: Clone the Repository

git clone https://github.com/PRERITARYA/driver-drowsiness-iot.git
cd driver-drowsiness-iot

Step 2: Create Virtual Environment

python -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Download the LBF Model

The 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.yaml

๐Ÿ“ก WiFi Streaming Mode (For Testing)

If your Pi camera is unavailable, you can stream your laptop webcam to the Pi over WiFi.

On Your Laptop:

# Install dependencies
pip install flask opencv-python

# Run the stream server
python streaming/stream_server.py

Note your laptop's IP address:

ipconfig   # Windows
ifconfig   # Linux/Mac

On Raspberry Pi:

Edit 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.


โ–ถ๏ธ Usage

Quick Run (using script)

chmod +x run.sh
./run.sh

Manual Run

source venv/bin/activate
cd app
python drowsy_detection.py

Expected Output

loading 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.


๐Ÿ”ง Configuration

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

๐Ÿ› ๏ธ Tech Stack

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

๐Ÿ“ฆ Requirements

opencv-contrib-python==4.12.0.88
scipy
pygame
numpy
flask

๐Ÿ”ฎ Future Improvements

  • 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

๐Ÿ‘ค Author

Prerit Arya

โญ If you found this project helpful, please give it a star on GitHub!

About

Real-time driver drowsiness detection on Raspberry Pi 4

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages