A real-time computer vision application that detects driver drowsiness using facial landmarks and eye aspect ratio analysis. When drowsiness is detected, the system triggers an audio alarm to alert the driver.
- Real-time eye detection using webcam
- Drowsiness detection based on eye aspect ratio (EAR)
- Audio alarm when driver shows signs of drowsiness
- Visual indicators (color-coded eye rectangles)
- Adjustable sensitivity thresholds
Before running this project, ensure you have:
- Python 3.7+ installed on your system
- A working webcam
- An audio file named
alarm.mp3in the project directory
cd Sleep-DetectorRun the following command to install all required Python packages:
pip install opencv-python dlib scipy imutils pygamePackage Descriptions:
opencv-python- Computer vision library for video capture and image processingdlib- Machine learning library for facial landmark detectionscipy- Scientific computing library (for distance calculations)imutils- OpenCV convenience functionspygame- For playing audio alarms
Download the shape_predictor_68_face_landmarks.dat file from:
Installation steps:
- Download and extract the
.bz2file - Place the
shape_predictor_68_face_landmarks.datfile in your project directory - Update the path in
main.pyif needed (currently set to:C:\Users\yadav\OneDrive\Desktop\day 17\shape_predictor_68_face_landmarks.dat)
Add an audio file named alarm.mp3 to the project directory. This will play when drowsiness is detected.
You can adjust the following parameters in main.py:
ALARM_FILE = "alarm.mp3" # Path to your alarm audio file
EYE_ASPECT_RATIO_THRESHOLD = 0.25 # EAR threshold (lower = more sensitive)
EYE_ASPECT_RATIO_CONSEC_FRAMES = 20 # Frames threshold before triggering alarmAdjusting Sensitivity:
- Lower
EYE_ASPECT_RATIO_THRESHOLD= More sensitive (detects drowsiness faster) - Higher
EYE_ASPECT_RATIO_THRESHOLD= Less sensitive (requires more obvious eye closure) EYE_ASPECT_RATIO_CONSEC_FRAMES= Number of consecutive frames with closed eyes before alarm triggers
- Open a terminal/command prompt in the project directory
- Run the application:
python main.py-
The webcam feed will open in a new window showing:
- Green rectangles around eyes = Eyes are open (normal)
- Red rectangles around eyes = Drowsiness detected (alarm will sound)
-
To exit the application, press Q on your keyboard
- Face Detection - Uses dlib's frontal face detector to locate faces in the video stream
- Facial Landmarks - Detects 68 facial landmarks including eye coordinates
- Eye Aspect Ratio (EAR) - Calculates the ratio of vertical to horizontal eye distances
- Drowsiness Detection - If EAR falls below the threshold for consecutive frames, drowsiness is detected
- Alarm Trigger - An audio alarm plays and a visual alert appears on screen
Issue: "shape_predictor_68_face_landmarks.dat not found"
- Solution: Download the file and update the path in
main.pyline 26
Issue: "No module named 'dlib'"
- Solution: Install dlib using
pip install dlib(may take a few minutes to compile)
Issue: Alarm doesn't play
- Solution: Ensure
alarm.mp3exists in the project directory and pygame is properly installed
Issue: Webcam not detected
- Solution: Check if another application is using your webcam or try changing the camera index from
0to1inmain.pyline 34
Issue: Low FPS or laggy detection
- Solution: The video is resized to 960x720. You can reduce this for better performance or increase for better quality
Sleep-Detector/
├── main.py # Main application file
├── README.md # This file
├── alarm.mp3 # Your alarm audio file
└── shape_predictor_68_face_landmarks.dat # Face landmarks model
This project is open source and available under the MIT License.
Created as a driver safety application to prevent accidents caused by driver drowsiness.