如何通过 Conda 安装 Python OpenCV?

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

How do I install Python OpenCV through Conda?

pythonopencvanacondaconda

提问by Chet

I'm trying to install OpenCV for Python through Anaconda, but I can't seem to figure this out.

我正在尝试通过Anaconda为 Python 安装 OpenCV ,但我似乎无法弄清楚。

I tried

我试过

conda install opencv
conda install cv2

I also tried searching

我也试过搜索

conda search cv

No cigar. I ran across this which lists opencvas an included package:

没有雪茄。我遇到了这个,它opencv列为一个包含的包:

http://docs.continuum.io/anaconda/pkgs.html

http://docs.continuum.io/anaconda/pkgs.html

After running conda infoI noticed my version is 3.4.1, but I couldn't seem to find any information about this version online. I'm pretty confused about this.

运行后conda info我注意到我的版本是3.4.1,但我似乎无法在网上找到有关此版本的任何信息。我对此很困惑。

Am I missing something pretty obvious here? If opencvwas available for a previous version of Anaconda, then why wouldn't it be available for the newer version? And why does that link only show me documentation for version 1.9.2?

我在这里错过了一些很明显的东西吗?如果opencvAnaconda 的先前版本可用,那么为什么它不适用于较新版本?为什么该链接只显示 1.9.2 版的文档?

回答by eculeus

It doesn't seem like the page you linked includes opencvany more. (Funny, I do recall it being included at a previous point as well.)

您链接的页面似乎不再包含opencv任何内容。(有趣,我记得它也被包含在前一点。)

In any case, installation of OpenCV into Anaconda, although unavailable through conda, is pretty trivial. You just need to download one file.

在任何情况下,将 OpenCV 安装到 Anaconda 中,虽然无法通过 conda 安装,但非常简单。您只需要下载一个文件。

  1. Download OpenCV from http://opencv.org/downloads.htmland extract
  2. From the extracted folder, copy the file from the extracted directory: opencv/build/python/2.7/(either x86 or x64, depending on your Anaconda version)/cv2.pyd to your Anaconda site-packages directory, e.g., C:\Anaconda\Lib\site-packages
  3. To get FFmpeg within opencvto work, you'll have to add the directory that FFmpeg is located in to the path (e.g., opencv/sources/3rdparty/ffmpeg). Then you'll have to find the DLL file in that folder (e.g., opencv_ffmpeg_64.dll) and copy or rename itto a filename that includes the opencvversion you are installing, (e.g., opencv_ffmpeg249_64) for 2.4.9.
  1. http://opencv.org/downloads.html下载 OpenCV并解压
  2. 从解压的文件夹中,将解压目录中的文件复制:opencv/build/python/2.7/(x86 或 x64,取决于您的 Anaconda 版本)/cv2.pyd 到您的 Anaconda 站点包目录,例如,C:\ Anaconda\Lib\site-packages
  3. 要使 FFmpegopencv工作,您必须将 FFmpeg 所在的目录添加到路径中(例如,opencv/sources/3rdparty/ffmpeg)。然后,您必须在该文件夹中找到 DLL 文件(例如 opencv_ffmpeg_64.dll)并将其复制或重命名为包含opencv您正在安装的版本的文件名(例如 opencv_ffmpeg249_64),用于 2.4.9。

Now at the Python prompt you should be able to type "import cv2"...to verify that it works, type "print cv2.__version__", and it should print out the OpenCV version you downloaded.

现在在 Python 提示符下,您应该能够输入“import cv2”...以验证它是否有效,输入“print cv2.__version__”,它应该会打印出您下载的 OpenCV 版本。

回答by RussellStewart

conda install opencvcurrently works for me on UNIX/python2. This is worth trying first before consulting other solutions.

conda install opencv目前在 UNIX/python2 上对我有用。在咨询其他解决方案之前,这值得先尝试。

回答by jonnycowboy

You can install it using binstar:

您可以使用 binstar 安装它:

conda install -c menpo opencv

回答by mrbean

I faced the same problem, but I have solved it now. This is what I did:

我遇到了同样的问题,但我现在已经解决了。这就是我所做的:

First enter conda install -c https://conda.binstar.org/menpo opencvin the command prompt and then find the path Anaconda\pkgs\opencv-2.4.9.1-np19py27_0\Lib\site-packages. Now copy all the files present here into Anaconda\Lib\site-packages. Now you will be able to use OpenCV with Python.

