为 Python 安装 OpenCV(多个 Python 版本)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17287250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:48:03  来源:igfitidea点击:

Install OpenCV for Python (multiple python versions)

pythonopencvpython-2.7

提问by Mohammad Moghimi

I have two different versions of python installed on my machine: 2.4 and 2.7. I'm trying to install OpenCV(2.4.5) for the 2.7 version.

我的机器上安装了两个不同版本的 python:2.4 和 2.7。我正在尝试为 2.7 版本安装 OpenCV(2.4.5)。

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..

It detects the python 2.4 as the current installation:

它将 python 2.4 检测为当前安装:

--   Python:
--     Interpreter:                 /usr/bin/python2.4 (ver 2.4)
--     Libraries:                   /usr/lib64/python2.4/config/libpython2.4.a
--     numpy:                       /usr/lib64/python2.4/site-packages/numpy/core/include (ver 1.2.1)
--     packages path:               lib/python2.4/site-packages

and later in building opencv gives me this error:

后来在构建 opencv 时给了我这个错误:

[ 75%] Generating pyopencv_generated_funcs.h, pyopencv_generated_func_tab.h, pyopencv_generated_types.h, pyopencv_generated_type_reg.h, pyopencv_generated_const_reg.h
  File "/home/mmoghimi/opencv-2.4.5/modules/python/src2/gen2.py", line 815
    cname1=("cv::Algorithm" if classinfo.isalgorithm else classinfo.cname)))
                             ^
SyntaxError: invalid syntax
make[2]: *** [modules/python/pyopencv_generated_funcs.h] Error 1
make[1]: *** [modules/python/CMakeFiles/opencv_python.dir/all] Error 2
make: *** [all] Error 2

apparently it uses a new format that python2.4 does not support. So, my question is that is there any way to explicitly specify the version of python?

显然它使用了 python2.4 不支持的新格式。所以,我的问题是有没有办法明确指定python的版本?

采纳答案by Aurelius

There are some Cmake flags which allow you to explicitly specify which version of Python to use. You will need to set the values of these flags to the correct location for your installation of Python.

有一些 Cmake 标志允许您明确指定要使用的 Python 版本。您需要将这些标志的值设置为安装 Python 的正确位置。

The flag names and likely locations are below:

标志名称和可能的位置如下:

PYTHON_EXECUTABLE=/usr/bin/python2.7/
PYTHON_INCLUDE=/usr/include/python2.7/
PYTHON_LIBRARY=/usr/lib/libpython2.7.a    //or .so for shared library
PYTHON_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages/
PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python2.7/dist-packages/numpy/core/include

If these paths don't work, you will need to locate them on your machine.

如果这些路径不起作用,您将需要在您的机器上找到它们。

回答by Sofia Velmer

Use virtualenv

使用虚拟环境

virtualenv -p python2.7 env
source env/bin/activate
python --version  # prints ?Python 2.7.3?
pip install pyopencv

If you need support of 2.4 (or other version), just create new environment.

如果您需要 2.4(或其他版本)的支持,只需创建新环境。

回答by lucidbrot

This is a low-quality answer but it is still very useful if you have been trying for ten hours and need some new ideas.

这是一个低质量的答案,但如果您已经尝试了十个小时并且需要一些新想法,它仍然非常有用。



This issue took me a day and I still am unsure how I solved it, but here are a few pointers in case the given answers alone don't help:

这个问题花了我一天的时间,我仍然不确定我是如何解决它的,但这里有一些提示,以防单独给出的答案没有帮助:

Originally, I was following this guidewhich can be summarized as follows:

最初,我遵循本指南,可总结如下:

sudo apt-get update && sudo apt-get upgrade && sudo apt-get install build-essential git cmake pkg-config libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk2.0-dev libatlas-base-dev gfortran

git clone https://github.com/Itseez/opencv.git && cd opencv &&git checkout 3.0.0

sudo apt-get install python2.7-dev && sudo apt-get install python3-dev

cd ~ && wget https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py

