Skip to content

Instantly share code, notes, and snippets.

@g40
Last active December 5, 2023 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g40/8418653791110858ce8b9d8c183099f1 to your computer and use it in GitHub Desktop.
Save g40/8418653791110858ce8b9d8c183099f1 to your computer and use it in GitHub Desktop.
How to configure OpenCV with Python3/numpy support in an aarch64 (ARM64) chroot for QEMU compilation
#!/bin/sh
#
# configure OpenCV with Python/numpy support in an Arm64 chroot on an Intel host.
#
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_PYTHON3=ON \
-D BUILD_OPENCV_PYTHON2=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.1/modules
-D WITH_NEON=ON \
-D BUILD_JAVA=OFF \
-D WITH_LAPACK=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_CAROTENE=OFF \
-D WITH_TBB=OFF \
-D WITH_OPENCL=OFF \
-D WITH_NVCUVID=OFF \
-D WITH_OPENCL_SVM=OFF \
-D WITH_OPENCLAMDFFT=OFF \
-D WITH_OPENCLAMDBLAS=OFF \
-D WITH_OPENEXR=OFF \
-D WITH_MATLAB=OFF \
-D WITH_GTK=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_GPHOTO2=OFF \
-D WITH_FFMPEG=OFF \
-D WITH_CUBLAS=OFF \
-D WITH_CUDA=OFF \
-D WITH_CUFFT=OFF \
-D WITH_EIGEN=OFF \
-D WITH_VTK=OFF \
-D WITH_JASPER=OFF \
-D WITH_WEBP=OFF \
-D WITH_1394=OFF \
-D BUILD_SHARED_LIBS=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON3_LIBRARIES=usr/lib/aarch64-linux-gnu/libpython3.5m.so \
-D PYTHON3_LIBRARY=usr/lib/aarch64-linux-gnu/libpython3.5m.so \
-D PYTHON_INCLUDE_DIRS=/usr/include/python3.5m \
-D PYTHON3_INCLUDE_PATH=/usr/include/python3.5m \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include \
-D PYTHON3_NUMPY_VERSION="1.15.0" \
-DPYTHON_DEFAULT_EXECUTABLE=$(which python3) \
-D CMAKE_TOOLCHAIN_FILE=../opencv-3.4.1/platforms/linux/aarch64-gnu.toolchain.cmake \
../opencv-3.4.1 2>&1 | tee config.log
@g40
Copy link
Author

g40 commented Aug 21, 2018

I needed a minimal footprint OpenCV cross-compiled with Python3/numpy support for an Arm64 (aarch64) board with limited resources. i.e. compiling on the target prohibitive in terms of time and awkward to set up (required build artefacts to be on an NFS connected server) so not good for automation.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_PYTHON3=ON -D BUILD_OPENCV_PYTHON2=OFF -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.1/modules _D WITH_NEON=ON -D BUILD_JAVA=OFF -D WITH_LAPACK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_CAROTENE=OFF -D WITH_TBB=OFF -D WITH_OPENCL=OFF -D WITH_NVCUVID=OFF -D WITH_OPENCL_SVM=OFF -D WITH_OPENCLAMDFFT=OFF -D WITH_OPENCLAMDBLAS=OFF -D WITH_OPENEXR=OFF -D WITH_MATLAB=OFF -D WITH_GTK=OFF -D WITH_GSTREAMER=OFF -D WITH_GPHOTO2=OFF -D WITH_FFMPEG=OFF -D WITH_CUBLAS=OFF -D WITH_CUDA=OFF -D WITH_CUFFT=OFF -D WITH_EIGEN=OFF -D WITH_VTK=OFF -D WITH_JASPER=OFF -D WITH_WEBP=OFF -D WITH_1394=OFF -D BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_LIBRARIES=usr/lib/aarch64-linux-gnu/libpython3.5m.so -D PYTHON3_LIBRARY=usr/lib/aarch64-linux-gnu/libpython3.5m.so -D PYTHON_INCLUDE_DIRS=/usr/include/python3.5m -D PYTHON3_INCLUDE_PATH=/usr/include/python3.5m -D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include -D PYTHON3_NUMPY_VERSION="1.15.0" -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) -D CMAKE_TOOLCHAIN_FILE=../opencv-3.4.1/platforms/linux/aarch64-gnu.toolchain.cmake ../opencv-3.4.1 2>&1 | tee config.log

The chroot in this case was based off a Devuan Arm64 base install. Make sure qemu-aarch64-static is installed and that update-binfmts --enable qemu-aarch64 has been run. Running the cmake spell below should then complete successfully with the following options enabled.

The critical element here is to specify the numpy version manually. Without it CMake gets into a twist, unable to complete the mission itself, an echo of which still shows up in the configuration process:

-- 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

and here is the final configuration report:

--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /build/opencv/opencv_contrib-3.4.1/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2018-08-21T15:16:52Z
--     Host:                        Linux 4.9.0-6-amd64 aarch64
--     Target:                      Linux 1 aarch64
--     CMake:                       3.7.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    NEON FP16
--       required:                  NEON
--       disabled:                  VFPV3
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++11:                       YES
--     C++ Compiler:                /usr/bin/aarch64-linux-gnu-g++  (ver 6.3.0)
--     C++ flags (Release):         -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/aarch64-linux-gnu-gcc
--     C flags (Release):           -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fdata-sections -Wa,--noexecstack -fsigned-char -Wno-psabi   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now    
--     Linker flags (Debug):        -Wl,--no-undefined -Wl,--gc-sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now    
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern freetype hdf java matlab ovis python2 sfm ts viz
--     Applications:                examples apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
-- 
--   Media I/O: 
--     ZLib:                        build (ver 1.2.11)
--     JPEG:                        build (ver 90)
--     PNG:                         build (ver 1.6.34)
--     TIFF:                        build (ver 42 - 4.0.9)
-- 
--   Video I/O:
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (built-in)
-- 
--   Other third-party libraries:
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.3)
--     Libraries:                   usr/lib/aarch64-linux-gnu/libpython3.5m.so
--     numpy:                       /usr/local/lib/python3.5/dist-packages/numpy/core/include (ver 1.15.0)
--     packages path:               lib/python3.5/dist-packages
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /build/opencv/test```

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