首先conda install -c https://conda.binstar.org/menpo opencv在命令提示符中输入,然后找到路径Anaconda\pkgs\opencv-2.4.9.1-np19py27_0\Lib\site-packages。现在将此处存在的所有文件复制到Anaconda\Lib\site-packages. 现在您将能够在 Python 中使用 OpenCV。

回答by OnTheContrary

I had exactly the same problem, and could not get conda to install OpenCV. However, I managed to install it with the OpenCV installer you find at this site:

我遇到了完全相同的问题,无法让 conda 安装 OpenCV。但是,我设法使用您在此站点上找到的 OpenCV 安装程序安装它:

http://www.lfd.uci.edu/~gohlke/pythonlibs/

http://www.lfd.uci.edu/~gohlke/pythonlibs/

His files are "Wheel" whl files that can be installed with pip, e.g.

他的文件是可以用pip安装的“Wheel”whl文件,例如

pip install SomePackage-1.0-py2.py3-none-any.whl

pip install SomePackage-1.0-py2.py3-none-any.whl

in a command window. It worked with Spyder directly after executing this command for me. I have had the same experience with other packages, the above UC Irvine site is a gold mine.

在命令窗口中。在为我执行此命令后,它直接与 Spyder 一起工作。我用其他包也有过同样的经历,上面的UC Irvine站点是金矿。

回答by Atlas7

I have summarized my now fully working solution, OpenCV-Python - How to install OpenCV-Python package to Anaconda (Windows). Nevertheless I've copied and pasted the important bits to this post.

我总结了我现在完全可用的解决方案OpenCV-Python - How to install OpenCV-Python package to Anaconda (Windows)。尽管如此,我已经将重要的部分复制并粘贴到了这篇文章中。



At the time of writing I was using Windows 8.1, 64-bit machine, Anaconda/ Python 2.x. (see notes below - this works also for Windows 10, and likely Python 3.x too).

在撰写本文时,我使用的是 Windows 8.1、64 位机器、Anaconda/Python 2.x。(请参阅下面的注释 - 这也适用于 Windows 10,也可能适用于 Python 3.x)。

  • NOTE 1: as mentioned mentioned by @great_raisin(thank you) in comment section however, this solution appears to also work for Windows 10.

  • NOTE 2: this will probably work for Anaconda/Python 3.x too. If you are using Windows 10 and Anaconda/Python 3.x, and this solution works, please add a comment below. Thanks! (Update: noting from comment "Working on Windows 10")

  • NOTE 3: depending on whether you are using Python 2.x or 3.x, just adjust the printstatement accordingly in code snippets. i.e. in Python 3.x it would be print("hello"), and in Python 2.x it would be print "hello".

  • 注意 1:正如@great_raisin(谢谢)在评论部分提到的,这个解决方案似乎也适用于 Windows 10。

  • 注意 2:这可能也适用于 Anaconda/Python 3.x。如果您使用的是 Windows 10 和 Anaconda/Python 3.x,并且此解决方案有效,请在下方添加评论。谢谢!(更新:从评论“在 Windows 10 上工作”中注意到)

  • 注意 3:根据您使用的是 Python 2.x 还是 3.x,只需print在代码片段中相应地调整语句即可。即在 Python 3.x 中它会是print("hello"),而在 Python 2.x 中它会是print "hello"

TL;DR

TL; 博士

To use OpenCV fully with Anaconda (and Spyder IDE), we need to:

要在 Anaconda(和 Spyder IDE)中完全使用 OpenCV,我们需要:

  1. Download the OpenCV package from the official OpenCV site
  2. Copy and paste the cv2.pydto the Anaconda site-packages directory.
  3. Set user environmental variables so that Anaconda knows where to find the FFMPEG utility.
  4. Do some testing to confirm OpenCV and FFMPEG are now working.
  1. 从 OpenCV官方站点下载 OpenCV 包
  2. 将 复制并粘贴cv2.pyd到 Anaconda 站点包目录。
  3. 设置用户环境变量,以便 Anaconda 知道在哪里可以找到 FFMPEG 实用程序。
  4. 做一些测试以确认 OpenCV 和 FFMPEG 现在正在工作。

(Read on for the detail instructions...)

(请继续阅读详细说明...)

Prerequisite

先决条件

Install Anaconda

安装蟒蛇

