Skip to content

Face detector using the BlazeFace Mediapipe model (with both CPU and TPU delegates) written in C++

License

Notifications You must be signed in to change notification settings

CLFML/Face_Detector.Cpp

Repository files navigation

Face_detector.Cpp

Face detector using the BlazeFace Mediapipe model (with both CPU and TPU delegates) written in C++;

  • Plain C/C++ implementation with minimal dependencies (Tensorflow Lite + OpenCV)
  • Google MediaPipe models without the Mediapipe framework
  • Support for Coral Edge TPU (not supported when using the ROS2 binding)
  • Runs on ARM as well (Tested on RPI 3,4 and 5)
  • ROS2 support

API Features

This library offers support for:

  • Face detection
  • Face ROI (Region of interest) detection
  • 2D Landmarks of the Left eye, Right Eye, Nose tip, Mouth, Left eye tragion and Right eye tragion

Face detection

This is some example code for face detection:

    #define MODEL_PATH FACE_DETECTOR_MODEL_DIR "/CPU/face_detection.tflite"
    ...

    /* Initialize face detector library */
    CLFML::FaceDetection::FaceDetector det;
    
    /* Load model and initialize inference runtime */
    det.load_model(MODEL_PATH);
    
    /* Load image into model and do inference! */
    det.load_image(cam_frame);

    /* Get face_detected value */
    const int face_detected = det.detected(); // returns -1 for no face and 0 for face detected!

Region Of Interest

This is some example code for capturing the region of interest:

    #define MODEL_PATH FACE_DETECTOR_MODEL_DIR "/CPU/face_detection.tflite"
    ...

    /* Initialize face detector library */
    CLFML::FaceDetection::FaceDetector det;
    
    /* Load model and initialize inference runtime */
    det.load_model(MODEL_PATH);
    
    /* Load image into model and do inference! */
    det.load_image(cam_frame);

    /* Get the face roi rectangle */
    cv::Rect face_roi = det.get_face_roi();

Example code

For a full example showcasing both these API functions see the example code in example/face_roi_demo/demo.cpp.

Building with CMake

Before using this library you will need the following packages installed:

  • OpenCV
  • Working C++ compiler (GCC, Clang, MSVC (2017 or Higher))
  • CMake
  • Ninja (Optional, but preferred)

Running the examples (CPU)

  1. Clone this repo
  2. Run:
cmake . -B build -G Ninja
  1. Let CMake generate and run:
cd build && ninja
  1. After building you can run (linux & mac):
./face_roi_demo

or (if using windows)

face_roi_demo.exe

Running the examples (Coral TPU)

  1. Make sure you got:
  • The newest libedgetpu library installed (based on TF 2.16.1); If not sure compile it from source!
  • libusb-1.0-0-dev package or installed (only when using Linux)\
  • When using Linux your udev configured correctly (is done automatically when compiling from source!)
  1. Clone this repo
  2. Run:
cmake . -B build -G Ninja -DFACE_DETECTOR_ENABLE_CORAL_TPU=ON

or add this block to your settings.json in vscode:

"cmake.configureSettings": {
    "FACE_DETECTOR_ENABLE_CORAL_TPU": "ON"
}
  1. Let CMake generate and run:
cd build && ninja
  1. After building you can run (linux & mac):
./face_roi_demo

or (if using windows)

face_roi_demo.exe

Using it in your project as library

Add this to your top-level CMakeLists file:

include(FetchContent)

FetchContent_Declare(
    face_detector
    GIT_REPOSITORY https://github.com/CLFML/Face_Detector.Cpp
    GIT_TAG main
    SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/lib/Face_Detector.Cpp
)
FetchContent_MakeAvailable(face_detector)
...
target_link_libraries(YOUR_EXECUTABLE CLFML::face_detector)

Or manually clone this repo and add the library to your project using:

add_subdirectory(Face_Detector.Cpp)
...
target_link_libraries(YOUR_EXECUTABLE CLFML::face_detector)

Building a ROS2 package with Colcon

Before using this library you will need the following packages installed:

  • OpenCV
  • ROS2
  • ROS CV bridge
  • Working C++ compiler (GCC, Clang, MSVC (2017 or Higher))
  • CMake

Running the examples (Ubuntu, CPU)

  1. Clone this repo:
git clone https://github.com/CLFML/Face_Detector.Cpp.git
  1. Source your ROS2 installation:
source /opt/ros/jazzy/setup.bash
  1. Install the dependencies:
rosdep install --from-paths src -y --ignore-src
  1. Build the package:
colcon build --packages-select face_detector
  1. Set up the environment:
source install/setup.bash
  1. Run the camera node:
ros2 run v4l2_camera v4l2_camera_node
  1. In another terminal, run the face detector node (don’t forget to source the setup script):
ros2 run face_detector face_detector_node
  1. In yet another terminal, run the viewer:
ros2 run face_detector face_detector_viewer

Aditional documentation

See our wiki...

Todo

  • Add language bindings for Python, C# and Java
  • Add support for MakeFiles and Bazel
  • Add Unit-tests
  • Add ROS2 package TPU support
  • If needed to crunch ROS2 latency: Do not use nodes, put plugins in a container

License

This work is licensed under the Apache 2.0 license.