This project implements a drowsiness detection system using OpenCV, dlib, and playsound. The system detects a user's face and monitors their eye aspect ratio (EAR) to determine if they are drowsy. If the EAR falls below a predefined threshold for a certain number of frames, an alert message is displayed, and an alarm sound is played to wake up the user.
- Real-time face and eye detection
- Eye Aspect Ratio (EAR) calculation to detect drowsiness
- Visual alert message displayed on the screen
- Audio alarm when drowsiness is detected
Ensure you have the following dependencies installed before running the script:
pip install opencv-python imutils dlib playsoundAdditionally, you need to download the pre-trained dlib facial landmark model:
Extract and place the file in the models/ directory of your project.
- Clone the repository:
git clone https://github.com/yourusername/drowsiness-detection.git- Navigate to the project directory:
cd drowsiness-detection- Install required dependencies:
pip install -r requirements.txtRun the script to start the drowsiness detection system:
python drowsiness_detection.pyeye_aspect_ratio(eye): Computes the EAR to determine if eyes are closed.detect: Uses dlib's frontal face detector.predict: Uses dlib's landmark predictor to identify facial landmarks.playsound(): Triggers an alarm sound if drowsiness is detected.
- Press
qto exit the program.
You can modify the following parameters to fine-tune detection:
thresh = 0.25(Eye Aspect Ratio threshold for drowsiness detection)frame_check = 20(Number of consecutive frames required to trigger an alert)
- Ensure your webcam is properly connected.
- If the alarm sound does not play, check the path to the
.mp3file and verify thatplaysoundis installed. - If
dlibinstallation fails, ensure you have CMake installed and use:pip install dlib --verbose
This project is open-source and available under the MIT License.
- OpenCV and dlib for facial landmark detection
- Imutils for image processing helpers