OpenCV仍然不适用于Python 3.3并且我是否真的必须降级到Python 2.7才能使用它?我没有在互联网上找到太多关于它,只有2012年的一些帖子,OpenCV尚未移植到Python 3.x中使用。但现在是2014年,在尝试安装最新的OpenCV 2.4.x并将cv2.pyd
文件复制到 C:\ Program Files(x86)\ Python333 \ Lib \ site-packages 之后在Python IDLE中产生错误:
>>> import cv2
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cv2
ImportError: DLL load failed: %1 ist keine zulässige Win32-Anwendung.
答案 0 :(得分:43)
编辑:首先尝试新的点子方法:
Windows:pip3 install opencv-python opencv-contrib-python
Ubuntu:sudo apt install python3-opencv
或继续下面的构建说明
注意:最初的问题是要求OpenCV + Python 3.3 + Windows。从那时起,Python 3.5已经发布。另外,我使用Ubuntu进行大多数开发,所以这个答案将集中在该设置上,不幸的是
OpenCV 3.1.0 + Python 3.5.2 + Ubuntu 16.04是可能的!这是怎么回事。
从以下位置复制(并稍加修改)这些步骤:
安装所需的依赖项,并可选择在系统上安装/更新某些库:
# Required dependencies
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# Dependencies for Python bindings
# If you use a non-system copy of Python (eg. with pyenv or virtualenv), then you probably don't need to do this part
sudo apt install python3.5-dev libpython3-dev python3-numpy
# Optional, but installing these will ensure you have the latest versions compiled with OpenCV
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
有几个标志和选项可以调整你的OpenCV版本。可能有关于它们的全面文档,但这里有一些可能有用的有趣标志。它们应包含在cmake
命令中:
# Builds in TBB, a threading library
-D WITH_TBB=ON
# Builds in Eigen, a linear algebra library
-D WITH_EIGEN=ON
如果您有多个版本的Python(例如,使用pyenv或virtualenv),那么您可能希望针对某个Python版本进行构建。默认情况下,OpenCV将为系统的Python版本构建。您可以通过将这些参数添加到脚本中稍后看到的cmake
命令来更改此设置。实际值取决于您的设置。我使用pyenv
:
-D PYTHON_DEFAULT_EXECUTABLE=$HOME/.pyenv/versions/3.5.2/bin/python3.5
-D PYTHON_INCLUDE_DIRS=$HOME/.pyenv/versions/3.5.2/include/python3.5m
-D PYTHON_EXECUTABLE=$HOME/.pyenv/versions/3.5.2/bin/python3.5
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1
CMakeLists文件将尝试检测要构建的各种Python版本。如果你在这里有不同的版本,它可能会感到困惑。上述参数可能只“解决”一个版本的Python而不是另一个版本的问题。如果你只关心那个特定的版本,那么没有什么可担心的了。
对我而言,遗憾的是,我还没有研究如何解决其他Python版本的问题。
# Clone OpenCV somewhere
# I'll put it into $HOME/code/opencv
OPENCV_DIR="$HOME/code/opencv"
OPENCV_VER="3.1.0"
git clone https://github.com/opencv/opencv "$OPENCV_DIR"
# This'll take a while...
# Now lets checkout the specific version we want
cd "$OPENCV_DIR"
git checkout "$OPENCV_VER"
# First OpenCV will generate the files needed to do the actual build.
# We'll put them in an output directory, in this case "release"
mkdir release
cd release
# Note: This is where you'd add build options, like TBB support or custom Python versions. See above sections.
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local "$OPENCV_DIR"
# At this point, take a look at the console output.
# OpenCV will print a report of modules and features that it can and can't support based on your system and installed libraries.
# The key here is to make sure it's not missing anything you'll need!
# If something's missing, then you'll need to install those dependencies and rerun the cmake command.
# OK, lets actually build this thing!
# Note: You can use the "make -jN" command, which will run N parallel jobs to speed up your build. Set N to whatever your machine can handle (usually <= the number of concurrent threads your CPU can run).
make
# This will also take a while...
# Now install the binaries!
sudo make install
默认情况下,即使您已指定要使用的自定义Python版本,install
脚本也会将Python绑定放在某个系统位置。修复很简单:将符号链接放到本地site-packages
中的绑定:
ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so $HOME/.pyenv/versions/3.5.2/lib/python3.5/site-packages/
第一条路径取决于您设置的Python版本。第二个取决于您的自定义Python版本所在的位置。
好的,试一试吧!
ipython
Python 3.5.2 (default, Sep 24 2016, 13:13:17)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import cv2
In [2]: img = cv2.imread('derp.png')
i
In [3]: img[0]
Out[3]:
array([[26, 30, 31],
[27, 31, 32],
[27, 31, 32],
...,
[16, 19, 20],
[16, 19, 20],
[16, 19, 20]], dtype=uint8)
答案 1 :(得分:13)
是支持Python 3,它在当前版本中仍然不可用,但它可以从版本3.0, (see this ticket)获得。如果你真的想让python 3尝试使用开发版,你可以下载它from GitHub。
编辑(2015年7月18日): 版本3.0现已发布,python 3 support is now officially available
答案 2 :(得分:13)
这是一个解决方案(我相信在下面的链接中可以看到&#39; cp34&#39;)Python 3.4。
转到http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv。
下载合适的.whl。
转到保存.whl的目录。
使用pip安装.whl。例如pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
然后使用import cv2
。
答案 3 :(得分:7)
使用pip应用程序。在Windows上,您可以在Python3/Scripts/pip.exe
和On Ubuntu中找到它,您可以使用apt-get install python3-pip
进行安装。
所以,使用命令行:
pip3 install --upgrade pip
pip3 install opencv-python
在Windows上,只使用pip.exe而不是pip3
答案 4 :(得分:6)
由于缺乏声誉,我无法对midopa的优秀answer发表评论。
在Mac上我(终于)使用以下命令成功安装了opencv from source:
cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local
-D PYTHON_EXECUTABLE=/Library/Frameworks/Python.framework/Versions/3.4/bin/python3
-D PYTHON_LIBRARY=/Library/Frameworks/Python.framework//Versions/3.4/lib/libpython3.4m.dylib
-D PYTHON_INCLUDE_DIR=/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m
-D PYTHON_NUMPY_INCLUDE_DIRS=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/core/include/numpy
-D PYTHON_PACKAGES_PATH=/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
/relative/path/to/source/directory/
然后,
make -j8
为机器可以处理的线程数更改8,以加快速度
sudo make install
我在PYTHONPATH
文件中添加了~/.bash_profile
环境变量,以便Python找到cv2.so
:
PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python3.4/site-packages”
export PYTHONPATH
[对于那些使用PyCharm的人,我必须转到首选项&gt;项目结构&gt;添加内容根,并添加了cv2.so
的父目录的路径:/usr/local/lib/python3.4/site-packages
]
这个命令让我错过了以下错误:
Could NOT find PythonLibs
,通过明确声明python库路径
ld: can't link with a main executable for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [lib/cv2.so] Error 1
make[1]: *** [modules/python2/CMakeFiles/opencv_python2.dir/all] Error 2
make: *** [all] Error 2
明确指向libpython3.4m.dylib
在终端中,检查它是否有效:
$python3
>>> import cv2
如果你没有得到ImportError: No module named 'cv2'
这适用于Macbook Pro Retina 15&#34; 2013年,Mavericks 10.9.4,Python 3.4.1(以前从official download安装),opencv3来自源代码。希望这有助于某人。
答案 5 :(得分:6)
I know this is an old thread, but just in case anyone is looking, here is how I got it working on El Capitan:
brew install opencv3 --with-python3
and wait a while for it to finish.
Then run the following as necessary:
brew unlink opencv
Then run the following as the final step:
brew ln opencv3 --force
Now you should be able to run import cv2
no problem in a python 3.x script.
答案 6 :(得分:3)
使用python3绑定和虚拟环境让opencv 3.0在OSX上运行时遇到了很多麻烦。其他答案帮助很大,但仍然需要一些。希望这将有助于下一个人。将其保存到build_opencv.sh
。然后download opencv,修改下面shell脚本中的变量,交叉手指,然后运行它(. ./build_opencv.sh
)。要进行调试,请使用其他帖子,尤其是James Fletchers。
不要忘记将opencv lib目录添加到PYTHONPATH。
注意 - 这也会下载opencv-contrib,其中许多功能已被移动。现在,它们也被文档引用了不同的命名空间 - 例如,SIFT现在位于cv2.xfeatures2d.SIFT_create下。 Uggh。
#!/bin/bash
# Install opencv with python3 bindings: https://stackoverflow.com/questions/20953273/install-opencv-for-python-3-3/21212023#21212023
# First download opencv and put in OPENCV_DIR
#
# Edit this section
#
PYTHON_DIR=/Library/Frameworks/Python.framework/Versions/3.4
OPENCV_DIR=/usr/local/Cellar/opencv/3.0.0
NUM_THREADS=8
CONTRIB_TAG="3.0.0" # This will also download opencv_contrib and checkout the appropriate tag https://github.com/Itseez/opencv_contrib
#
# Run it
#
set -e # Exit if error
cd ${OPENCV_DIR}
if [[ ! -d opencv_contrib ]]
then
echo '**Get contrib modules'
[[ -d opencv_contrib ]] || mkdir opencv_contrib
git clone git@github.com:Itseez/opencv_contrib.git .
git checkout ${CONTRIB_TAG}
else
echo '**Contrib directory already exists. Not fetching.'
fi
cd ${OPENCV_DIR}
echo '**Going to do: cmake'
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON_EXECUTABLE=${PYTHON_DIR}/bin/python3 \
-D PYTHON_LIBRARY=${PYTHON_DIR}/lib/libpython3.4m.dylib \
-D PYTHON_INCLUDE_DIR=${PYTHON_DIR}/include/python3.4m \
-D PYTHON_NUMPY_INCLUDE_DIRS=${PYTHON_DIR}/lib/python3.4/site-packages/numpy/core/include/numpy \
-D PYTHON_PACKAGES_PATH=${PYTHON_DIR}lib/python3.4/site-packages \
-D OPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules \
-D BUILD_opencv_legacy=OFF \
${OPENCV_DIR}
echo '**Going to do: make'
make -j${NUM_THREADS}
echo '**Going to do: make install'
sudo make install
echo '**Add the following to your .bashrc: export PYTHONPATH=${PYTHONPATH}:${OPENCV_DIR}/lib'
export PYTHONPATH=${PYTHONPATH}:${OPENCV_DIR}/lib
echo '**Testing if it worked'
python3 -c 'import cv2'
echo 'opencv properly installed with python3 bindings!' # The script will exit if the above failed.
答案 7 :(得分:3)
花了3个小时尝试在这里提到的Ubuntu 14.04LTS和另一个引用的tutorial上的各种选项无济于事。有一段时间尝试使用OpenCV3.0.0但最终切换到3.1.0。以下工作:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.4m.so \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.4m \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.4m/ \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.4m/ \
-D PYTHON3_INCLUDE_DIRS=/usr/include/python3.4m/ \
-D PYTHON_INCLUDE_DIRS=/usr/include/python3.4m/ \
-D BUILD_opencv_python3=ON \
.
输出:
-- OpenCV modules:
-- To be built: core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab python3
-- Disabled: java world
-- Disabled by dependency: -
-- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev python2 viz
-- Python 3:
-- Interpreter: /usr/bin/python3.4m (ver 3.4.3)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
-- packages path: /usr/lib/python3/dist-packages
--
-- Python (for build):
使用virtualenv使用以下cmake选项:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$VIRTUAL_ENV \
-D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python3 \
-D PYTHON3_PACKAGES_PATH=$VIRTUAL_ENV/lib/python3.4/site-packages \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.4m.so \
-D PYTHON3_INCLUDE_DIR=$VIRTUAL_ENV/include/python3.4m \
-D PYTHON_INCLUDE_DIR=$VIRTUAL_ENV/include/python3.4m \
-D PYTHON3_INCLUDE_DIRS=$VIRTUAL_ENV/include/python3.4m \
-D PYTHON_INCLUDE_DIRS=$VIRTUAL_ENV/include/python3.4m \
-D BUILD_opencv_python3=ON \
.
如果您遇到ffmpeg问题,请添加以下内容以删除视频支持:
-D WITH_FFMPEG=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_V4L=OFF \
-D WITH_1394=OFF \
还要注意cmake关于使用make clean
的警告。如果你运行make clean,你可能需要重新解压缩原始包。 Cmake死了,Cmake持续了很长时间。
答案 8 :(得分:2)
可以找到有关James Fletcher答案的完整说明here
特别是对于Anaconda发行版,我不得不这样修改它:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON3_PACKAGES_PATH=/anaconda/lib/python3.4/site-packages/ \
-D PYTHON3_LIBRARY=/anaconda/lib/libpython3.4m.dylib \
-D PYTHON3_INCLUDE_DIR=/anaconda/include/python3.4m \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
可以省略最后一行(参见上面的链接)
答案 9 :(得分:1)
如果你在这里...我很抱歉其他选择没有锻炼。试试这个:
conda install -c menpo opencv3
来自Scivision's Tutorial的第1步。如果这不起作用,那么继续第2步:
(仅限Windows)OpenCV 3.2点安装
Download OpenCV .whl file here。在其名称中提及
contrib
的包包括 OpenCV-extra软件包。例如,假设您有Python 3.6,那么 可能会下载opencv_python-3.2.0+contrib-cp36-none-win_amd64.whl
以获取OpenCV-extra软件包。然后,从命令提示符:
pip install opencv_python-3...yourVersion...win_amd64.whl
请注意,该教程中步骤2的...win_amd64.whl
轮包适用于AMD芯片。
答案 10 :(得分:1)
有人为此发布了一个docker容器/文件:
https://github.com/vipul-sharma20/docker-opencv3-python3
https://hub.docker.com/r/vipul20/docker-opencv3-python3/~/dockerfile/
您可以直接从docker hub提取图像,也可以按照Dockerfile中的说明进行安装。
答案 11 :(得分:1)
以防万一您发现pip3 install opencv-python对您来说太长时间,您可以设置构建线程数:
export MAKEFLAGS="-j8"
pip3 install opencv-python --no-cache-dir
(--no-cache-dir 将忽略之前的构建)
答案 12 :(得分:0)
无论您是手动安装opencv3还是从Gohlke的whl软件包安装,我都发现需要在site_packages中创建/编辑文件cv.py,如下所示,以便与旧代码兼容:
import cv2 as cv
答案 13 :(得分:0)
您可以在 Windows 上的命令提示符( cmd )上使用以下命令:
py -3.3 -m pip install opencv-python
关于如何在 1分钟中在Windows上安装OpenCV Python,视频:
https://www.youtube.com/watch?v=m2-8SHk-1SM
希望它有所帮助!
答案 14 :(得分:0)
对于 Ubuntu - pip3 install opencv-python
sudo apt-get install python3-opencv