Conda Opencv

 
  1. Conda Opencv-contrib-python
  2. Conda Opencv Gpu

Question or problem about Python programming:

I have had countless problems with installing opencv with conda This is my approach, create an env if you don't already have one. Conda create -n py36 python=3.6 conda activate py36 Install opencv with pip NOT conda. Pip install opencv-python. If your still having an issue, uninstall opencv, update ffmpeg. Conda install -c conda-forge ffmpeg. 在终端打开conda中对应的虚拟环境,输入pip install opencv-python 这里需要提示下: 这里是 opencv-python 而不是 opencv ,代表的是python下的opencv。. Conda install pip. Now you can install any package in anaconda environment using pip. Suppose we want to install OpenCV, follow the command – pip install opencv-python. Now, at the end, to view all the installed packages, either use the Environments tab of Anaconda Navigator or type the following command in Anaconda prompt-conda list. Opencv 4.1.0 py36h79d2e431 conda-forge NOTE: If we want to install specific version of the package it can be specified like here: (my-env) $ conda install -c conda-forge opencv=4.1.0. Conda install pip. Now you can install any package in anaconda environment using pip. Suppose we want to install OpenCV, follow the command – pip install opencv-python. Now, at the end, to view all the installed packages, either use the Environments tab of Anaconda Navigator or type the following command in Anaconda prompt-conda list.

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

I tried

I also tried searching

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

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

Conda

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.

Access-restricted-item true Addeddate 2011-09-26 17:30:17 Boxid IA150901 Boxid2 BL11203T Camera Canon EOS 5D Mark II City New York Donor Edition 1st Back Bay pbk. Flyboys pdf free download for windows 7

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?

How to solve the problem:

Solution 1:

You can install it using binstar:

Solution 2:

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

Solution 3:

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

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

This was the output:

I tested the following in python without errors:

Solution 4:

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

Conda Opencv-contrib-python

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.

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

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.

Append %OPENCV_DIR%bin to the User Variable PATH.

For example, my PATH user variable looks like this…

Before:

After:

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)
Conda opencv python 3.8
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:

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

(Or the older version…)

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 Opencv Gpu

Solution 5:

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

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

  1. Download OpenCV from https://opencv.org/releases/ and 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:AnacondaLibsite-packages
  3. To get FFmpeg within opencv to 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 it to a filename that includes the opencv version you are installing, (e.g., opencv_ffmpeg249_64) for 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.

Hope this helps!