This project implements a real-time drowsiness detection system using OpenCV and MediaPipe's FaceMesh module. It can be applied in scenarios like monitoring driver alertness during long journeys or ensuring attentiveness in security and surveillance roles. The system monitors a user's eye aspect ratio (EAR) to detect signs of drowsiness and triggers an alert when the eyes remain closed for a specified duration.
- Real-time detection of facial landmarks using MediaPipe FaceMesh.
- Calculation of Eye Aspect Ratio (EAR) to monitor eye closure.
- Drowsiness detection with customizable thresholds.
- Visual feedback on the detected EAR and drowsiness status.
- Audio alert triggered during prolonged eye closure.
- Python 3.7 or higher
- OpenCV
- MediaPipe
- NumPy
- A computer with a webcam
-
🛠️ Clone the repository:
git clone https://github.com/<your-username>/drowsiness-detection.git cd drowsiness-detection
-
📦 Install required Python packages:
pip install opencv-python mediapipe numpy
-
(Optional) 🎵 Ensure your system supports the audio alert command
play
by installing thesox
package. On Ubuntu, you can install it using:sudo apt-get install sox
-
▶️ Run the script:python drowsiness_detection.py
-
The webcam will start, and the system will begin analyzing facial landmarks in real-time.
-
If drowsiness is detected (both eyes remain closed for the defined
WAIT_TIME
seconds), a visual alert will appear on the screen, and an audio alert will play. -
Press
q
to quit the application.
- EAR_THRESH: Adjust this value to set the threshold for detecting closed eyes. Default is
0.25
. - WAIT_TIME: Set the time (in seconds) for which eyes must remain closed to trigger the alert. Default is
2
seconds. - duration: Define the duration of the audio alert in seconds. Default is
0.3
seconds. - freq: Set the frequency of the audio alert tone in Hz. Default is
580
Hz.
EAR is calculated using specific facial landmarks for the eyes. It is an effective metric for detecting drowsiness because it measures the proportion of eye openness, providing a reliable indicator of whether the eyes are closing or remaining shut.
Where p1
to p6
are the coordinates of specific eye landmarks.
- Capture video frames from the webcam.
- Use MediaPipe FaceMesh to detect facial landmarks.
- Calculate EAR for both eyes.
- Monitor EAR and check if it is below the threshold.
- If eyes remain closed for
WAIT_TIME
seconds, trigger an alert.
- EAR value is displayed in real-time on the video feed.
- Visual feedback highlights eye landmarks and alerts if drowsiness is detected.
- Performance may vary in poor lighting conditions.
- May not work accurately for individuals wearing glasses or with unusual eye shapes.
- Adaptive thresholding based on individual eye characteristics.
- Integration with additional sensors for enhanced accuracy.
This project is licensed under the MIT License. See the LICENSE file for details.
- MediaPipe for providing a powerful FaceMesh solution.
- OpenCV for real-time computer vision capabilities.
Feel free to contribute and enhance this project! Submit issues or pull requests on the GitHub repository.