Opencv-cuda python3 bindings?

I tried various ways to install opencv-cuda on my jetson-xavier (jetpack 4.6 coming with opencv 4.5.5 dirty).

this script from mdegans installed opencv4.4-cuda for python 2.7, but no support for python3.6.

i tried several ways to get python3.6 working with opencv-cuda
copying so-files
deinstall python2 & relink to python3

even when I

  • sudo apt remove python2.7-minimal
  • linked /usr/bin/python and /usr/bin/pip to their python3 counterparts
  • removed all installation commands for python2-packages from the script
  • startet the modified script

I still got no working python3 bindings (this destroyed the working python2.7 bindings as well).

in my dispair,

  • I hardcore removed all opencv pieces:
sudo su
rm /usr/local/lib/libopencv*.*
find /usr/loca/share -iname "*opencv*" -exec rm -irf {} \;
find /usr/loca/lib -iname "*opencv*" -exec rm -irf {} \;
find /usr/loca/include -iname "*opencv*" -exec rm -irf {} \;
find /usr/loca/bin -iname "*opencv*" -exec rm -irf {} \;
find /home/<user>/.local -iname "*opencv*" -exec rm -irf {} \;
  • removed my previous created links from /usr/bin/python and /usr/bin/pip to their python3 counterparts
  • started the original script again

but now I get segmentation faults:

modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/build.make:110: recipe for target 'modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/fundamental.cc.o' failed
modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/build.make:86: recipe for target 'modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/euclidean_resection.cc.o' failed
make[2]: *** [modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/euclidean_resection.cc.o] Error 4
CMakeFiles/Makefile2:6650: recipe for target 'modules/sfm/src/libmv/libmv/multiview/CMakeFiles/multiview.dir/all' failed
[ 27%] Built target IlmImf
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

Finally, I am running out of (internet-search)-ideas how to get jetson-xavier jetpack 4.6 a running python3(.6) opencv-cuda system. can anyone please give me some further hints?

1 Like

Hi,

The script you shared should build OpenCV with Python3.

Do you meet any error when compiling or installation?
Thanks.

Sorry, no errors, no python3-bindings. I tried this script several times - everytime python2.7 worked, but python3 did not.

There was already an issue posted on github, but without a solution which worked for me.

in the meanwhile i still try to use the script again and again (there is still a libopencv_ml.so.3.4 hiding somewhere, but a find / -iname "libopencv_ml*" does not find it)

the compile-errors i mentioned in the posting above, vanished after (again) removing all “opencv” and “cv2” findings and restarting the jetson. no errors or warnings occured, but the system can not find the opencv-python bindings at all:
python2 ends in “libopencv_ml.so.3.4 cannot open shared object file”
python3 ends in “no module named ‘cv2’”

whatsoever I tried to install opencv-python via pip and got warnings: “Defaulting to user installation because normal site-packages are not writeable”. nevertheless the pip-installation results in a non cuda version.

so now I am trying to run the installation script inside a venv.

1 Like

this time i tried the installation within an new created venv python3 -m venv venv01

logs of the installation tries are here: opencv-cuda-installation-logs

cmake flags: 
        
        -D BUILD_EXAMPLES=OFF
        -D BUILD_opencv_python2=OFF
        -D BUILD_opencv_python3=ON
        -D CMAKE_BUILD_TYPE=RELEASE
        -D CMAKE_INSTALL_PREFIX=/usr/local
        -D CUDA_ARCH_BIN=5.3,6.2,7.2
        -D CUDA_ARCH_PTX=
        -D CUDA_FAST_MATH=ON
        -D CUDNN_VERSION='8.2'
        -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 
        -D ENABLE_NEON=ON
        -D OPENCV_DNN_CUDA=ON
        -D OPENCV_ENABLE_NONFREE=ON
        -D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules
        -D OPENCV_GENERATE_PKGCONFIG=ON
        -D WITH_CUBLAS=ON
        -D WITH_CUDA=ON
        -D WITH_CUDNN=ON
        -D WITH_GSTREAMER=ON
        -D WITH_LIBV4L=ON
        -D WITH_OPENGL=ON
        -D BUILD_PERF_TESTS=OFF
        -D BUILD_TESTS=OFF