Anaconda is essentially a nicely packaged Python IDE that is shipped with tons of useful packages, such as NumPy, Pandas, IPython Notebook, etc. It seems to be recommended everywhere in the scientific community. Check out Anacondato get it installed.

Anaconda 本质上是一个包装精美的 Python IDE,附带了大量有用的软件包,例如 NumPy、Pandas、IPython Notebook 等。它似乎在科学界的任何地方都被推荐。查看Anaconda以安装它。

Install OpenCV-Python to Anaconda

将 OpenCV-Python 安装到 Anaconda

Cautious Note: I originally tried out installing the binstar.org OpenCV package, as suggested. That method however does not include the FFMPEG codec - i.e. you may be able to use OpenCV, but you won't be able to process videos.

注意事项:我最初尝试按照建议安装 binstar.org OpenCV 包。然而,该方法不包括 FFMPEG 编解码器 - 即您可以使用 OpenCV,但您将无法处理视频。

The following instruction works for me is inspired by this OpenCV YouTube video. So far I have got it working on both my desktop and laptop, both 64-bit machines and Windows 8.1.

以下说明对我有用,其灵感来自此OpenCV YouTube 视频。到目前为止,我已经让它在我的台式机和笔记本电脑上运行,64 位机器和 Windows 8.1。

Download OpenCV Package

下载 OpenCV 包

Firstly, go to the official OpenCV siteto download the complete OpenCV package. Pick a version you like (2.x or 3.x). I am on Python 2.x and OpenCV 3.x - mainly because this is how the OpenCV-Python Tutorialsare setup/based on.

首先,到OpenCV 官方网站下载完整的 OpenCV 包。选择您喜欢的版本(2.x 或 3.x)。我使用的是 Python 2.x 和 OpenCV 3.x - 主要是因为OpenCV-Python 教程是如何设置/基于的。

In my case, I've extracted the package (essentially a folder) straight to my C drive (C:\opencv).

就我而言,我已将包(基本上是一个文件夹)直接解压缩到我的 C 驱动器 ( C:\opencv)。

Copy and Paste the cv2.pyd file

复制并粘贴 cv2.pyd 文件

The Anaconda Site-packages directory (e.g. C:\Users\Johnny\Anaconda\Lib\site-packagesin my case) contains the Python packages that you may import. Our goal is to copy and paste the cv2.pydfile to this directory (so that we can use the import cv2in our Python codes.).

Anaconda Site-packages 目录(例如C:\Users\Johnny\Anaconda\Lib\site-packages在我的例子中)包含您可以导入的 Python 包。我们的目标是将cv2.pyd文件复制并粘贴到此目录中(以便我们可以import cv2在 Python 代码中使用。)。

To do this, copy the cv2.pydfile...

为此,请复制cv2.pyd文件...

From this OpenCV directory (the beginning part might be slightly different on your machine). For Python 3.x, I guess, just change the 2.xto 3.xaccordingly.

从这个 OpenCV 目录(开始部分在你的机器上可能略有不同)。对于 Python 3.x,我想,只需相应地更改2.x3.x

# Python 2.7 and 32-bit machine:
C:\opencv\build\python.7\x84

# Python 2.7 and 64-bit machine:
C:\opencv\build\python.7\x64

To this Anaconda directory (the beginning part might be slightly different on your machine):

到这个 Anaconda 目录(开始部分在你的机器上可能略有不同):

C:\Users\Johnny\Anaconda\Lib\site-packages

After performing this step we shall now be able to use import cv2in Python code. BUT, we still need to do a little bit more work to get FFMPEG (video codec) to work (to enable us to do things like processing videos).

执行完这一步后,我们现在就可以import cv2在 Python 代码中使用了。但是,我们仍然需要做更多的工作才能使 FFMPEG(视频编解码器)工作(使我们能够处理视频之类的事情)。

Set Environmental Variables

设置环境变量

Right-click on "My Computer" (or "This PC" on Windows 8.1) → left-click Properties→ left-click "Advanced" tab → left-click "Environment Variables..." button.

右键单击“我的电脑”(或 Windows 8.1 上的“此电脑”)→ 左键单击属性→ 左键单击“高级”选项卡 → 左键单击“环境变量...”按钮。

Add a new User Variable to point to the OpenCV (either x86 for 32-bit system or x64 for 64-bit system). I am currently on a 64-bit machine.

添加一个新的用户变量以指向 OpenCV(32 位系统的 x86 或 64 位系统的 x64)。我目前在 64 位机器上。

