Create a gist now

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.
@wb666greene

This comment has been minimized.

Show comment
Hide comment
@wb666greene

wb666greene Nov 25, 2017

This gist worked great on Ubuntu 16.04 cross-compiling for my Rasperry Pi3, but when I ran a PyImageSearch example code for the Pi it looks like highgui is not built:
_pi@Pi3cam:~/object-detection-deep-learning $ ./oddl.sh example_05.jpg
[INFO] loading model...
[INFO] computing object detections...
[INFO] car: 99.48%
[INFO] cat: 57.13%
[INFO] dog: 57.64%
[INFO] horse: 99.85%
[INFO] person: 85.77%
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /home/wally/ocv_armhf/opencv-3.3.0/modules/highgui/src/window.cpp, line 605
Traceback (most recent call last):
File "deep_learning_object_detection.py", line 74, in
cv2.imshow("Output", image)
cv2.error: /home/wally/ocv_armhf/opencv-3.3.0/modules/highgui/src/window.cpp:605: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

Any ideas of what needs to be added to the cmake command to fix this?

I can cross-compile many times in the time it takes to build once on the Pi3 (which I did following the PyImageSearch instructions), tried the cross-compliation for the "optimized" version using NEON & VFPV3 after reading your comment on PyImageSearch.

wb666greene commented Nov 25, 2017

This gist worked great on Ubuntu 16.04 cross-compiling for my Rasperry Pi3, but when I ran a PyImageSearch example code for the Pi it looks like highgui is not built:
_pi@Pi3cam:~/object-detection-deep-learning $ ./oddl.sh example_05.jpg
[INFO] loading model...
[INFO] computing object detections...
[INFO] car: 99.48%
[INFO] cat: 57.13%
[INFO] dog: 57.64%
[INFO] horse: 99.85%
[INFO] person: 85.77%
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /home/wally/ocv_armhf/opencv-3.3.0/modules/highgui/src/window.cpp, line 605
Traceback (most recent call last):
File "deep_learning_object_detection.py", line 74, in
cv2.imshow("Output", image)
cv2.error: /home/wally/ocv_armhf/opencv-3.3.0/modules/highgui/src/window.cpp:605: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

Any ideas of what needs to be added to the cmake command to fix this?

I can cross-compile many times in the time it takes to build once on the Pi3 (which I did following the PyImageSearch instructions), tried the cross-compliation for the "optimized" version using NEON & VFPV3 after reading your comment on PyImageSearch.

@hrshovon

This comment has been minimized.

Show comment
Hide comment
@hrshovon

hrshovon Dec 22, 2017

I shall look into it. Sorry for late reply.

Owner

hrshovon commented Dec 22, 2017

I shall look into it. Sorry for late reply.

@hrshovon

This comment has been minimized.

Show comment
Hide comment
@boudhayan-dev

This comment has been minimized.

Show comment
Hide comment
@boudhayan-dev

boudhayan-dev Feb 9, 2018

I followed all the steps and it even generated the cv2.so file in my Raspberry pi . but when i import cv2 using Python3 , it shows there is no module named cv2
opencv

boudhayan-dev commented Feb 9, 2018

I followed all the steps and it even generated the cv2.so file in my Raspberry pi . but when i import cv2 using Python3 , it shows there is no module named cv2
opencv

@wb666greene

This comment has been minimized.

Show comment
Hide comment
@wb666greene

wb666greene Jul 22, 2018

@boudhayan-dev
Don't know if you've a solution, but does it work for Python 2.7?

I believe the Python3 bindings need to be renamed after building 3.3.0, try something like this:

cd /usr/local/lib/python3.5/dist-packages sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

The paths will be different if you are using virtual environments.

HTH.

wb666greene commented Jul 22, 2018

@boudhayan-dev
Don't know if you've a solution, but does it work for Python 2.7?

I believe the Python3 bindings need to be renamed after building 3.3.0, try something like this:

cd /usr/local/lib/python3.5/dist-packages sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

