Installing OpenCV on Windows with Anaconda

I installed the OpenCV library on my Windows machine. The process was a minor nightmare. But open source installation often entails much pain so I wasn’t very surprised.

OpenCV (“open source computer vision”) can be used to do all kinds of sophisticated processing on images and video.

My configuration was: a Windows 10 machine, with the Anaconda 4.1.1 distribution of Python (Python 3.5.2, NumPy 1.11.1, and 400+ other packages). An Internet search suggested many different ways to install OpenCV for Python — and all except one technique failed for me. A typical failed process (for me) was to try to install like this:

> pip install opencv-python

The installation apparently succeeded, and apparently correctly created a folder named cv2 in my machine’s site-packages folder. But when I tried to import the package:

> python
>>> import cv2

the import failed.

Notice the mildly annoying naming inconsistency — opencv-python for the package and cv2 for the module. Anyway, fortunately I was able to uninstall the failed installation:

> pip uninstall opencv-python

And I was back to the start. In the end, after many other failed attempts, the one, single installation process that worked was to go to an unofficial Web site, get a whl (“wheel”) file and install it. But, I had to upgrade my NumPy library.

First I went to https://www.lfd.uci.edu/~gohlke/pythonlibs/ and found the whl file for Python 35 — opencv_python-3.4.1-cp35-cp35m-win_amd64.whl — and I downloaded into a C:\Python\_Local directory. The installation command was

> pip install C:\Python\_Local\opencv_python-3.4.1-
    cp35-cp35m-win_amd64.whl

This succeeded but placed raw files into my site-packages folder rather than creating a nice cv2 folder. However, my initial attempt to import cv2 failed with a NumPy version error. So, I eventually figured out I had to upgrade NumPy to 1.14.2 with the command

> pip install -U numpy

and at last, I was able to import and use OpenCV for Python:

> python
>>> import cv2
(and I was able to use cv2 -- another post)

When working with machine learning libraries, installation is often a huge barrier to entry.

This entry was posted in Machine Learning. Bookmark the permalink.