| 32-bit or 64 bit machine? | Variable     | Value                                |
|---------------------------|--------------|--------------------------------------|
| 32-bit                    | `OPENCV_DIR` | `C:\opencv\build\x86\vc12`           |
| 64-bit                    | `OPENCV_DIR` | `C:\opencv\build\x64\vc12`           |

Append %OPENCV_DIR%\binto the User Variable PATH.

附加%OPENCV_DIR%\bin到用户变量PATH

For example, my PATHuser variable looks like this...

例如,我的PATH用户变量看起来像这样......

Before:

前:

C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts

After:

后:

C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts;%OPENCV_DIR%\bin

This is it we are done! FFMPEG is ready to be used!

这就是我们完成了!FFMPEG 已准备好使用!

Test to confirm

测试确认

We need to test whether we can now do these in Anaconda (via Spyder IDE):

我们需要测试我们现在是否可以在 Anaconda 中执行这些操作(通过 Spyder IDE):

  • Import OpenCV package
  • Use the FFMPEG utility (to read/write/process videos)
  • 导入 OpenCV 包
  • 使用 FFMPEG 实用程序(读取/写入/处理视频)

Test 1: Can we import OpenCV?

测试1:我们可以导入OpenCV吗?

To confirm that Anaconda is now able to import the OpenCV-Python package (namely, cv2), issue these in the IPython console:

要确认 Anaconda 现在能够导入 OpenCV-Python 包(即cv2),请在 IPython 控制台中发出以下命令:

import cv2
print cv2.__version__

If the package cv2is imported OK with no errors, and the cv2version is printed out, then we are all good! Here is a snapshot:

如果包cv2导入OK,没有错误,并且cv2打印出版本,那我们就都好了!这是一个快照:

import-cv2-ok-in-anaconda-python-2.png
(source: mathalope.co.uk)

import-cv2-ok-in-anaconda-python-2.png
(来源:mathalope.co.uk

Test 2: Can we Use the FFMPEG codec?

测试 2:我们可以使用 FFMPEG 编解码器吗?

Place a sample input_video.mp4video file in a directory. We want to test whether we can:

将示例input_video.mp4视频文件放在目录中。我们想测试我们是否可以:

  • read this .mp4video file, and
  • write out a new video file (can be .avior .mp4etc.)
  • 阅读此.mp4视频文件,以及
  • 写出一个新的视频文件(可以是.avi.mp4等)

To do this we need to have a test Python code, call it test.py. Place it in the same directory as the sample input_video.mp4file.

为此,我们需要有一个测试 Python 代码,将其命名为test.py. 将它放在与示例input_video.mp4文件相同的目录中。

This is what test.pymay look like (I've listed out both newer and older version codes here - do let us know which one works / not work for you!).

test.py可能看起来像(我在这里列出了新版本和旧版本代码 - 请让我们知道哪一个对您有效/无效!)。

(Newer version...)

(新版本...)

import cv2
cap = cv2.VideoCapture("input_video.mp4")
print cap.isOpened()   # True = read video successfully. False - fail to read video.

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("output_video.avi", fourcc, 20.0, (640, 360))
print out.isOpened()  # True = write out video successfully. False - fail to write out video.

cap.release()
out.release()

(Or the older version...)

(或旧版本...)

import cv2
cv2.VideoCapture("input_video.mp4")
print cv2.isOpened()   # True = read video successfully. False - fail to read video.

fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter("output_video.avi",fourcc, 20.0, (640,360))
print out.isOpened()  # True = write out video successfully. False - fail to write out video.

cap.release()
out.release()

This test is VERY IMPORTANT. If you'd like to process video files, you'd need to ensure that Anaconda / Spyder IDE can use the FFMPEG (video codec). It took me days to have got it working. But I hope it would take you much less time! :)

这个测试非常重要。如果您想处理视频文件,您需要确保 Anaconda / Spyder IDE 可以使用 FFMPEG(视频编解码器)。我花了几天时间才让它工作。但我希望它会花费你更少的时间!:)

Note: One more very important tip when using the Anaconda Spyder IDE. Make sure you check the current working directory (CWD)!!!

注意:使用 Anaconda Spyder IDE 时还有一个非常重要的提示。确保检查当前工作目录(CWD)!!!

Conclusion

结论

To use OpenCV fully with Anaconda (and Spyder IDE), we need to:

