【问题标题】:How to build OpenCV from source with python binding?如何使用 python 绑定从源代码构建 OpenCV?
【发布时间】:2022-01-16 21:24:12
【问题描述】:

我需要从源代码构建 OpenCV,并且空间有限,因此,我必须基于模块构建 OpenCV(https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html - 构建有限的模块集)。好的,cmake 并让工作正常。

从 python 中使用这些 OpenCV 模块会更容易,所以我添加了 cmake -D BUILD_opencv_python3=ONmake install 不会创建我预期的 prefix/lib/pythonX.Y/site-packages 目录。据我了解文档,没有与 python 相关的选项,只有 CMake 选项。

如何在基于模块的基础上构建 OpenCV 的 python(3) 绑定? 安装后,如何设置环境以使 python -c "import cv2" 与我定制的 OpenCV 一起工作?

更新

在 jetson 上运行 ubuntu L4T。 CMake 输出:

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.9)
--     Libraries:                   NO
--     numpy:                       /home/me/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.19.5)
--     install path:                -

所以我猜空的安装路径隐藏了一个问题......但grep -ni OPENCV_PYTHON3_INSTALL_PATH CMakeCache.txt 什么也没返回。

此时,我尝试添加cmake -DOPENCV_PYTHON3_INSTALL_PATH=~/Programs/opencv/local/lib/python3.6/site-packages/,但cmake输出中仍然没有(空白安装路径)但是

grep -ni OPENCV_PYTHON3_INSTALL_PATH CMakeCache.txt 
1076:OPENCV_PYTHON3_INSTALL_PATH:UNINITIALIZED=~/Programs/opencv/local/lib/python3.6/site-packages/

make install 之后(没有 sudo - 我没有 root 权限,需要本地安装),我在本地安装中没有 python 绑定。

...任何线索将不胜感激!

注意:在 L4T ubuntu 上编译 opencv-4.5.4

