Install Precompiled libfacedetection on Raspberry Pi

The libfacedetection is an open-source library to detect faces in the images. Library is based on CNN (Convolutional Neural Network). A pre-trained CNN model has been converted to C arrays and stored in a file. So we don’t need to download the model before using this library. The libfacedetection library doesn’t have any external dependencies.

This tutorial explains how to install precompiled libfacedetection on Raspberry Pi.

Debian package

We have created Debian package (.deb) that consist of precompiled libfacedetection binaries for Raspberry Pi 3 Model A+/B+ and Raspberry Pi 4 Model B. Binaries are compatible with Raspberry Pi OS Buster (32-bit). We have created a release on GitHub repository and uploaded libfacedetection.deb ​package.

The libfacedetection was built with the following features:

  • NEON optimization
  • OpenMP multi-threading

Testing was done on Raspberry Pi 4 Model B (8 GB).

Install libfacedetection

Use SSH to connect to Raspberry Pi. Run the following command to download the .deb package from releases page of the repository:

wget https://github.com/prepkg/libfacedetection-raspberrypi/releases/latest/download/libfacedetection.deb

Now install libfacedetection with command:

sudo apt install -y ./libfacedetection.deb

You can remove .deb package because we don’t need it anymore:

rm -rf libfacedetection.deb

Testing libfacedetection

For simplicity, install precompiled OpenCV for reading and writing image file. Also GNU C++ compiler needs to be installed:

sudo apt install -y g++

Download image which will be used for testing:

wget -O test.jpg https://raw.githubusercontent.com/ageitgey/face_recognition/master/examples/two_people.jpg

Now create a main.cpp file:

nano main.cpp

Add the following code:

#include <opencv2/opencv.hpp>
#include <facedetection/facedetectcnn.h>

using namespace cv;

#define DETECT_BUFFER_SIZE 0x20000

int main()
{
    Mat img = imread("test.jpg");

    unsigned char *buffer = (unsigned char *) malloc(DETECT_BUFFER_SIZE);
    int *results = facedetect_cnn(buffer, img.ptr(0), img.cols, img.rows, img.step);

    for (int i = 0; i < *results; i++) {
        short *p = ((short *) (results + 1)) + 142 * i;
        int score = p[0];
        int x = p[1];
        int y = p[2];
        int w = p[3];
        int h = p[4];

        char text[4];
        snprintf(text, sizeof(text), "%d", score);

        putText(img, text, Point(x, y - 3), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
        rectangle(img, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
    }

    imwrite("result.jpg", img);
    free(buffer);

    return 0;
}

The code detects faces in an image and draw bounding boxes around faces. Also we write confidence score (number between 0 and 100) near each bounding box.

Compile code using the following command:

g++ main.cpp -o test -lfacedetection -lopencv_core -lopencv_imgcodecs -lopencv_imgproc

Run a program:

./test

Output image is written to a specified file. Here is result:

Detected faces in image using libfacedetection on Raspberry Pi

Uninstall libfacedetection

If you want to completely remove libfacedetection, run the following command:

sudo apt purge --autoremove -y libfacedetection

Leave a Comment

Your email address will not be published. Required fields are marked *