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 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
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
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 opencv
in the command prompt and then find the pathAnaconda\pkgs\opencv-2.4.9.1-np19py27_0\Lib\site-packages
. Now copy all the files present here intoAnaconda\Lib\site-packages
. Now you will be able to use OpenCV with Python.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.
Currently, I am using Windows 8.1 and 64-bit machine, Anaconda as IDE for Python 2.x.
Note: if you are on Windows 10 (or above) and if the below instruction works, please could you kindly add a comment? This will help out the community a lot! :-)
TL;DR
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...)
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:\Users\Johnny\Anaconda\Lib\site-packages
in my case) contains the Python packages that you may import. Our goal is to copy and paste thecv2.pyd
file to this directory (so that we can use theimport 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):
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 VariablePATH
.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):
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 thecv2
version is printed out, then we are all good! Here is a snapshot:import-cv2-ok-in-anaconda-python-2.png http://mathalope.co.uk/wp-content/uploads/2015/07/import-cv2-ok-in-anaconda-python-2.png
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:.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 sampleinput_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:
cv2.pyd
to the Anaconda site-packages directory.Good luck!
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 theopencv
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.
I installed it like this:
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.