Facial Recognition Application using OpenCV 3 on Buildroot - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

Overview

This is a guide to create a simple facial recognition application using OpenCV3 on Buildroot for Raspberry Pi 3B+, 4 platform. This guide can be generalized to be used with other platforms.

Setup Instructions

Setup OpenCV 3

Use this wiki page to setup OpenCV on your platform.
Note: It will be much easier to setup if you are using Raspberry Pi 4 platform. For Raspberry Pi 3B or 3B+, or any other platform, carefully read the wiki page to install OpenCV on your platform correctly.

Setup Python Packages for Facial Recognition Application

  1. Clone the following repository on your Ubuntu machine at a different location. Do NOT clone this in your working directory (where you are building the image).
git clone https://github.com/tanmay-mk/buildroot-assignments-base
  1. Navigate to base_external/package/ directory of the repository cloned above.

  2. Copy following directories and paste them in your project directory at ~/path-to-project/base_external/package/ directory.
    1. distro
    2. dlib
    3. pyparsing
    4. python-face-recognition-models
    5. python-face-recognition
    6. python-opencv-python
    7. python-scikit-build
    8. scikit-build
    9. wheel
    Make sure you add all these packages to Config.in in your ~/path-to-project/base_external/ directory.

  3. Now, navigate to base_external/rootfs_overlay directory of the repository cloned above.

  • Copy the entire etc folder inside your ~/path-to-project/base_external/rootfs_overlay/ directory.
  1. Navigate to your buildroot directory & open Buildroot menuconfig.
cd buildroot
make menuconfig
  1. Make sure you have selected following packages in the Buildroot menuconfig.
    Target packages -> Development Tools -> ctest
    Host utilities -> host cmake External options -> distro
    External options -> dlib
    External options -> python-dlib (will only appear after selecting dlib)
    External options -> pyparsing
    External options -> python-face-recognition
    External options -> python-face-recognition-models
    External options -> python-scikit-build
    External options -> wheel
    System Configuration -> Root filesystem overlay directories

and add the path to overlay directories.

../base_external/rootfs_overlay
  1. Now, let's install python dependencies required to build the image. If you do not already have these packages, they will be installed.
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org cmake
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org dlib
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org face_recognition

If you already have any or all of these packages installed on your Ubuntu machine, you should see following output. The following image shows the output for cmake package.
image

  1. Exit from menuconfig, and save the configuration.
cd ..
./save_config.sh

Check whether all packages were selected by examining aesd_rpi_defconfig file.

cat base_external/configs/aesd_rpi_defconfig 

If you don't see all the required packages, go back to Buildroot menuconfig and select them.

  1. We can now build the image.
./build.sh

This will be a long process.

  1. While the build process is in progress, you are most likely to get the following error.
Building extension for Python 3.8.6 (default, Nov 13 2021, 23:12:19) 
Invoking CMake setup: 'cmake /home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/tools/python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/build/lib.linux-x86_64-3.8 -DPYTHON_EXECUTABLE=/home/saloni/final-project-saloni1307/buildroot/output/host/bin/python -DCMAKE_BUILD_TYPE=Release'
CMake Error at /home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/dlib/external/pybind11/tools/FindPythonLibsNew.cmake:124 (message):
  Python config failure: Python is 64-bit, chosen compiler is 32-bit
Call Stack (most recent call first):
  /home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/dlib/external/pybind11/tools/pybind11Tools.cmake:16 (find_package)
  /home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/dlib/external/pybind11/CMakeLists.txt:33 (include)


-- Configuring incomplete, errors occurred!
See also "/home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/build/temp.linux-x86_64-3.8/CMakeFiles/CMakeOutput.log".
Traceback (most recent call last):
  File "setup.py", line 222, in <module>
    setup(
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/lib/python3.8/site-packages/setuptools/__init__.py", line 145, in setup
    return distutils.core.setup(**attrs)
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "setup.py", line 134, in run
    self.build_extension(ext)
  File "setup.py", line 171, in build_extension
    subprocess.check_call(cmake_setup, cwd=build_folder)
  File "/home/saloni/final-project-saloni1307/buildroot/output/host/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/python3.8/subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '/home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/tools/python', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/build/lib.linux-x86_64-3.8', '-DPYTHON_EXECUTABLE=/home/saloni/final-project-saloni1307/buildroot/output/host/bin/python', '-DCMAKE_BUILD_TYPE=Release']' returned non-zero exit status 1.
make[1]: *** [package/pkg-generic.mk:269: /home/saloni/final-project-saloni1307/buildroot/output/build/python-dlib-19.22.1/.stamp_built] Error 1
make: *** [Makefile:84: _all] Error 2
make: Leaving directory '/home/saloni/final-project-saloni1307/buildroot'

If you get this error, perform following steps.
a. Navigate to following directory.

cd buildroot/output/build/dlib-v19.17/dlib/external/pybind11/tools

b. Open FindPythonLibsNew.cmake file.

c. Find this piece of code
image

and replace it with

# Ignore this test while crosscompiling otherwise it will use the host python.
IF(NOT CMAKE_CROSSCOMPILING)
	# Make sure the Python has the same pointer-size as the chosen compiler
	# Skip if CMAKE_SIZEOF_VOID_P is not defined
	if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
	    if(PythonLibsNew_FIND_REQUIRED)
		math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
		math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
		message(FATAL_ERROR
		    "Python config failure: Python is ${_PYTHON_BITS}-bit, "
		    "chosen compiler is  ${_CMAKE_BITS}-bit")
	    endif()
	    set(PYTHONLIBS_FOUND FALSE)
	    return()
	endif()
endif()
  1. Now, build the image again.
./build.sh
  1. This time, the build should successfully complete.

Test the sample program.

  1. Flash your buildroot image on a SD card and boot up the hardware platform.
  2. Once the hardware platform is all booted up, test your application code.
cd /etc/face-rec-sample
python3 face_recog.py

The camera takes a while to boot up. Once it is on, you will see a kernel log message on the screen.

The facial recognition application is now running successfully!

References

  1. Saloni Shah's Final Project Repository issue #2 & #3
  2. Installing dlib library patch. Link
  3. OpenCV setup for Buildroot. Link
  4. Raspberry Pi Hardware Support wiki page. Link
⚠️ **GitHub.com Fallback** ⚠️