Install Precompiled Dlib on Raspberry Pi

Install Precompiled Dlib on Raspberry Pi

Dlib is an open-source library that provides machine learning algorithms and tools for solving classification, regression, and clustering problems.

This tutorial demonstrates how to install precompiled Dlib on Raspberry Pi.

Debian package

We have created Debian package (.deb) that contains precompiled Dlib 19.24.5 binaries for Raspberry Pi 3 Model A+/B+ and Raspberry Pi 4 Model B. Binaries are compatible with Raspberry Pi OS Bookworm 64-bit. We have created a release on GitHub repository and uploaded the dlib.deb ​package.

Dlib was built with the following features:

  • NEON optimization
  • Statically linked with OpenBLAS library
  • Python 3 bindings

Tested using Raspberry Pi 4 Model B (8 GB).

Install Dlib

Connect to Raspberry Pi via SSH. Download the .deb package from releases page of the repository using the following command:

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

Once the download is complete, run the command to install Dlib:

sudo apt install -y ./dlib_64.deb

The .deb package is no longer necessary, you can remove it:

rm -rf dlib_64.deb

Testing Dlib (C++)

To test program compilation, install GNU C++ compiler:

sudo apt install -y g++

Download a test image from the Internet:

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

Create a main.cpp file:

nano main.cpp

Add the following code when a file has been opened:

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_io.h>

using namespace dlib;

int main()
{
    array2d<rgb_pixel> img;
    load_image(img, "test.jpg");

    frontal_face_detector detector = get_frontal_face_detector();
    std::vector<rectangle> bboxes = detector(img);

    for (unsigned int i = 0; i < bboxes.size(); i++) {
        draw_rectangle(img, bboxes[i], rgb_pixel(0, 255, 0), 3);
    }

    save_jpeg(img, "result.jpg");

    return 0;
}

The code is used to detect faces in an image and draw a bounding box around the detected faces. The final image is saved to a file.

Execute the following command to compile a code:

g++ main.cpp -o test -O3 -Wno-psabi -ldlib

Run a program:

./test

Here is result:

Detected faces in image using Dlib on Raspberry Pi

Testing Dlib (Python)

The Python bindings of DLib doesn't provide draw_rectangle or similar function for drawing rectangles on an image. For this purpose, we can install precompiled OpenCV.

After you install OpenCV, create a main.py file:

nano main.py

Add the following code:

import dlib
import cv2

img = dlib.load_rgb_image('test.jpg')

detector = dlib.get_frontal_face_detector()
bboxes = detector(img, 1)

for b in bboxes:
    cv2.rectangle(img, (b.left(), b.top()), (b.right(), b.bottom()), (0, 255, 0), 2)

dlib.save_image(img, 'result.jpg')

Execute a script using the Python 3:

python3 main.py

Python code is doing the same process as C++ code.

Uninstall Dlib

If you want to completely remove Dlib and related dependencies, run the following command:

sudo apt purge --autoremove -y dlib

Leave a Comment

Cancel reply

Your email address will not be published.