【问题讨论】:

    标签: python-3.x opencv


    【解决方案1】:

    要进行 py 绑定,请确保 cmake 配置输出接近末尾有 Python 3 部分,即

    Python 3:
        Interpreter:                 /home/user/anaconda3/envs/cv451/bin/python3 (ver 3.8.12)
        Libraries:                   /home/user/anaconda3/envs/cv451/lib/libpython3.8.so (ver 3.8.12)
        numpy:                       /home/user/anaconda3/envs/cv451/lib/python3.8/site-packages/numpy/core/include (ver 1.21.2)
        install path:                /home/user/anaconda3/envs/cv451/lib/python3.8/site-packages/cv2/python-3.8
    

    对我来说,那部分起初是缺失的。我的问题是 lib(从系统中找到)和解释器(从 conda env)之间的 python 版本不匹配。

    因此,在我的情况下,解决方法是:

    1. 让 cmake 找到 conda env 的 python 而不是系统的 python:
    CMAKE_LIBRARY_PATH=/home/user/anaconda3/envs/cv451/lib cmake /path/to/opencv/arc
    
    1. 默认情况下绑定安装到系统的python。要指定 python 绑定安装路径,请使用 cmake 标志 OPENCV_PYTHON3_INSTALL_PATH
    -D OPENCV_PYTHON3_INSTALL_PATH=/home/user/anaconda3/envs/cv451/lib/python3.8/site-packages/
    
    1. 记得编译后安装
    sudo make install
    

    参考:

    【讨论】:

      【解决方案2】:

      我想添加到 CYL 答案,我使用了 GitHub Gist 并且它有效,但我必须删除我所有的垃圾 cmake 标志。

      这是我最终的结果。

      cmake \
          -D CMAKE_BUILD_TYPE=RELEASE \
          -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \
          -D INSTALL_PYTHON_EXAMPLES=ON \
          -D INSTALL_C_EXAMPLES=ON \
          -D WITH_TBB=ON \
          -D WITH_CUDA=OFF \
          -D BUILD_opencv_cudacodec=OFF \
          -D ENABLE_FAST_MATH=1 \
          -D CUDA_FAST_MATH=1 \
          -D WITH_CUBLAS=1 \
          -D WITH_V4L=ON \
          -D WITH_QT=OFF \
          -D WITH_OPENGL=ON \
          -D WITH_GSTREAMER=ON \
          -D OPENCV_GENERATE_PKGCONFIG=ON \
          -D OPENCV_PC_FILE_NAME=opencv.pc \
          -D OPENCV_ENABLE_NONFREE=ON \
          -D OPENCV_PYTHON3_INSTALL_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
          -D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib/modules \
          -D PYTHON_EXECUTABLE=$(which python3) \
          -D BUILD_EXAMPLES=ON ..
      

      这是常规配置输出

      -- General configuration for OpenCV 4.5.5 =====================================
      --   Version control:               4.5.5
      -- 
      --   Extra modules:
      --     Location (extra):            /tmp/opencv_contrib/modules
      --     Version control (extra):     4.5.5
      -- 
      --   Platform:
      --     Timestamp:                   2022-01-23T08:47:55Z
      --     Host:                        Linux 5.11.0-41-generic x86_64
      --     CMake:                       3.18.2
      --     CMake generator:             Unix Makefiles
      --     CMake build tool:            /usr/bin/make
      --     Configuration:               RELEASE
      -- 
      --   CPU/HW features:
      --     Baseline:                    SSE SSE2 SSE3
      --       requested:                 SSE3
      --     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      --       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      --       SSE4_1 (18 files):         + SSSE3 SSE4_1
      --       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      --       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      --       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      --       AVX2 (33 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      --       AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
      -- 
      --   C/C++:
      --     Built as dynamic libs?:      YES
      --     C++ standard:                11
      --     C++ Compiler:                /usr/bin/c++  (ver 9.3.0)
      --     C++ flags (Release):         -fsigned-char -ffast-math -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 -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
      --     C++ flags (Debug):           -fsigned-char -ffast-math -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 -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
      --     C Compiler:                  /usr/bin/cc
      --     C flags (Release):           -fsigned-char -ffast-math -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 -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
      --     C flags (Debug):             -fsigned-char -ffast-math -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 -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
      --     Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
      --     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
      --     ccache:                      NO
      --     Precompiled headers:         NO
      --     Extra dependencies:          dl m pthread rt
      --     3rdparty dependencies:
      -- 
      --   OpenCV modules:
      --     To be built:                 aruco barcode bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
      --     Disabled:                    world
      --     Disabled by dependency:      -
      --     Unavailable:                 alphamat cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf java julia matlab ovis python2 sfm viz
      --     Applications:                tests perf_tests examples apps
      --     Documentation:               NO
      --     Non-free algorithms:         YES
      -- 
      --   GUI:                           NONE
      --     GTK+:                        NO
      --     OpenGL support:              NO
      --     VTK support:                 NO
      -- 
      --   Media I/O: 
      --     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
      --     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
      --     WEBP:                        build (ver encoder: 0x020f)
      --     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
      --     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
      --     JPEG 2000:                   build (ver 2.4.0)
      --     OpenEXR:                     build (ver 2.3.0)
      --     HDR:                         YES
      --     SUNRASTER:                   YES
      --     PXM:                         YES
      --     PFM:                         YES
      -- 
      --   Video I/O:
      --     DC1394:                      NO
      --     FFMPEG:                      YES
      --       avcodec:                   YES (58.54.100)
      --       avformat:                  YES (58.29.100)
      --       avutil:                    YES (56.31.100)
      --       swscale:                   YES (5.5.100)
      --       avresample:                YES (4.0.0)
      --     GStreamer:                   YES (1.16.2)
      --     v4l/v4l2:                    YES (linux/videodev2.h)
      -- 
      --   Parallel framework:            TBB (ver 2020.1 interface 11101)
      -- 
      --   Trace:                         YES (with Intel ITT)
      -- 
      --   Other third-party libraries:
      --     Intel IPP:                   2020.0.0 Gold [2020.0.0]
      --            at:                   /tmp/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
      --     Intel IPP IW:                sources (2020.0.0)
      --               at:                /tmp/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
      --     VA:                          NO
      --     Lapack:                      NO
      --     Eigen:                       NO
      --     Custom HAL:                  NO
      --     Protobuf:                    build (3.19.1)
      -- 
      --   OpenCL:                        YES (no extra features)
      --     Include path:                /tmp/opencv/3rdparty/include/opencl/1.2
      --     Link libraries:              Dynamic load
      -- 
      --   Python 3:
      --     Interpreter:                 /home/user/miniconda3/bin/python3 (ver 3.9.5)
      --     Libraries:                   /home/user/miniconda3/lib/libpython3.9.so (ver 3.9.5)
      --     numpy:                       /home/user/miniconda3/lib/python3.9/site-packages/numpy/core/include (ver 1.21.2)
      --     install path:                /home/user/miniconda3/lib/python3.9/site-packages/cv2/python-3.9
      -- 
      --   Python (for build):            /home/user/miniconda3/bin/python3
      -- 
      --   Java:                          
      --     ant:                         NO
      --     JNI:                         NO
      --     Java wrappers:               NO
      --     Java tests:                  NO
      -- 
      --   Install to:                    /home/user/miniconda3
      -- -----------------------------------------------------------------
      -- 
      -- Configuring done
      -- Generating done
      -- Build files have been written to: /tmp/opencv/build
      

      仅用于文档,这是我在 ubuntu 20.04 上安装的。

      apt-get update && apt-get install git openssh-client curl wget sudo \
          build-essential pkg-config libboost-system-dev libboost-thread-dev libboost-program-options-dev libboost-test-dev \
          libgl1-mesa-glx libglib2.0-0 \
          libjpeg-dev libpng-dev libtiff-dev \
          libavcodec-dev libavformat-dev libswscale-dev libavresample-dev \
          libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
          libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev \
          libfaac-dev libmp3lame-dev libvorbis-dev \
          libtbb-dev libatlas-base-dev gfortran -y
      

      【讨论】:

        【解决方案3】:

        cmake -DBUILD_LIST=python3 .. 解决了问题:获取干净的安装目录,包括 python 站点包目录。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-02
          • 2017-12-15
          相关资源
          最近更新 更多