我尝试从源代码安装 OpenCV。它可以完美安装 python 2.7 和 python 3.5,但无法安装 python 3.6。
我使用以下命令构建它:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON_EXECUTABLE=/usr/bin/python3.6 ..
输出显示:
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.5.2)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
-- numpy: /home/courts/.local/lib/python3.5/site-packages/numpy/core/include (ver 1.12.0)
-- packages path: lib/python3.5/dist-packages
--
-- Python (for build): /usr/bin/python3
当我make install
使用 OpenCV 库时,它出现在 python3.5 的 dist-packages 文件夹中,但没有出现在 python3.6 的 dist-packages 文件夹中。
这是在构建日志中:
-- Found PythonInterp: /usr/bin/python3.6 (found suitable version "3.6.2", minimum required is "2.7")
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.5.2", minimum required is "3.4")
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.5m.so (found suitable exact version "3.5.2")
答案1
我通过运行以下命令列出了所有编译器标志:
cmake -L | awk '{if(f)print} /-- Cache values/{f=1}'
使用看起来有用的标志,我构建了这个:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/ \
-D PYTHON_EXECUTABLE=/usr/bin/python3.6 \
-D PYTHON_INCLUDE=/usr/include/python3.6/ \
-D PYTHON_LIBRARY=/usr/lib/python3.6/ \
-D PYTHON_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages/ \
-D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ \
..
这有效。
答案2
关注后这些步骤首先要在 Ubuntu 16 上安装 Python 3.6,你必须执行以下操作来编译 OpenCV仅有的适用于 Python 3.6。已针对 OpenCV 3.4.3 进行了测试。
curl -L https://github.com/opencv/opencv/archive/3.4.3.zip -o opencv.zip
curl -L https://github.com/opencv/opencv_contrib/archive/3.4.3.zip -o opencvContrib.zip
unzip -q opencvContrib.zip
unzip -q opencv.zip && cd opencv-3.4.3/ && mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=RELEASE \
-DBUILD_opencv_python3=yes \
-DCMAKE_INSTALL_PREFIX=/usr/local/ \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.3/modules \
-DPYTHON3_EXECUTABLE=/usr/bin/python3.6 \
-DPYTHON3_INCLUDE=/usr/include/python3.6/ \
-DPYTHON3_INCLUDE_DIR=/usr/include/python3.6m \
-DPYTHON3_LIBRARY=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so \
-DPYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages/ \
-DPYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packageis/numpy/core/ \
-DBUILD_NEW_PYTHON_SUPPORT=ON
make -j 4 && make install && cd / && rm opencv.zip && rm opencvContrib.zip && rm -rf opencv-3.4.3/