要在 Anaconda(和 Spyder IDE)中完全使用 OpenCV,我们需要:

  1. Download the OpenCV package from the official OpenCV site
  2. Copy and paste the cv2.pydto the Anaconda site-packages directory.
  3. Set user environmental variables so that Anaconda knows where to find the FFMPEG utility.
  4. Do some testing to confirm OpenCV and FFMPEG are now working.
  1. 从 OpenCV官方站点下载 OpenCV 包
  2. 将 复制并粘贴cv2.pyd到 Anaconda 站点包目录。
  3. 设置用户环境变量,以便 Anaconda 知道在哪里可以找到 FFMPEG 实用程序。
  4. 做一些测试以确认 OpenCV 和 FFMPEG 现在正在工作。

Good luck!

祝你好运!

回答by Joben R. Ilagan

This worked for me (on Ubuntu and conda 3.18.3):

这对我有用(在 Ubuntu 和 conda 3.18.3 上):

conda install --channel https://conda.anaconda.org/menpo opencv3

The command above was what was shown to me when I ran the following:

上面的命令是我运行以下命令时显示给我的:

anaconda show menpo/opencv3

This was the output:

这是输出:

To install this package with conda run:
     conda install --channel https://conda.anaconda.org/menpo opencv3

I tested the following in python without errors:

我在 python 中测试了以下内容,没有错误:

>>> import cv2
>>>

回答by Dan Nguyen

Like others, I had issues with Python 3.5.1/Anaconda 2.4.0 on OS X 10.11..

和其他人一样,我在 OS X 10.11 上遇到了 Python 3.5.1/Anaconda 2.4.0 的问题。

But I found a compatible package here:

但我在这里找到了一个兼容的包:

https://anaconda.org/menpo/opencv3

https://anaconda.org/menpo/opencv3

It can be installed via the command line like so:

它可以通过命令行安装,如下所示:

 conda install -c https://conda.anaconda.org/menpo opencv3

Worked like a charm. First time I've ever gotten OpenCV to work on 3.x!

像魅力一样工作。我第一次让 OpenCV 在 3.x 上工作!

回答by Taiwo O. Adetiloye

To install OpenCV in Anaconda, start up the Anaconda command prompt and install OpenCV with

要在 Anaconda 中安装 OpenCV,请启动 Anaconda 命令提示符并使用以下命令安装 OpenCV

conda install -c https://conda.anaconda.org/menpo opencv3

Test that it works in your Anaconda Spyder or IPython console with

测试它是否在您的 Anaconda Spyder 或 IPython 控制台中工作

import cv2

You can also check the installed version using:

您还可以使用以下方法检查已安装的版本:

cv2.__version__

回答by liuyuyuil

If conda install opencvor conda install -c https://conda.binstar.org/menpo opencvdoes not work, you can try to compile from the source.

如果conda install opencv还是conda install -c https://conda.binstar.org/menpo opencv不行,可以尝试从源码编译。

Download the source from http://opencv.org/downloads.html, follow the install instruction in http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html, (maybe you can jump to the last part directly, 'Building OpenCV from Source Using CMake...), change the cmakecommand as following:

http://opencv.org/downloads.html下载源码,按照http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html 中的安装说明,(也许你可以跳转直接到最后一部分,'使用 CMake 从源代码构建 OpenCV...),将cmake命令更改如下:

mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/**/env/opencv-2.4.10 -D BUILD_NEW_PYTHON_SUPPORT=ON -D PYTHON_EXECUTABLE=/home/**/env/anaconda/bin/python -D PYTHON_INCLUDE_DIR=/home/**/env/anaconda/include/python2.7 -D PYTHON_LIBRARY=/home/**/env/anaconda/lib/libpython2.7.so -D PYTHON_PACKAGES_PATH=/home/**/env/anaconda/lib/python2.7/site-packages -D PYTHON_NUMPY_INCLUDE_DIRS=/home/**/env/anaconda/lib/python2.7/site-packages/numpy/core/include ..

make -j4
make install

You will find cv2.so in anaconda/lib/python2.7/site-packages.

你会在anaconda/lib/python2.7/site-packages.

Then:

然后:

import cv2
print cv2.__version__

It will print out 2.4.10.

它会打印出来2.4.10

My environment is GCC 4.4.6, Python 2.7 (anaconda), and opencv-2.4.10.

我的环境是 GCC 4.4.6、Python 2.7 (anaconda) 和 opencv-2.4.10。