Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cross compile opencv3.3.0 for your raspberry pi and similar ARM devices with python support
This is a note on how to cross compile opencv for pretty much any ARM device(HardFP supported in this case) and deploy. Native
compiling in ARM devices can be painfully slow and they seem to hang often during build(mine got stuck at 43%). So if you happen
to have a desktop/laptop/server running ubuntu or similar linux distro, u can build opencv in fractionth of the time taken for
native compiling without any issues.
Building opencv3 with TBB and NEON and VFP support can boost opencv performance. Thanks to Adrian at pyimagesearch for pointing
that out.
Both my PC and target machine aka orange pi zero are running ubuntu 16.04 with python2.7 and python 3.5.
Let us use the term "build machine" for your PC where you are building opencv and "target machine" for the ARM single board computer.
1.Run the following commands in both machines(I think installing these in target machine only would do) to install the necessary libraries etc.(mine worked with them,so they should be enough
hopefully)
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev libcanberra-gtk* libatlas-base-dev gfortran python2.7-dev python3-dev
sudo apt-get install ffmpeg(in the target machine)
(Note-linpng12-dev may report conflict, if so, install libpng-dev)
sudo pip install numpy
sudo pip3 install numpy
2.The above command should take care of the dependencies specific to opencv. On the "build machine", u need to install a few
more packages. Run
sudo apt-get install crossbuild-essential-armhf gawk pkg-config git pkg-config-arm-linux-gnueabihf sshfs
Some of these packages may not be necessary but its nice to have them.
3. If we try to build things directly, it will build but it will have many issues like no python or highgui support.So for this, we would
need a running instance of the OS out opencv build would be deployed to. We would be mounting the filesystem using sshfs into our build
machine and would link the path to opencv build system.
The following instructions are courtesy of the great instruction guide at http://studiow.cf/blog/post/how-to-cross-compile-opencv-for-armbian-with-gtk
I got info from someone that highgui didnt build and GTK+2.0 wasnt linked so I am using this part from the aforementioned guide.
Run these commands,
mkdir ~/mntsysroot
sudo sshfs <target username>@<ip of target>:/ ~/mntsysroot/ -o transform_symlinks -o allow_other
sudo ln -s /home/{your_username}/mntsysroot/usr/lib/arm-linux-gnueabihf/ /usr/lib/arm-linux-gnueabihf
sudo ln -s /home/{your_username}/mntsysroot/lib/arm-linux-gnueabihf/ /lib/arm-linux-gnueabihf
sudo ln -s /home/{your_username}/mntsysroot/usr/share /usr/share/arm-linux-gnueabihf
sudo ln -s /home/{your_username}/mntsysroot/usr/include/arm-linux-gnueabihf /usr/include/arm-linux-gnueabihf
export PKG_CONFIG_SYSROOT_DIR=/home/{your_username}/mntsysroot
export PKG_CONFIG_PATH=/usr/share/arm-linux-gnueabihf/pkgconfig:/home/{your_username}/mntsysroot/usr/lib/pkgconfig
arm-linux-gnueabihf-pkg-config --libs gtk+-2.0
arm-linux-gnueabihf-pkg-config --cflags gtk+-2.0
4. Now the regular things.Get opencv and opencv_contrib sources in the "build machine".
cd ~/
mkdir ocv_armhf(optional)
cd ocv_armhf
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip
cd opencv-3.3.0
mkdir build
cd build
nano ../platforms/linux/arm-gnueabi.toolchain.cmake
Now enter the following before everything in that file:
set(ENV{PKG_CONFIG_PATH} "/usr/share/arm-linux-gnueabihf/pkgconfig:/home/{your_username}/mntsysroot/usr/lib/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} "/home/{your_username}/mntsysroot")
set(PKG_CONFIG_EXECUTABLE "/usr/bin/arm-linux-gnueabihf-pkg-config")
set(ENV{LD_LIBRARY_PATH} "/home/{your_username}/mntsysroot/usr/lib")
set(ENV{C_INCLUDE_PATH} "/home/{your_username}/mntsysroot/usr/include")
set(ENV{CPLUS_INCLUDE_PATH} "/home/{your_username}/mntsysroot/usr/include")
save and exit
Then run
cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=~/ocv_armhf/opencv_contrib-3.3.0/modules -D ENABLE_NEON=ON -D ENABLE_VFPV3=ON -D BUILD_TESTS=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake -D WITH_TBB=ON -D BUILD_TBB=ON ..
If everything goes well, this will generate the Makefile. Check the messages to make sure that python 2.7 and 3.5 both have Libraries and numpy_include dirs set and none of them are showing NO.
run make -j8
after its complete(hopefully), run make install and it will put the binaries inside install folder inside the build directory.
now compress the install directory by running tar -zcvf opencv_build3.3.tar.gz install.
5.Copy the tar.gz file to "target machine". run tar -xf opencv_build3.3.tar.gz.
run sudo rsync -a install/ /usr/local
then sudo ldconfig -v (this should sort out the libraries)
then cd into /usr/local/lib/python3.5/dist-packages
run sudo ln -s cv2.cpython-35m-x86_64-linux-gnu.so cv2.so.
6. This should be it! Now run python2 and python3 and check if import cv2 happens without any error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment