Install OpenCV for python3

≯℡__Kan透↙ 提交于 2020-01-02 18:16:31

问题


I have followed many manuals/tutorials how to install OpenCV, but all seem to work for my python2.7 instead of python3.4 where I want it. I'm following this tutorial but without using virtualenv. When making the

$cmake \                                                                                                                  
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-D PYTHON_EXECUTABLE=/usr/bin/python3.4 \
-D PYTHON_PACKAGES_PATHS=/usr/local/lib/python3.4/dist-packages/ \
-D PYTHON_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.4/dist-packages/numpy/core/include ..

comand it list both versions:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 3.4.3)
--     Libraries:                   NO
--     numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   NO
--     numpy:                       /usr/local/lib/python3.4/dist-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python3.4/dist-packages
-- 
--   Python (for build):            /usr/bin/python2.7

But omits the python_executable flag and uses the python2.7 for building (I checked it worked on python2.7 after continuing with the installation).

How can I make it that it uses python3.4 for the build?

Things I tried:

When running this cmake:

cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \
-D PYTHON_EXECUTABLE=$(which python3) ..

It list correctly the libraries:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 3.4.3)
--     Libraries:                   NO
--     numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /usr/local/lib/python3.4/dist-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python3.4/dist-packages
-- 
--   Python (for build):            /usr/bin/python2.7

But still list the python2.7 to build for.

Related info:

$whereis python3                                                                                                          
python3: /usr/bin/python3.4dm-config /usr/bin/python3.4m /usr/bin/python3.4m-config /usr/bin/python3.4-config /usr/bin/python3 /usr/bin/python3.4-dbg-config /usr/bin/python3.4 /usr/bin/python3.4-dbg /usr/bin/python3.4dm /etc/python3 /etc/python3.4 /usr/lib/python3.0 /usr/lib/python3.5 /usr/lib/python3 /usr/lib/python3.4 /usr/lib/python3.2 /usr/lib/python3.1 /usr/lib/python3.3 /usr/bin/X11/python3.4dm-config /usr/bin/X11/python3.4m /usr/bin/X11/python3.4m-config /usr/bin/X11/python3.4-config /usr/bin/X11/python3 /usr/bin/X11/python3.4-dbg-config /usr/bin/X11/python3.4 /usr/bin/X11/python3.4-dbg /usr/bin/X11/python3.4dm /usr/local/lib/python3.4 /usr/include/python3.4m /usr/include/python3.4 /usr/include/python3.4dm /usr/share/python3 /usr/share/man/man1/python3.1.gz

回答1:


I found the answer, after removing the CMakeCache.txt rm CMakeCache.txtI rerun the cmake command:

cmake  \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \
-D PYTHON_EXECUTABLE=/usr/bin/python3.4 \
-D BUILD_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D INSTALL_PYTHON_EXAMPLES=ON ..

And the output was:

--   Python 2:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /usr/local/lib/python3.4/dist-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python3.4/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /usr/local/lib/python3.4/dist-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python3.4/dist-packages
-- 
--   Python (for build):            /usr/bin/python3.4
-- 

So I keep with the instalation:

make -j4
sudo make install
sudo ldconfig


来源:https://stackoverflow.com/questions/36201282/install-opencv-for-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!