首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python绑定从源代码构建OpenCV?

如何使用python绑定从源代码构建OpenCV?
EN

Stack Overflow用户
提问于 2021-12-13 11:43:13
回答 3查看 2.1K关注 0票数 1

我需要从源代码构建OpenCV,而且空间有限,所以我必须在模块的基础上构建OpenCV (reference.html -构建有限的模块集)。好的,做好,做得好。

通过python可以更容易地使用这些OpenCV模块,因此我添加了cmake -D BUILD_opencv_python3=ON,但是make install并没有创建我期望的prefix/lib/pythonX.Y/site-packages目录。据我所知,这里没有与python相关的选项,只有一个CMake选项。

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

更新

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

代码语言:javascript
复制
--   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输出(空白安装路径)中仍然没有添加任何内容,但是

代码语言:javascript
复制
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 -我没有根权限,需要本地安装),我在本地安装中没有python绑定。

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

注:在L4T ubuntu上编译opencv-4.5.4

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-12-17 10:15:36

cmake -DBUILD_LIST=python3 ..解决了这个问题:获得干净的安装目录,包括python packages目录。

票数 0
EN

Stack Overflow用户

发布于 2022-01-23 09:01:29

我想添加到赛尔的答案,我使用的GitHub吉斯特和它的工作,但我必须放下我所有的垃圾cmake标志。

这是我最终的结局。

代码语言:javascript
复制
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 ..

这是通用的Config输出

代码语言:javascript
复制
-- 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

仅作为文档,这就是我在ubuntu20.04上安装的内容。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2021-12-15 09:10:29

若要进行py绑定,请确保接近末尾的config输出具有Python 3节,即

代码语言:javascript
复制
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

对我来说,那部分一开始就不见了。我的问题是python版本在lib (从系统中找到)和解释器(来自conda env)之间不匹配。

因此,就我的情况而言,解决办法是:

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

参考:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70334087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档