如何为python安装OpenCV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3325528/
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-18 10:28:02  来源:igfitidea点击:

How to install OpenCV for python

pythonmacosopencv

提问by patrick

HI! I'm trying to install opencv and use it with python, but when I compile it I get no errors but I can't import cv module from python:

你好!我正在尝试安装 opencv 并将其与 python 一起使用,但是当我编译它时,我没有收到任何错误,但我无法从 python 导入 cv 模块:

patrick:release patrick$ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv

The code I used to compile it is this:

我用来编译它的代码是这样的:

cd opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON
make
sudo make install

how can I get it working with python?

我怎样才能让它与python一起工作?

采纳答案by patrick

I found here a way to install opencv for python: http://recursive-design.com/blog/2010/12/14/face-detection-with-osx-and-python/:)

我在这里找到了一种为 python 安装 opencv 的方法:http: //recursive-design.com/blog/2010/12/14/face-detection-with-osx-and-python/ :)

回答by Alex Martelli

You could try ctypes-opencv-- not sure why building and installing with -D BUILD_PYTHON_SUPPORT=ONdidn't work for you (maybe it doesn't know where to install the Python wrappers in osx...?), but the ctypes wrappers should, in theory, work anyway.

您可以尝试ctypes-opencv- 不知道为什么构建和安装 with-D BUILD_PYTHON_SUPPORT=ON对您不起作用(也许它不知道在 osx 中安装 Python 包装器的位置......?),但理论上 ctypes 包装器应该,反正工作。

回答by Jaime Ivan Cervantes

If you have you want a simple and quick install in Windows, you can download Python(x,y). This distribution includes OpenCv. Be sure to specify that you want to install OpenCV in the installation setup, because it is not installed by default.

如果您想在 Windows 中进行简单快速的安装,可以下载Python(x,y)。该发行版包括 OpenCv。请务必在安装设置中指定要安装 OpenCV,因为默认情况下不会安装它。

回答by Lri

brew tap homebrew/science
brew install opencv
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

回答by nodejh

We can install opencv for Python for Mac OS X with home-brew.

我们可以使用 home-brew 为 Mac OS X 安装 opencv for Python。

First, install home-brew:

首先,安装自制软件:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You can see the details for how to install homebrew. http://brew.sh

您可以查看有关如何安装自制软件的详细信息。http://brew.sh

If you don't install Python, install python(brew will install python2.7):

如果不安装Python,安装python(brew会安装python2.7):

brew install python

brew install python

Maybe you want install Python3: brew install python3

也许你想安装 Python3: brew install python3

Then install opencv3 for Python3:

然后为 Python3 安装 opencv3:

brew install opencv3 --with-python3

brew install opencv3 --with-python3

If you want install opencv3 for Python(Python2.7): brew install opencv3 --with-python

如果要为 Python(Python2.7) 安装 opencv3: brew install opencv3 --with-python

OR install opencv2 for Python3: brew install opencv --with-python3

或为 Python3 安装 opencv2: brew install opencv --with-python3

OR if you want install opencv2 for Python3: brew install opencv --with-python3

或者,如果您想为 Python3 安装 opencv2: brew install opencv --with-python3

Finally, maybe you will link site-packages of opencv to the site-packages of Python.

最后,也许你会将opencv 的 site-packages 链接到 Python 的 site-packages。

Notes: In the follow command, /usr/local/opt/opencv3/lib/python3.5/site-packagesis the directory of opencv3's site-packages, /usr/local/lib/python3.5/site-packages/is the directory of Python3.5's site-packages.

注:在下面的命令中,/usr/local/opt/opencv3/lib/python3.5/site-packages是opencv3的site-packages/usr/local/lib/python3.5/site-packages/目录,是Python3.5的site-packages目录。

Maybe you should change the two to your own OPENCV AND PYTHON site-packagesdirectories.

也许您应该将两者更改为您自己的 OPENCV 和 PYTHONsite-packages目录。

echo /usr/local/opt/opencv3/lib/python3.5/site-packages >> /usr/local/lib/python3.5/site-packages/opencv3.pth

echo /usr/local/opt/opencv3/lib/python3.5/site-packages >> /usr/local/lib/python3.5/site-packages/opencv3.pth

回答by Ive Ji

easy_install pip
pip install opencv-python --user
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

Maybe you will use sudo,

也许你会用sudo

and the path of installed may not be like mentioned.

并且安装的路径可能不像提到的那样。

回答by JobJob

This worked for me (change python36 to whatever version you want)

这对我有用(将 python36 更改为您想要的任何版本)

sudo port install opencv +avx2 +python36 +qt5 +contrib +eigen

sudo port install opencv +avx2 +python36 +qt5 +contrib +eigen

I got this errorand had to apply the patch there (download link)

我遇到了这个错误,不得不在那里应用补丁(下载链接

Apply the patch with:

应用补丁:

sudo patch /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-3.2.0/modules/highgui/src/window_QT.cpp ~/Downloads/patch-cpp11-narrowing-error.diff

Then run sudo port install -N opencv +avx2 +python36 +qt5 +contrib +eigenagain

然后sudo port install -N opencv +avx2 +python36 +qt5 +contrib +eigen再次运行

回答by HimanshuGahlot

When using Virtual Environment

使用虚拟环境时

Thanks to @user495470. Follow these steps

感谢@user495470。按着这些次序

brew update
brew install -v cmake 
brew install opencv`

If Part 1 didn't work kindly follow Part 2

Part I
Next step might work sometime, although it didn't work for me
export PYTHONPATH="/VENV_PATH/python2.7/site-packages:$PYTHONPATH"
Then check in python IDE check with import cvor import cv2

Part 2
Go to this path /usr/local/Cellar/opencv/3.4.3/lib/python2.7/site-packages/or /usr/local/lib/python2.7/site-packages
Copy cv2.sofile
Paste it /VENV_PATH/lib/python2.7/site-packageshere
Then check in python IDE check with import cvor import cv2

Kindly let me know if this thing works.

如果第 1 部分不起作用,请遵循第 2

部分第 I 部分
下一步可能会在某个时候起作用,尽管它对我不起作用
export PYTHONPATH="/VENV_PATH/python2.7/site-packages:$PYTHONPATH"
然后检查 python IDE 检查import cvimport cv2

第 2 部分
转到此路径 /usr/local/Cellar/opencv/3.4.3/lib/python2.7/site-packages//usr/local/lib/python2.7/site-packages
复制cv2.so文件
粘贴到/VENV_PATH/lib/python2.7/site-packages此处
然后检查python IDE 检查import cvimport cv2

请让我知道这件事是否有效。