Skip to content

Instantly share code, notes, and snippets.

@haipnh
Forked from manashmandal/build_opencv_ARM_cross
Created June 15, 2019 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haipnh/66b0216574c774b3722d05b501880453 to your computer and use it in GitHub Desktop.
Save haipnh/66b0216574c774b3722d05b501880453 to your computer and use it in GitHub Desktop.
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.
1.Run the following commands in both machines 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 pip install numpy
sudo pip3 install numpy
2.The above command should take care of the dependencies specific to opencv. Let us use the term "build machine" for your PC where you are building opencv and "target machine" for the ARM single board computer. On the "build machine", u need to install a few more packages. Run
sudo apt-get install crossbuild-essential-armhf gawk pkg-config git
Some of these packages may not be necessary but its nice to have them.
3. If you follow most of the tutorials for cross compiling opencv, it will build nicely but the python modules wont build without the following steps.
a. Copy /usr/lib/arm-linux-gnueabihf/libpython2.7.so and libpython3.5m.so(same directory) from "target machine" to "host machine".Put those in a suitable folder. In my case I kept them in python_libs folder inside my opencv-3.3.0 source directory.
b. Copy /usr/include/arm-linux-gnueabihf/python2.7/pyconfig.h and /usr/include/arm-linux-gnueabihf/python3.5m/pyconfig.h from "target_machine". Create arm-linux-gnueabihf folder inside /usr/include/ folder in the
"build machine" and create python2.7 and python3.5m folders inide it and copy the appropriate pyconfig.h files.
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
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 -D BUILD_opencv_python2=ON -D PYTHON2_LIBRARIES=/home/shovon/ocv_armhf/python_libs/libpython2.7.so -D PYTHON2_INCLUDE_PATH=/usr/include/python2.7 -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/lib/python2.7/dist-packages/numpy/core/include 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 -D BUILD_opencv_python2=ON -D PYTHON2_LIBRARIES=/home/<user>/ocv_armhf/python_libs/libpython2.7.so -D PYTHON2_INCLUDE_PATH=/usr/include/python2.7 -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/lib/python2.7/dist-packages/numpy/core/include BUILD_opencv_python3=ON -D PYTHON3_LIBRARIES=/home/<user>/ocv_armhf/python_libs/libpython3.5m.so -D PYTHON3_INCLUDE_PATH=/usr/include/python3.5 -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include ..
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