-
1Raspberry 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.
-
2Pre-Requisites
For CMake you can just follow the recommended instructions: Download the latest souce, unzip and enter:
./bootstrap make 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 https://github.com/opencv/opencv/archive/refs/tags/4.5.1.zip unzip opencv.zip 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.
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_V4L=ON \ -D WITH_LIBV4L=ON
if you are using Raspberry Pi 2 or newer you can also add these flags to add optimisations:
-D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \
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
-
3Windows
You can also run the code on Windows using your webcam. It was tested using MinGW compiler and pre-compiled OpenCV libraries.
- Install CMake.
- Install the MinGW-64 compiler (during install, select x86_64 architecture and posix threads)
- 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)
- 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/")
-
4Running the code
To run the code you can build it yourself by running this from the build directory (create an empty one inside the SleePi folder):
cmake .. cmake --build .
The executable will then appear in the bin folder from where you can run it. (Note that you need to run it from the bin directory, because the sound and prediction files are loaded using relative imports.
Or you can just use the provided scripts Build_and_run_Windows.bat or build_and_run_linux.sh to do this automatically.
-
5Launch program automatically at startup
- Edit the crontab list:
sudo crontab -e
- Select option 1 (nano)
- At the end of the file add this line:
@reboot /path/to/sleepi/run_sleepi_linux.sh
-
6Configuration
Before running the code, you can configure a lot of the parameters to adjust the code's behavior to your needs. These parameters are stored in the config.h, which is in the include folder. The description of what each parameter does is put in the file itself.
For example, if you want to save a video output at full size and show only Face detection and EAR, you could define:
const float SCALE_FACTOR = 1.0; const bool SHOW_VIDEO_OUTPUT = false; const bool SHOW_FACE_DETECTION = true; const bool SHOW_FACIAL_LANDMARKS = false; const bool SHOW_EYE_CONTOURS = false; const bool SHOW_EAR = true; const bool SAVE_TO_FILE = true;
Or if you want to use 60 frames for calibration, set the threshold 0.12 below the average value and sound the alarm if the instantaneous EAR (not using EMA) is below threshold for more than 3 seconds, you could define:
const float TIME_THRESHOLD = 3.0; const float ALPHA = 1.0; const float THRESHOLD_SENSITIVITY = 0.12; const int FRAMES_FOR_CALIBRATION = 60;
Note that after changing parameters you need to recompile the code.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.