如何通过 Conda 安装 Python OpenCV? [英] How do I install Python OpenCV through Conda?

查看:70
本文介绍了如何通过 Conda 安装 Python OpenCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过
(来源:mathalope.co.uk)

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

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

  • 阅读这个 .mp4 视频文件,然后
  • 写出一个新的视频文件(可以是 .avi.mp4 等)

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

这就是 test.py 的样子(我在这里列出了新旧版本的代码 - 请让我们知道哪一个对您有效/无效!).

(较新的版本...)

导入 cv2cap = cv2.VideoCapture("input_video.mp4")print cap.isOpened() # True = 成功读取视频.错误 - 无法读取视频.Fourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter("output_video.avi", Fourcc, 20.0, (640, 360))print out.isOpened() # True = 成功写出视频.错误 - 无法写出视频.cap.release()out.release()

(或旧版本...)

导入 cv2cv2.VideoCapture("input_video.mp4")print cv2.isOpened() # True = 成功读取视频.错误 - 无法读取视频.Fourcc = cv2.cv.CV_FOURCC(*'XVID')out = cv2.VideoWriter("output_video.avi",fourcc, 20.0, (640,360))print out.isOpened() # True = 成功写出视频.错误 - 无法写出视频.cap.release()out.release()

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

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

结论

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

  1. 官方 OpenCV 站点
  2. 下载 OpenCV 包
  3. cv2.pyd 复制并粘贴到 Anaconda 站点包目录.
  4. 设置用户环境变量,以便 Anaconda 知道在哪里可以找到 FFMPEG 实用程序.
  5. 进行一些测试以确认 OpenCV 和 FFMPEG 现在可以正常工作.

祝你好运!

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

I tried

conda install opencv
conda install cv2

I also tried searching

conda search cv

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

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

After running conda info I 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.

Am I missing something pretty obvious here? If opencv was 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?

解决方案

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.


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).

  • 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 print statement 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".

TL;DR

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

  1. Download the OpenCV package from the official OpenCV site
  2. Copy and paste the cv2.pyd to 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.

(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 Anaconda to get it installed.

Install OpenCV-Python to 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.

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.

Download OpenCV Package

Firstly, go to the official OpenCV site to 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 Tutorials are setup/based on.

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

Copy and Paste the cv2.pyd file

The Anaconda Site-packages directory (e.g. C:UsersJohnnyAnacondaLibsite-packages in my case) contains the Python packages that you may import. Our goal is to copy and paste the cv2.pyd file to this directory (so that we can use the import cv2 in our Python codes.).

To do this, copy the cv2.pyd file...

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

# Python 2.7 and 32-bit machine:
C:opencvuildpython2.7x84

# Python 2.7 and 64-bit machine:
C:opencvuildpython2.7x64

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

C:UsersJohnnyAnacondaLibsite-packages

After performing this step we shall now be able to use import cv2 in 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).

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.

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.

| 32-bit or 64 bit machine? | Variable     | Value                                |
|---------------------------|--------------|--------------------------------------|
| 32-bit                    | `OPENCV_DIR` | `C:opencvuildx86vc12`           |
| 64-bit                    | `OPENCV_DIR` | `C:opencvuildx64vc12`           |

Append %OPENCV_DIR%in to the User Variable PATH.

For example, my PATH user variable looks like this...

Before:

C:UsersJohnnyAnaconda;C:UsersJohnnyAnacondaScripts

After:

C:UsersJohnnyAnaconda;C:UsersJohnnyAnacondaScripts;%OPENCV_DIR%in

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

Test to confirm

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

  • Import OpenCV package
  • Use the FFMPEG utility (to read/write/process videos)

Test 1: Can we import OpenCV?

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

import cv2
print cv2.__version__

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


(source: mathalope.co.uk)

Test 2: Can we Use the FFMPEG codec?

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

  • read this .mp4 video file, and
  • write out a new video file (can be .avi or .mp4 etc.)

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.mp4 file.

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

(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! :)

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

Conclusion

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

  1. Download the OpenCV package from the official OpenCV site
  2. Copy and paste the cv2.pyd to 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.

Good luck!

这篇关于如何通过 Conda 安装 Python OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