tim*_*tim 57 python opencv python-3.x
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.
Run Code Online (Sandbox Code Playgroud)
小智 44
编辑:首先尝试新的点子方法:
视窗: 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
Run Code Online (Sandbox Code Playgroud)
有几个标志和选项可以调整你的OpenCV版本.可能有关于它们的全面文档,但这里有一些可能有用的有趣标志.它们应该包含在cmake命令中:
# Builds in TBB, a threading library
-D WITH_TBB=ON
# Builds in Eigen, a linear algebra library
-D WITH_EIGEN=ON
Run Code Online (Sandbox Code Playgroud)
如果您有多个版本的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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
默认情况下,install即使您已指定要使用的自定义Python版本,脚本也会将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/
Run Code Online (Sandbox Code Playgroud)
第一条路径取决于您设置的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)
Run Code Online (Sandbox Code Playgroud)
Paw*_*ech 13
是对Python 3的支持它在当前版本中仍然不可用,但它将从3.0版本开始提供(请参阅此票证).如果你真的想让python 3尝试使用开发版,你可以从GitHub下载它.
编辑(2015年7月18日):3.0版现已发布,python 3支持现已正式推出
use*_*622 13
这里有一个解决方案(我相信在下面的链接中可以看到'cp34')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.
小智 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
由于缺乏声誉,我不能对midopa的优秀答案发表评论.
在Mac上我(最终)使用以下命令从源代码成功安装了opencv :
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/
Run Code Online (Sandbox Code Playgroud)
然后,
make -j8
Run Code Online (Sandbox Code Playgroud)
将机器可以处理的线程数更改为8,以加快速度
sudo make install
Run Code Online (Sandbox Code Playgroud)
我PYTHONPATH在我的~/.bash_profile文件中添加了一个环境变量,以便Python可以找到cv2.so:
PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python3.4/site-packages”
export PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
[对于那些使用PyCharm的人,我必须转到首选项>项目结构>添加内容根,并添加了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
Run Code Online (Sandbox Code Playgroud)
通过明确指向libpython3.4m.dylib
在终端中,检查它是否有效:
$python3
>>> import cv2
Run Code Online (Sandbox Code Playgroud)
如果你没有得到,这一切都很好 ImportError: No module named 'cv2'
这适用于Macbook Pro Retina 15"2013,Mavericks 10.9.4,Python 3.4.1(以前从官方下载安装),opencv3来源.希望这有助于某人.
我知道这是一个老线程,但万一有人在看,这就是我在El Capitan工作的方式:
brew install opencv3 --with-python3
等待一段时间才能完成.
然后根据需要运行以下命令:
brew unlink opencv
然后运行以下作为最后一步:
brew ln opencv3 --force
现在你应该能够import cv2在python 3.x脚本中运行没有问题.
| 归档时间: |
|
| 查看次数: |
112110 次 |
| 最近记录: |