A Computer Vision based Project to check if the driver of a vehicle is sleepy.
The main idea of this project is to protect drivers who travel a long distance, especially during the night time, and are prone to feel sleepy.
The project can be deployed on a Raspberry Pi and can be installed along with a camera on the dashboard of the car. The system will automatically check if the person is sleepy and sound an alarm when the person seems to be sleepy.
The project checks for two things:
- If the person's eyes are closed continuously, then the person has slept (or is very sleepy) and an ALERT is sent out as this is critical.
- If the person is yawning more than a number of times under a certain time, that means is extremely sleepy and a WARNING is sent out.
The project is inspired from here.
-
The first step is to detect if a face can be identified using the dlib library.
-
Once a face is found, the coordinates of the eye are selected. The coordinates of the face are shown in the image below.
-
Once the eyes are identified, the Eye Aspect Ratio (EAR) for each eye is calculated using the equation shown below.
-
The average EAR of both eyes is taken.
-
If the Average EAR of both eyes falls below a threshold for a given consecutive number of frames, then the alarm is sounded and the driven can be woken.
-
The threshold of each driver/person can be found by a simple experiment.
- Simple run the main program using the command
python main.py
. - Ask the person to keep his eyes open for some time and then make a quick blink, then ask him to keep it open, and then a bigger blink.
- Then stop the program by pressing the
q
key. - Open the file
Graphs\EAR.png
. - You can see the visualized EAR against the frame in this graph and from this, you can identify and set the EAR_threshold.
- Modify this in the
config.json
. - A sample graph is shown in the image below. The red block represents a blink event.
- Simple run the main program using the command
-
On a similar basis the yawn detector also works and sents our WARNINGS. The parameters can be adjusted in the
config.json
. -
The yawn detector basically checks if a person yawns, and if the yawn is detected based on if MAR exceeds the
MAR_threshold
. -
If there are more than a defined number of yawns
number_of_yawns
in a specified number of frames (defined bynumber_of_frames_yawns
), then an ALARM is sounded. -
On a similar basis as defined in point 6, the thresholds can be detected and set.
- Install CMake: Since dlib is C-based, CMake is required for the build. Also, add CMake to your Environment Variables.
- Install Visual Studio here: Also install additional package Packages
CMake tools for Windows
.
-
It is highly recommended to use a virtual environment. A medium article for this can be found here.
-
Download the dblib models for face detection here.
-
Install requirements.txt using pip
install -r requirements.txt
-
In order to test or run the code, run the command
python main.py
The whole project is developed with python version Python 3.7.7
and pip version pip 19.2.3
.
In case of an error, feel free to contact me over Linkedin at Adnan.