gpt4 book ai didi

python - 为 Python 3.3 安装 opencv

转载 作者:IT老高 更新时间:2023-10-28 20:23:07 26 4
gpt4 key购买 nike

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.

最佳答案

编辑:首先尝试新的 pip 方法:

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 标志

有几个标志和选项可以调整您的 OpenCV 构建。可能有关于它们的综合文档,但这里有一些可能有用的有趣标志。它们应该包含在 cmake 命令中:

# Builds in TBB, a threading library
-D WITH_TBB=ON
# Builds in Eigen, a linear algebra library
-D WITH_EIGEN=ON

使用非系统级 Python 版本

如果您有多个 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

CMake Python 错误消息

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

默认情况下,install 脚本会将 Python 绑定(bind)放在某个系统位置,即使您已指定要使用的自定义 Python 版本。修复很简单:将符号链接(symbolic link)放到本地 site-packages 中的绑定(bind):

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)

关于python - 为 Python 3.3 安装 opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20953273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com