The paths will be different if you are using virtual environments.

HTH.

@wb666greene

This comment has been minimized.

Show comment
Hide comment
@wb666greene

wb666greene Jul 22, 2018

I'm using these instructions to try and cross-compile OpenCV-3.4.2 and getting stuck at the end of step 4.

After the cmake

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.

I'm getting this:
-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.12", minimum required is "2.7") -- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "2.7") -- Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV) -- If you want to enable Python/Numpy support, set the following variables: -- PYTHON2_INCLUDE_PATH -- PYTHON2_LIBRARIES (optional on Unix-like systems) -- PYTHON2_NUMPY_INCLUDE_DIRS -- PYTHON3_INCLUDE_PATH -- PYTHON3_LIBRARIES (optional on Unix-like systems) -- PYTHON3_NUMPY_INCLUDE_DIRS -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.5.2", minimum required is "3.4") -- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "3.5") -- Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV) -- If you want to enable Python/Numpy support, set the following variables: -- PYTHON2_INCLUDE_PATH -- PYTHON2_LIBRARIES (optional on Unix-like systems) -- PYTHON2_NUMPY_INCLUDE_DIRS -- PYTHON3_INCLUDE_PATH -- PYTHON3_LIBRARIES (optional on Unix-like systems) -- PYTHON3_NUMPY_INCLUDE_DIRS
Furthermore none of the video stuff seems enabled:
`-- GUI:
-- GTK+: NO

-- Media I/O:
-- ZLib: build (ver 1.2.11)
-- JPEG: libjpeg-turbo (ver 1.5.3-62)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: build (ver 1.6.34)
-- TIFF: build (ver 42 - 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES

-- Video I/O:
-- DC1394: NO
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
-- avutil: NO
-- swscale: NO
-- avresample: NO
-- GStreamer: NO
-- libv4l/libv4l2: NO
-- v4l/v4l2: linux/videodev2.h
-- gPhoto2: NO
`
I'm stumped. Does every release of openCV need different build instructions?

Edit: I was able to compile on the Pi3, but it took many hours :(

wb666greene commented Jul 22, 2018

I'm using these instructions to try and cross-compile OpenCV-3.4.2 and getting stuck at the end of step 4.

After the cmake

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.

I'm getting this:
-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.12", minimum required is "2.7") -- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "2.7") -- Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV) -- If you want to enable Python/Numpy support, set the following variables: -- PYTHON2_INCLUDE_PATH -- PYTHON2_LIBRARIES (optional on Unix-like systems) -- PYTHON2_NUMPY_INCLUDE_DIRS -- PYTHON3_INCLUDE_PATH -- PYTHON3_LIBRARIES (optional on Unix-like systems) -- PYTHON3_NUMPY_INCLUDE_DIRS -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.5.2", minimum required is "3.4") -- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "3.5") -- Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV) -- If you want to enable Python/Numpy support, set the following variables: -- PYTHON2_INCLUDE_PATH -- PYTHON2_LIBRARIES (optional on Unix-like systems) -- PYTHON2_NUMPY_INCLUDE_DIRS -- PYTHON3_INCLUDE_PATH -- PYTHON3_LIBRARIES (optional on Unix-like systems) -- PYTHON3_NUMPY_INCLUDE_DIRS
Furthermore none of the video stuff seems enabled:
`-- GUI:
-- GTK+: NO

-- Media I/O:
-- ZLib: build (ver 1.2.11)
-- JPEG: libjpeg-turbo (ver 1.5.3-62)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: build (ver 1.6.34)
-- TIFF: build (ver 42 - 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES

-- Video I/O:
-- DC1394: NO
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
-- avutil: NO
-- swscale: NO
-- avresample: NO
-- GStreamer: NO
-- libv4l/libv4l2: NO
-- v4l/v4l2: linux/videodev2.h
-- gPhoto2: NO
`
I'm stumped. Does every release of openCV need different build instructions?

Edit: I was able to compile on the Pi3, but it took many hours :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment