4

I want to install dlib and OpenCV on Raspberry Pi 2. As you know, if I try to compile those on the Pi directly, then a) It takes a lot of time (3-4 hours for dlib and then 2 for OpenCV) and b) It is prone to errors.

However, using the cross-compiler arm-linux-gnueabihf, I can use the following command to run the cmake build process (copied from here):

export CC=/usr/bin/arm-linux-gnueabihf-gcc export CXX=/usr/bin/arm-linux-gnueabihf-g++ cmake --build --config Release ..

This works fine, and I can make -j4 later and get the .so files:

Contents of ...dlib-19.4/build/dlib

$ ls CMakeFiles config dlib-1.pc libdlib.so Makefile cmake_install.cmake config.h libdlib.a libdlib.so.19.4.0 revision.h

Is this it? Can I just copy/paste the .so file to site-packages on my Pi and it would work? I highly doubt it.

What are the exact steps to follow to compile a cmake enabled project for Raspberry Pi, with host as an Ubuntu machine?

Note: I am going to use OpenCV in a python project, and not C++.

7
  • 1
    Quick answer: No. If you search for opencv all over the whole system. You might find the answer you are looking for. Or maybe search for a .deb file that's OpenCV and walk through the steps you need.
    – kuzeyron
    Sep 4, 2017 at 10:25
  • 1
    Sure. Then how can I cross-compile it?
    – code
    Sep 4, 2017 at 10:47
  • 1
    If you take a part a .deb file. You can see how it's made. And from that, you'll know what you need.
    – kuzeyron
    Sep 4, 2017 at 10:48
  • 2
    OpenCV doesn't come with a .deb file. Is there some ambiguity in the question? :/ I don't know where you're getting the .deb file bit.
    – code
    Sep 4, 2017 at 10:49
  • 1
    The question is more about how to cross-compile a C++ library for ARM.
    – code
    Sep 4, 2017 at 10:50

1 Answer 1

1

Here's a partial answer. To cross-compile OpenCV and dlib for Raspbian 8 Jessie, mount your sdcard to an Ubuntu machine and set up like so:

cd ~/
git clone https://github.com/raspberrypi/tools
git clone https://github.com/opencv/opencv
git clone https://github.com/davisking/dlib
sudo apt install cmake

cd opencv; mkdir build; cd build
CC=$HOME/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc \
  CXX=$HOME/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ \
  cmake --config Release -DWITH_PNG=OFF -DWITH_JPEG=OFF ..
make -j8
sudo mount --bind /media/<your username>/root/usr/local /usr/local
sudo make install

cd ../../dlib; mkdir build; cd build
CC=$HOME/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc \
  CXX=$HOME/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ \
  cmake --config Release ..
make -j8
sudo make install

sudo umount /usr/local

Why disable JPEG and PNG support in OpenCV? Well these depend on zlib, so you'd need zlib cross-compiled for the Pi or else those would fail to build. Keep this in mind as later I touch upon how cross-compilation tasks like this tend to snowball out of control.

You can then take your micro-SD card out and put it back in the Pi and it'll have your cross-compiled OpenCV + dlib libraries, but we're still missing a big part you asked for: Python support.

So let's inspect what's required there:

sudo apt install cmake-curses-gui
cd ~/opencv/build
ccmake ..

In the CMake configuration you'll see missing variables like PYTHON2_LIBRARY. In order to link OpenCV for Python you'd need a libpython*.so cross-compiled as well! This is do-able and you could even pull the binaries off your Pi and use the mount trick for pointing CMake at the appropriate paths. Before we go down that route, may I suggest an alternative.

On your Pi, you can install the appropriate Python bindings via:

sudo apt install python-opencv cmake libboost-python-dev
sudo env CC=clang CXX=clang++ pip install -v dlib

For C++ projects it's not uncommon that custom cross-compilation comes about in order to satisfy downstream linking requirements. But if you're planning to use only Python, can't you get by using the standard prebuilt packages for OpenCV?

As for dlib, the pip compiles locally so it's slow. Perhaps one hour on a Pi 3 and longer on other models. As shown above I used clang to avoid gcc taking up all of the Pi's RAM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.