Today we are going to make a smart Driver Drowsiness Detection System using Raspberry Pi that will keep track of a driver’s eye movement. If it detects that the driver might fall asleep or is in a drowsy state, then an alert is triggered in the form of a sound to bring that person back to the conscious state.
During the transportation of cargo and heavy materials over long distances (especially during night time) through trucks, the effects of inadequate sleep prove to be a major problem for these vehicle drivers involved in continuous driving, leading to fatal accidents.
According to a 2014 AAA Traffic Safety Foundation study, it was found that 37 percent of drivers fell asleep during driving. Out of the 21 percent of fatal crashes, 13 percent caused severe injury – all due to a drowsy driver who has to continuously drive for long time periods and at the same time have an eye on the road.
We have also designed the circuit for the anti-sleep alarm device that you can check.
How does the Driver Drowsiness Detection System Work?
A camera module connected to an RPi module continuously records video of the driver’s seat. A Python script then detects the face of the driver and when detected, the eyes of the person are captured and passed on to another module named eyegame, which then processes the captured video frame and detects the eye movement. If the eye of the driver remains in the same position without any movement or blinking, then an alarm sound will be triggered, asking the driver to stay awake and keep driving.
Bill of Materials
We will also be needing an SD card for setting up the Raspbian OS.
Driver Drowsiness Detection System – Code
First of all, we need to set up the library and modules in our RPi. To do so, open the Linux terminal and install the required modules using the following syntax and commands.
sudo pip3 install install opencv
sudo pip3 instal face-recognition
sudo pip3 install espeak
sudo pip3 instal eyegame
sudo pip3 install delib
sudo pip3 instal numpy
After successfully installing the modules, now create a Python code. I have named it eyetrack.py. We will import all the required modules into the code.
Next, set the filename and path. This is needed because we want to capture and cut a specific frame from the video and save it in that path, which will be passed to the eyegame for eye movement detection.
Next, we will set the face and image name of the truck driver and create a while function that will run in loop until the statement is true.
Now we will capture the video from the camera by using OpenCV and cutting the video frame-by-frame. It will then be passed to the face recognition module for detecting the face.
If the recognized face is the same as the driver’s face, then its image will be saved in the path that we had previously created. Then we will call the eyegame module to analyze the eye and the eyeball movement.
By successfully detecting the face of the driver and the eyeball movement, the device will then repeatedly analyze the position of the eyeball. If no eye movement is detected, then the device will wait for 30 seconds.
Even after that, if no eye movement is detected, then audio will be set off to alert and wake up the driver. When the driver is awake and eye movement is detected, then the alert process will automatically stop.
After writing the entire code, connect the camera module to the Raspberry Pi, which is then connected to a speaker via an audio jack. Now run the script and wait for a few seconds. A window will appear on the screen that will be displaying a live video recording from the camera. By being present in front of the camera, your face and the eye movement and its position will be detected by the python terminal. If it detects that there is no eye movement, then it will start counting. After 30 seconds, a voice alert saying “Are You sleeping Start Driving” will be prompted.
This is how you can make your own driver drowsiness detection system.
If you face any issues while making this project, then please feel free to ask in the comments below.