pip3.6 install numpy               # note that we specify the pip version after the command
                                   # it is also possible to write python3.6 to refer to the python version 3.6 command

# The following failed for me. Use it if it works for you.

cd ~/opencv && mkdir build && cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE 
 -D CMAKE_INSTALL_PREFIX=/usr/local 
 -D INSTALL_PYTHON_EXAMPLES=ON 
 -D INSTALL_C_EXAMPLES=ON 
 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules 
 -D BUILD_EXAMPLES=ON ..

# This must be done after cmake worked. Pay attention to the start of the cmake output. It tells you whether it found the Python Interpreter.


cd ~/opencv && mkdir build && cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE 
 -D CMAKE_INSTALL_PREFIX=/usr/local 
 -D INSTALL_PYTHON_EXAMPLES=ON 
 -D INSTALL_C_EXAMPLES=ON 
 -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules 
 -D BUILD_EXAMPLES=ON ..

I had python 2.7 and 3.4 installed already, and built python 3.6 myself in /usr/local/bin. After following Aurelius' Answerand some trying out, I built this cmake command:

我已经安装了 python 2.7 和 3.4,并在/usr/local/bin. 在遵循Aurelius 的回答并进行了一些尝试之后,我构建了这个 cmake 命令:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON -D PYTHON_EXECUTABLE=/usr/local/bin/python3.6/ -D PYTHON_INCLUDE=/usr/local/include/python3.6m/ -D PYTHON_LIBRARY=/usr/local/lib/python3.6/ -D PYTHON_PACKAGES_PATH=/usr/local/lib/python3.6/site-packages/ -D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ -D PYTHON_INCLUDE_DIR=/usr/local/include/python3.6m/ -D PYTHON_LIBRARY=/usr/local/lib/  -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON3_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON -D PYTHON3_INCLUDE=/usr/local/include/python3.6m/ -D PYTHON3_LIBRARY=/usr/local/lib/python3.6/ -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/site-packages/ -D PYTHON3_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ -D PYTHON3_INCLUDE_DIR=/usr/local/include/python3.6m/ -D PYTHON3_LIBRARY=/usr/local/lib/ ..

Note that it is likely that many of these do not make sense. I was mindlessly trying out until I got something to work. Some of these pathes can be found using python itselfsuch as

请注意,其中许多可能没有意义。我一直在无意识地尝试,直到我找到了工作。 其中一些路径可以使用 python 本身找到,例如

python3.6 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"

or directly in the cmake command:

或直接在 cmake 命令中:

$ cmake .. \
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  \
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")

At this point, cmake was finding the correct python interpreter and libraries... but then failed, saying it did not find them. I can only quote the error from memory since I cannot scroll far enough back in tmux and I do not want to reproduce the error.

此时,cmake 正在找到正确的 python 解释器和库……但后来失败了,说它没有找到它们。我只能从内存中引用错误,因为我无法在 tmux 中向后滚动足够远,而且我不想重现错误。

opencv found unsuitable version "1.4" but required is at least 3.6

opencv 发现不合适的版本“1.4”但需要至少 3.6

The solution to this is telling cmake exactly what you wantby modifying the file build/../cmake/OpenCVDetectPython.cmaketo contain the following checks somewhere at the start:

解决这个问题的方法是通过修改文件在开始的某处包含以下检查来告诉 cmake 你想要什么build/../cmake/OpenCVDetectPython.cmake

find_package( PythonInterp 3.6 REQUIRED )
find_package( PythonLibs 3.6 REQUIRED )

(Note that these are deprecated)

(请注意,这些已弃用

It still did not work, and that was because I had set the cmake flag -D PYTHON3_EXECUTABLE. This e-mail chainwas helpful for figuring that out and also suggests using virtualenv as an alternative solution. Which is probably what I should have started with.

它仍然不起作用,那是因为我设置了 cmake flag -D PYTHON3_EXECUTABLE这个电子邮件链有助于解决这个问题,还建议使用 virtualenv 作为替代解决方案。这可能是我应该开始的。