results in

-- General configuration for OpenCV 4.4.0 =====================================
--   Version control:               4.4.0
-- 
--   Extra modules:
--     Location (extra):            /tmp/build_opencv/opencv_contrib/modules
--     Version control (extra):     4.4.0
-- 
--   Platform:
--     Timestamp:                   2022-06-28T11:56:16Z
--     Host:                        Linux 4.9.253-tegra aarch64
--     CMake:                       3.10.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++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 7.5.0)
--     C++ flags (Release):         -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 -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -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 -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -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/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -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-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -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-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed  
--     Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    python2 world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cvv java js julia matlab ovis python3 ts viz
--     Applications:                apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.22.30)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  NO
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   build Jasper (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                YES (3.7.0)
--     GStreamer:                   YES (1.14.5)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Lapack:                      YES (/usr/lib/aarch64-linux-gnu/liblapack.so /usr/lib/aarch64-linux-gnu/libcblas.so /usr/lib/aarch64-linux-gnu/libatlas.so)
--     Eigen:                       YES (ver 3.3.4)
--     Custom HAL:                  YES (carotene (ver 0.0.1))
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   YES (ver 10.2, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             53 62 72
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.2)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /tmp/build_opencv/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /home/vc-forecr/ximea-opencv/venv01/bin/python3 (ver 3.6.9)
--     Libraries:                   NO
--     numpy:                       NO (Python3 wrappers can not be generated)
--     install path:                -
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------

showing python3 problems, but I am not able to fix them.

what information do I have to look for in order to fix this?

Please be sure to understand whether your opencv is under apt management, pip management, or unmanaged, before considering the following solution.

【Solution A1 - apt/debian package/unmanaged】
You have already built opencv-contrib(opencv+cuda) and installed it on your system.
Create venv with --system-site-packages option.

python3 -m venv venv01 --system-site-packages

Explain:
There is an option “–system-site-packages” in venv/virtualenv.
Debien package is system site package. So you must need this option.

【Solution A2 - apt/debian package/unmanaged】
If you don’t want to use “–system-site-packages”, you can create a symbolic link instead.
See also: Installation of TensorRT in VEnv on Jetson Nano B01 - #2 by naisy
Example:

sudo ln -s /usr/lib/python3.6/dist-packages/cv2 /PATH_TO_VENV/lib/python3.6/site-packages/cv2

【Solution A3 - apt/debian package/unmanaged】
Instead of a symbolic link, it can be added to the PYTHONPATH.
Example:

export PYTHONPATH=/usr/lib/python3.6/dist-packages/cv2:$PYTHONPATH

【Solution B1 - pip/wheel package】
Build opencv-contrib with pip install.
Example: (EDIT)

CMAKE_ARGS='-DOPENCV_ENABLE_NONFREE=ON -DBUILD_TYPE=Release -DCUDA_ARCH_BIN=7.2,6.2,5.3 -DWITH_CUDA=ON' pip install -v --no-binary=opencv-contrib-python opencv-contrib-python==4.6.0.66

Explain:
If you are dissatisfied with the distributed binaries, you can build from source code.

The following URL was used as a reference for Solution B1.

【Solution B2 - pip/wheel package】
If you need to patch the source code, Solution B1 is not available.
git clone or download the compressed file, apply the patch, and create wheel file.
See also: GitHub - opencv/opencv-python: Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.

Hello @naisy,
thank you very much for your detailed answer.
It seems that solution B1 could the helpful, but i fear, that I already have messed up the (OS-)system beyond repair.
So first step might be restoring the OS to factory-settings (copy file-system from emmc to sdd?).

If your environment is difficult to restore, I recommend using docker.
Even if you destroy software environment in docker container, you can restore it in 5 seconds.
For me, there is no reason not to use docker.

the script from mdegans worked fine, after I recovered SSD from eMMC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.