Driver Sleep Detection Using Computer Vision

About the Project

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.

  1. 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.
  2. If the person is yawning more than a number of times under a particular time, that means is extremely sleepy and a WARNING is sent out.

Working of the Project

Part 1: Sleep Detector

  1. The first step is to detect if a face can be identified using the dlib library.
  2. Once a face is found, the coordinates of the eye are selected. The coordinates of the face are shown in the image below.
Face Coordinates

1. Simple run the main program using the command `python`.

2. 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.

3. Then stop the program by pressing the `q` key.

4. Open the file `Graphs\EAR.png`.

5. You can see the visualized EAR against the frame in this graph and from this, you can identify and set the EAR_threshold.

6. Modify this in the `config.json`.

7. A sample graph is shown in the image below. The red block represents a blink event.

Part 2: Yawn Detector

EAR Graph

Prerequisites for

  1. Install CMake: Since dlib is C-based, CMake is required for the build. Also, add CMake to your Environment Variables.
  2. Install Visual Studio here: Also install additional package Packages CMake tools for Windows .

Run the Code

  1. It is highly recommended to use a virtual environment. A medium article for this can be found here.
  2. Download the dblib models for face detection here.
  3. Install requirements.txt using pip install -r requirements.txt
  4. In order to test or run the code, run the command python

Additional Information


The codes of the project can be found on GitHub.

Python Version

The whole project is developed with the 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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store