Reputation: 19889
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?
Upvotes: 323
Views: 1052694
Reputation: 455
This one worked best for me: I changed the Conda solver (source):
conda install -n base conda-libmamba-solver
conda config --set solver libmamba
And then installed opencv using:
conda install -c conda-forge opencv
Installed in approx. 5-10 seconds.
Upvotes: 2
Reputation: 5367
As of 2024, you can use the following command to install opencv with Conda:
conda install -c conda-forge opencv
and then validate you install in Python script:
import cv2
print(cv2.__version__)
which will print version of your installed opencv, e.g.:
4.9.0
Upvotes: 3
Reputation: 8480
Conda has made this easy as of 4/2024. I'm running python 3.9.19 and Conda 4.10.1.
conda install opencv
Verify in Python:
conda import cv2
Upvotes: 2
Reputation: 324
In my case, I've python 3.9 I did this in my conda env and it works :
pip install opencv-python==3.4.15.55
pip install opencv-contrib-python==3.4.15.55
Upvotes: 10
Reputation: 114
To insatll opencv without downgrading just hit out this in terminal particularly the user of linux mint or Ubuntu based distro
conda install -c conda-forge opencv
Hence go with it I am pretty sure that this will not disappoint you.
Upvotes: 6
Reputation: 1270
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.
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.
Upvotes: 31
Reputation: 97
Try following these commands in this order:-
conda update anaconda-navigator
conda update navigator-updater
pip install opencv-python
Upvotes: 0
Reputation: 1848
One thing i think nobody mentioned is that if you are using Windows N or KN edition, please install Media Feature Pack that is used for OpenCV. Also make sure you have Visual C++ Distributable installed. And then you can use command
conda install -c menpo opencv
Upvotes: 2
Reputation: 5043
You can install OpenCV by running these commands in the Anaconda command prompt:
conda config --add channels conda-forge
conda install libopencv opencv py-opencv
Source:
https://github.com/conda-forge/opencv-feedstock
Upvotes: 34
Reputation: 6363
In May 2019, the answer is as follows for MacOS:
conda update freetype
conda install opencv -c conda-forge
This updates libfreetype.6.dylib
to 24.0.0 compatibility and installs OpenCV 4.1.0.
Upvotes: 25
Reputation: 109
it works on anaconda3 windows 10 I have already downloaded it at 5 December 2019.
Firstly, using this command:
pip install opencv-contrib-python
after that windows will ask for permission and try again:
pip install opencv-contrib-python --user
look at this it works!!
Upvotes: 9
Reputation: 167
I tried following command and it works fine
conda install -c conda-forge opencv
once you hit the command it will ask for yes or no
If you select yes it will proceed and install all the required packages
Upvotes: 3
Reputation: 2856
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"
.
To use OpenCV fully with Anaconda (and Spyder IDE), we need to:
cv2.pyd
to the Anaconda site-packages directory.(Read on for the detail instructions...)
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.
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.
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
).
The Anaconda Site-packages directory (e.g. C:\Users\Johnny\Anaconda\Lib\site-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:\opencv\build\python\2.7\x84
# Python 2.7 and 64-bit machine:
C:\opencv\build\python\2.7\x64
To this Anaconda directory (the beginning part might be slightly different on your machine):
C:\Users\Johnny\Anaconda\Lib\site-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).
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:\opencv\build\x86\vc12` |
| 64-bit | `OPENCV_DIR` | `C:\opencv\build\x64\vc12` |
Append %OPENCV_DIR%\bin
to the User Variable PATH
.
For example, my PATH
user variable looks like this...
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!
We need to test whether we can now do these in Anaconda (via Spyder IDE):
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)
Place a sample input_video.mp4
video file in a directory. We want to test whether we can:
.mp4
video file, and.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)!!!
To use OpenCV fully with Anaconda (and Spyder IDE), we need to:
cv2.pyd
to the Anaconda site-packages directory.Good luck!
Upvotes: 85
Reputation: 3057
I just installed conda 4.7.11 in Windows 10. OpenCV can be easily installed in Anaconda Navigator. After launching Navigator, click on Environments on the left panel. In the top drop-down, select Not installed. Then search for py-opencv, tick on the left checkbox to install it. It will install the dependent package libopencv. I use import cv2
in Spyder to access it.
Upvotes: 5
Reputation: 35
Following command adds a different anaconda channel for opencv3
, you should be able to pull from it.
conda install --channel https://mirrors.ustc.edu.cn/anaconda/cloud/menpo opencv3
Upvotes: -2
Reputation: 436
The following installs opencv
from conda-forge
(note: tried on Windows)
conda config --add channels conda-forge
conda install opencv
Upvotes: 21
Reputation: 15753
i was on MAC machine inside one of anaconda virutal environment. For me,
conda install -c conda-forge opencv
worked fine.
It installed opencv version 3.4.4
Hope it helps.
Upvotes: -1
Reputation: 389
If you want to install opencv 3.4.0, there, unfortunately, is not this version inside conda. You need to use pip instead.
pip install opencv-python==3.4.0.12
Upvotes: 1
Reputation: 2739
Although not through Conda, and this is specific to Ubuntu, the easiest way to install OpenCV with all its contrib modules built in such as SIFT and SURF is to use:
pip install opencv-contrib-python
It supports Python2.7+ and Python 3.4+
If you choose not to have the contribs built in, you could instead do:
pip install opencv-python
You need to install the following dependencies below before you run those commands though:
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
This is a good tutorial on setting OpenCV on Ubuntu: https://medium.com/@debugvn/installing-opencv-3-3-0-on-ubuntu-16-04-lts-7db376f93961
Upvotes: 4
Reputation: 27
Windows 7, conda 4.4.10 python 2.7.6 Downloaded opencv package from Unofficial Windows Binaries for python extensions packages. (picked python 2.4 AMD 64) cd Download pip install opencv_python... python run_some.py (where import cv2 ...) seems to work. YMMV
Upvotes: -1
Reputation: 39
I had steps in repo that will help you properly install OpenCV.
If the links if broken, see this.
For requirements and prerequisites follow this instructions:
YOU NEED TO HAVE WINDOWS OS to begin this project.
windows 7 to 10
Follow the steps carefully
Install Anaconda2 32/64 python 2
or
Install Anaconda3 32/64 python 3
It Depends on System Information
Along the way you must check all the options ignore the red text
Then wait till installation completed ...
Open CMD admin when finish installed
Make sure that the anaconda-python is configured in CMD admin
Type: python
This will come out:
C:\WINDOWS\system32>python Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45)
[MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
Next is to install packages:
Numpy
OpenCV
PyMySQL or pymysql
Install and Import OpenCV
conda install -c conda-forge opencv = (version optional)
(click yes if there's updates)
Install and Import Numpy
pip install numpy
(click yes if there's updates)
Install and Import PyMySQL/pymysql
pip install PyMySQL or pymysql
When all the package is completely installed, then you must Reboot it.
go to CMD admin again
type "python"
import cv2 (cv2 is OpenCV)
import Numpy or numpy
import pymysql
import os
from PIL from Image
if all of then is ok.. your ready to run the code!!
After you finish all the steps, you can now test the code in SPYDER python that I use
Upvotes: 1
Reputation: 720
The correct command for installing the current version of OpenCV 3.3 in Anaconda windows:
conda install -c conda-forge opencv
or
conda install -c conda-forge/label/broken opencv
For 3.2 use this:
conda install -c menpo opencv3
Upvotes: 2
Reputation: 59
I installed it like this:
$ conda install --channel https://conda.anaconda.org/conda-forge opencv
I tried conda install opencv
directly, but it does not work for me since I am using Python 3.5 which is higher version that default OpenCV library in conda. Later, I tried 'anaconda/opencv', but it does not work either. I found finally that conda-forge/opencv works for Python 3.5.
Upvotes: -1
Reputation: 65
To install the OpenCV package with conda, run:
conda install -c menpo opencv3=3.1.0
https://anaconda.org/menpo/opencv3
Upvotes: 4
Reputation: 1711
I have just tried on two Win32 Python 3.5 computers. At first, I was able to conda install opencv
, but it didn't work nor did the version from menpp, but this did:
conda install -c https://conda.binstar.org/conda-forge opencv
Upvotes: 1
Reputation: 5095
Here's a general approach to using conda to install packages for Python that applies:
conda search packageName
e.g. conda search opencv
If this doesn't return results, conda install packageName
will not work.
At this point you can go to, https://anaconda.org/ and type the packageName into the search box. If this pulls up results (which it should for OpenCV), then click on one of the results that is for your platform (e.g. win-64). The next page will show you the command to use to install this package (e.g. conda install -c menpo opencv=2.4.11
).
If your package doesn't return results by search https://anaconda.org, then you can try pip install packageName
.
Caution: when I used step 3 to install OpenCV for win-64, I got an error when I tried to import cv2
.
Here is the error:
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
I think the error is due to package version conflicts. Nevertheless, this is a valid way to install OpenCV and other Python packages, you just might need to resolve some package version conflicts.
Upvotes: 0
Reputation: 182
I just wanted to update the brilliant answer by Atlas7.
If you're using OpenCV 3, change the test code to the following:
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.avi',fourcc, 20.0, (640,480))
print out.isOpened() # True = write out video successfully. False - fail to write out video.
cap.release()
out.release()
Upvotes: 0
Reputation: 361
The following command works for me too. I am using an embedded IPython Notebook in Anaconda.
conda install -c https://conda.binstar.org/menpo opencv
Upvotes: 1
Reputation: 423
Using Wheel files is an easier approach. If you cannot install Wheel files from the command prompt, you can use an executable pip file which exists in the <Anaconda path>/Scripts folder.
Upvotes: -4
Reputation: 17
You just copy the cv2.pyd file to the C:\Users\USERNAME\Anaconda2\Lib
directory.
You get the cv2.pyd file at this link (https://sourceforge.net/projects/opencvlibrary/files/).
The cv2.pyd is located at C:\Users\USERNAME\Desktop\opencv\build\python\2.7\x64
.
Upvotes: 0