Sleep Pi is a real time project which alerts the driver when he is about to fall asleep while driving.

Similar projects worth following
SleePi is a real-time sleepiness detection and alert system. It is developed primarily for Raspberry Pi and its camera module.
The main application for this project is driver drowsiness detection, as the Raspberry Pi is small enough to be placed in the car without obstructing the view and can be powered from the 12V socket (or a dedicated USB port).
The module in this project uses an infrared camera, which helps visibility at low-light conditions as the drivers are likely to become drowsy while driving at night

How does SleePi work?

SleePi determines the position of the eyes and uses the Eye Aspect Ratio (EAR) to determine when the eyes are closed (or more squinted than usual). When the system detects that the user is sleepy, it starts playing a loud alarm sound trough the AUX port on Raspberry Pi (it can be connected to the car's speaker system). The alarm keeps playing until the user opens his eyes.

Raspberry Pi

Infrared Camera

The first and most important step is to obtain a video from the Raspberry Pi's camera. We resized the obtained video to 640x480 pixels and converted it to grayscale, making it suitable for ongoing operation. From there on, the facial parts like eyebrows, nose, mouth, eyes and facial structure were captured. The eyes were then confined to ascertain the PERCLOS, which depends on the Eye Aspect Ratio (EAR). The EAR was calculated during the calibration process, which was used to decide the drowsiness limit

  • 1 × Raspberry Pi (v1) with Pi NoIR Camera Module v2
  • 2 × Integrated Camera

  • 1
    Raspberry Pi (or other Linux devices)

    If you are using the official camera module you need to enable it first (more detailed instructions here). Enter into terminal:

    sudo raspi-config

    Go to Interfacing Options, select Camera and follow the prompts to enable it. Your Raspberry Pi will reboot.

    To output the audio trough the AUX port, you need to go to the raspi-config again, select System Options , then Audio and 3.5mm jack.

  • 2
    • CMake (Tested using 3.20.1)
    • OpenCV (Tested using 4.1.1 and 4.5.1)

    For CMake you can just follow the recommended instructions: Download the latest souce, unzip and enter:

    make install

    For OpenCV you need to install dependencies

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install cmake gfortran
    sudo apt-get install libjpeg-dev libtiff-dev libgif-dev
    sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
    sudo apt-get install libgtk2.0-dev libcanberra-gtk*
    sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
    sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev libv4l-dev
    sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
    sudo apt-get install libjasper-dev liblapack-dev libhdf5-dev
    sudo apt-get install protobuf-compiler

    Then download source (you can use more recent version if available) unzip and rename for conveniance

    cd ~
    wget -O
    mv opencv-4.5.0 opencv

    Create and build build directory

    cd ~/opencv/
    mkdir build
    cd build

    Then compile. The V4L flags are needed to enable Video4Linux, which increases camera module support.

    		-D CMAKE_INSTALL_PREFIX=/usr/local \
    		-D WITH_V4L=ON \

    if you are using Raspberry Pi 2 or newer you can also add these flags to add optimisations:


    Before installing, you need to increase the swap size. Otherwise the install process may be stuck because it does not have enough memory space

    sudo nano /etc/dphys-swapfile

    Go to CONF_SWAPSIZE and change from 100 to 2048. Restart the swap service to make sure changes take effect.

    sudo /etc/init.d/dphys-swapfile stop
    sudo /etc/init.d/dphys-swapfile start

    Run make, with the number after j being the number of cores you have (Pi 1 has 1 core and the newer versions have 4 cores)

    make -j4

    Be aware that this process takes a very long time. On the tested Rasberry Pi 1 it took around 24 hours. On Raspberry Pi 4 it should take about 2 hours. To complete, run

    sudo make install

    Now you should change the swap file size back to 100 and restarting the swap service using the same steps as before.

    If this did not work or for more detailed information you can check out this guide

  • 3

    You can also run the code on Windows using your webcam. It was tested using MinGW compiler and pre-compiled OpenCV libraries.

    1. Install CMake.
    2. Install the MinGW-64 compiler (during install, select x86_64 architecture and posix threads)
    3. Add "YOUR_INSTALL_DIRECTORY"\mingw64\bin to your system PATH. Download pre-compiled OpenCV libraries for MinGW from here (Latest x64 version is 4.1.1 at the time of writing)
    4. Extract the archive on your pc and add "EXTRACTED_LOCATION"\x64\mingw\bin to system PATH.

    If CMake still can't find your OpenCV installation, then you can change this line in CMakeLists.txt to reflect your OpenCV location.

    SET("OpenCV_DIR" "C:/OpenCV/")

View all 6 instructions

Enjoy this project?



Dan Maloney wrote 04/21/2021 at 17:22 point

Interesting idea, and so important. I can't count the number of times I've caught myself driving with the "neck snaps" going on. Not good, a little tech assistance would probably save a lot of heartache.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates