A real-time drowsiness detection system designed for Raspberry Pi that monitors driver alertness using computer vision and machine learning. The system uses facial landmark detection to calculate Eye Aspect Ratio (EAR) and triggers an audible alarm when drowsiness is detected.
This IoT-based drowsiness detection system captures live video feed from a Raspberry Pi camera, analyzes facial features to detect eye closure patterns, and activates a buzzer alarm when drowsiness is detected. The system is designed to help prevent accidents caused by driver fatigue.
- Real-time face detection and tracking
- Eye Aspect Ratio (EAR) calculation for drowsiness detection
- Audible alarm system using buzzer
- Visual feedback with on-screen alerts
- Drowsiness event logging
- Configurable sensitivity thresholds
- Histogram equalization for improved detection in varying lighting conditions
- Raspberry Pi (any model with GPIO support)
- Raspberry Pi Camera Module (PiCamera2 compatible)
- Buzzer connected to GPIO pin 17
- Monitor or display for visual output
- Python 3.11
- Raspberry Pi OS (or compatible Linux distribution)
The project requires the following Python packages:
- opencv-python (4.11.0.86)
- dlib (19.24.8)
- imutils (0.5.4)
- scipy (1.15.2)
- RPi.GPIO (0.7.1)
- picamera2
- numpy
cd /path/to/project/Drowsiness_DetectionA virtual environment is already included in the myenv directory. To activate it:
source myenv/bin/activateAlternatively, create a new virtual environment:
python3 -m venv myenv
source myenv/bin/activatepip install opencv-python
pip install dlib
pip install imutils
pip install scipy
pip install RPi.GPIO
pip install picamera2The system requires the dlib 68-point facial landmark predictor model. Ensure the following file is present in the models directory:
models/shape_predictor_68_face_landmarks.dat
If not present, download it from:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
Extract and place in the models directory.
Connect the buzzer to Raspberry Pi GPIO:
- Buzzer positive terminal: GPIO 17 (BCM numbering)
- Buzzer negative terminal: GND
Ensure the Raspberry Pi Camera Module is properly connected and enabled:
sudo raspi-config
# Navigate to Interface Options -> Camera -> Enablepython drowsiness_detector.pyThe system will:
- Initialize the camera (2-second warm-up period)
- Load the facial landmark predictor
- Start real-time video processing
- Display live video feed with detection overlays
- Trigger buzzer alarm when drowsiness is detected
Press q key in the video window or use Ctrl+C to stop the system gracefully.
To verify camera functionality:
python test.pyThis will capture a test frame and display its dimensions.
The system uses the Eye Aspect Ratio algorithm to detect eye closure:
- Face Detection: Uses dlib's frontal face detector to locate faces in the frame
- Landmark Detection: Identifies 68 facial landmarks, including eye coordinates
- EAR Calculation: Computes the ratio between eye height and width
- Formula:
EAR = (||p2-p6|| + ||p3-p5||) / (2 * ||p1-p4||) - Where p1-p6 are the eye landmark points
- Formula:
- Drowsiness Detection: When EAR falls below threshold for consecutive frames, drowsiness is detected
- EAR Threshold: 0.24 (configurable in code)
- Consecutive Frames: 3 frames (configurable in code)
- Camera Resolution: 640x480 pixels
- Display Resolution: 1280x720 pixels (upscaled)
- Buzzer activates when eyes remain closed for 3 consecutive frames
- Buzzer deactivates when eyes reopen
- Alarm also deactivates if no face is detected
Drowsiness_Detection/
├── drowsiness_detector.py # Main drowsiness detection script
├── drowsiness_detection.ipynb # Jupyter notebook for development/testing
├── test.py # Camera testing script
├── drowsiness_log.csv # Log file for drowsiness events
├── README.md # This file
├── assets/ # Sample eye images
│ ├── eye1.jpg
│ ├── eye2.png
│ └── eye3.jpg
├── models/ # Machine learning models
│ └── shape_predictor_68_face_landmarks.dat
└── myenv/ # Python virtual environment
The system logs drowsiness events to drowsiness_log.csv with the following format:
timestamp,ear_value,alarm_status
2025-05-08 14:44:46.477,0.1664,0- timestamp: Date and time of the event
- ear_value: Calculated Eye Aspect Ratio
- alarm_status: 0 (off) or 1 (on)
Modify these constants in drowsiness_detector.py:
EYE_AR_THRESH = 0.24 # Lower = more sensitive
EYE_AR_CONSEC_FRAMES = 3 # Higher = less sensitiveModify the buzzer pin number:
BUZZER_PIN = 17 # Change to your GPIO pinAdjust camera configuration:
config = cam.create_preview_configuration(
main={"format": 'BGR888', "size": (640, 480)}
)- System processes frames in real-time
- Histogram equalization improves detection in varying light
- Face detection uses single-scale processing (parameter 0)
- Display resolution upscaled for better visibility
- Requires clear view of the face
- Performance depends on lighting conditions
- Single face detection (processes first detected face)
- Requires Raspberry Pi hardware with GPIO
- Not suitable for multiple simultaneous users
- Multi-face detection support
- Cloud-based logging and analytics
- Mobile app integration for alerts
- Head pose estimation for attention tracking
- Night vision support with IR camera
- Yawn detection algorithm
- Integration with vehicle systems
This system is designed as an educational project and assistive tool. It should not be relied upon as the sole safety mechanism for preventing drowsy driving. Always ensure adequate rest before operating vehicles or machinery.
This project is provided as-is for educational and research purposes.
The system uses dlib's 68-point facial landmark model:
- Points 36-41: Left eye
- Points 42-47: Right eye
- Capture frame from PiCamera2
- Convert to grayscale
- Apply histogram equalization
- Detect faces using dlib
- Extract facial landmarks
- Calculate EAR for both eyes
- Average EAR values
- Compare against threshold
- Track consecutive closed frames
- Trigger alarm if threshold exceeded
- Display annotated frame
For issues, questions, or contributions, please refer to the project repository or documentation.
- dlib library for facial landmark detection
- OpenCV for computer vision operations
- Raspberry Pi Foundation for hardware platform
- Eye Aspect Ratio algorithm based on computer vision research