Realtime Driver Drowsiness Detection (Sleep Detection)


Various studies show that around 20% of all road accidents are fatigue-related, up to 50% on certain conditions. One of the ways to reduce this percentage is to use Driver drowsiness detection technology. In this code I introduce an implementation of Driver drowsiness detection via eye monitoring being it closed or opened.


  1. Install Opencv3.
  2. Install Python.
  3. Install Xquartz for Mac os and install libx11-dev if you use Ubuntu to be able run the realtime code.
  4. Install Dlib and its dependencies.
  5. Download the trained facial shape predictor from
  6. Source code


Dlib is a C++ toolkit for machine learning, it also provides a python API to use it in your python apps. One of its best features is a great documentation for C++ and Python API. It is used in the code to detect faces and get facial landmarks coordinates especially the 12 points which define the two eyes left and right (Fig 1). After getting the 12 points of left and right eye, we compute Eye aspect ratio (Fig 2) to estimate the level of the eye opening. Open eyes have high values (>0.2) of EAR while the closed eye it is getting close to zero.

Fig 1: 300-W facial landmarks annotations.

Fig 2: Eye aspect ratio formula.

  • Python code: Simple app to detect the status of eye being it closed or opened (Source code here with two test images).

    python /path/to/shape_predictor_68_face_landmarks.dat test1.jpg

  • C++ code: Realtime sleep detection.

    To Compile c++ app, clone dlib from github and execute the following commands in your terminal.

    $git clone $cd dlib  $mkdir  sleep_detection $cd sleep_detection/ 
    Then paste two files (sleep_detetction.cpp and CMakeLists.txt) from my repository here to sleep_detection folder and then create build folder and compile the app via these commands.
    $mkdir build $cd build  $cmake .. $cmake --build . --config Release 
    Run the following command to run the app.
    $ ./sleep_detection 

Real time sleep detection


Subscribe to Our Mailing List



Blog Tags

Follow me

Facebook Page