Recently, Satya Mallick, founder of learnopencv.com, posted an impressive (but complicated) method for installing OpenCV 3 on Windows that supports both the C++ and Python API’s. Since a lot of users will be interested in solely Python OpenCV, I figured it would be helpful to post a relatively quick method for getting Python OpenCV 3 up and running on Windows.
1) Install Anaconda
Head over to continuum.io/downloads/ and install the latest version of Anaconda. Make sure to install the “Python 3.6 Version” for the appropriate architecture. Install it with the default settings.
2) Open the Anaconda Prompt
Anaconda installs a few programs on your computer when you run the installer. These include the Anaconda Navigator, Anaconda Cloud, Spyder, and the Anaconda Prompt. Search in your Windows taskbar for the Anaconda Prompt. This is a modified version of the Windows Command Prompt that support specific Anaconda commands. All of the code we discuss in these instructions will be run directly in the Anaconda Prompt.
To test the Anaconda Prompt, run:
3) Create an Anaconda Environment
This section is essentially a Windows distillation of https://conda.io/docs/using/envs.html#create-an-environment.
Anaconda environments are similar to a Python virtualenv, except they use Anaconda’s superb package managers. When we install OpenCV 3, we will do so in an Anaconda environment that uses specifically Python 3.5, and that version of Python will only be accessible through the environment. Below are some basics of Anaconda environment management.
### Basics of Anaconda environment management ### # Creating an environment conda create --name myNewEnv python=x.x.x # Activating an environment activate myNewEnv # Deactivating an environment deactivate myNewEnv # Listing environments conda info --envs # Removing an environment conda remove --name myNewEnv --all
Run the following to create and activate a new Anaconda environment for Python 3.5. We use Python 3.5 for compatibility with the OpenCV 3 distribution we will be using.
conda create --name myWindowsCV python=3.5 activate myWindowsCV
Now, you should see “(myWindowsCV)” prepended to the command line. When the environment’s name is prepended to the command line, the environment is active, and Anaconda has modified the $PATH variables of the console to point to various directories in anaconda3/envs/myWindowsCV. Running “python –version” should now return Python 3.5.* as opposed to your system Python version.
(myWindowsCV) C:\Users\Chris>python --version Python 3.5.2 :: Continuum Analytics, Inc.
4) Install OpenCV 3 and Dependencies
In the Anaconda Prompt, with your Anaconda environment active, run:
conda install numpy conda install anaconda-client conda install --channel https://conda.anaconda.org/menpo opencv3
Now, enter Python and check that OpenCV 3 is installed.
(myWindowsCV) C:\Users\Chris>python >>>import cv2 >>>cv2.__version__ '3.1.0'
5) Start Using Python and OpenCV3
Always make sure your environment is active, or you will not be accessing the correct Python version. Managing virtual environments can be cumbersome for users that prefer heavyweight IDE’s like Eclipse or PyCharm, but these issues are well-documented by Anaconda: IDE Integration.
This setup is ideal for users that:
- Need to ensure cross-OS compatibility (Anaconda can be installed on MacOS and Linux)
- Are interested in pure Python implementations of OpenCV
- Are interested in highly stable releases of OpenCV 3 and Python 3
- Prefer light text editors for programming
- Have experience configuring heavier IDE’s
This setup was tested on Windows 10 and Windows 8.1 computers. Comment below with any issues and we will see if we can’t work through them.