Wednesday, February 8, 2017

Installing OpenCV 3 on Anaconda Python 3.5 on Windows

If you checked my last post, you know that TensorFlow now officially works on Windows. This was really exciting to me, since I was using a Linux machine for my TensorFlow experiments, I had to switch between the Linux and Windows for my other tasks. Now, I should be able to perform some of those experiments on Windows as well. (I would still switch to Linux for more processor intensive deep learning models). So, I decided to set everything up on Windows again.

I've been using OpenCV 3 on Anaconda Python 3.5 on Linux. (Check out my guide on Installing OpenCV from source on Anaconda Python on Ubuntu 16.10 if you are trying to install on Linux). But on Windows, I've previously only tried OpenCV on Python 2.7. Since the OpenCV Downloads page has pre-built binaries for OpenCV 3 (version 3.2 being latest), I thought the installation would be straightforward.

I downloaded and ran the OpenCV 3.2 installer (which just extracts OpenCV to a directory of your choosing), set the OPENCV_DIR environment variable, add the bin dir in OpenCV to the PATH, and finally copy the cv2.pyd file to the site-packages directory in my Anaconda environment. I fired up the Anaconda environment, and tried loading cv2 in Python. But I got the following error,

 (tensorflow) C:\>python  
 Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 5 2016, 11:41:13) [MSC v  
 .1900 64 bit (AMD64)] on win32  
 Type "help", "copyright", "credits" or "license" for more information.  
 >>> import cv2  
 Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
 ImportError: DLL load failed: The specified module could not be found.  


I did a little searching online, and few has suggested to install the Microsoft Visual C++ Redistributable 2015 to solve the issue. I tried installing it, but still got the same error. It turns out that the cv2.pyd library which comes with the official pre-built binary for Windows is only meant to work with Python 2.7. I needed one that works with Python 3.5.

Rather than trying to compile OpenCV for Python 3.5 on Windows (I'm still attempting this), I looked to see if there's an Anaconda package solution.

There were 2 packages available on Anaconda cloud - OpenCV 3.2 by Menpo,  and OpenCV 3.1 by Conda-forge. Both had Windows packages. I decided to try the Menpo package first, since it had the latest version (3.2), but I got an install error when trying to install the 64-Bit Windows package. Then I tried the Conda-forge package, (even though it was only OpenCV 3.1).

Update: Conda-forge now has the OpenCV 3.2 package for all platforms. (Check the story here: OpenCV 3.2 and Dlib 19.4 Packages Now Available from Conda-Forge)
You can install the package like this,

 conda install -c conda-forge opencv=3.2.0  

The installation ran fine, and it didn't try to upgrade any of my installed packages. I ran the Python interpreter, and imported the cv2 module - no errors there as well. Finally, I ran a face detection example on OpenCV, which also ran perfectly.

OpenCV 3 running on Python 3.5 64-Bit on Windows
OpenCV 3 running on Python 3.5 64-Bit on Windows
Next step, try out TensorFlow with OpenCV 3 on Windows.

Summary
  • The cv2.pyd module which comes with the official OpenCV 3 Windows binary distribution is only meant for Python 2.7.
  • For Python 3.5 on Windows, right now the best option (that I've found so far) is to use the Anaconda package from Conda-forge.

Note that you can use the same steps to get OpenCV 3.2 working on any platform (Windows, Linux, Mac OS) with Anaconda.


Related posts:
Related links:

Build Deeper: Deep Learning Beginners' Guide is the ultimate guide for anyone taking their first step into Deep Learning.

Get your copy now!

8 comments:

  1. thank you. almost facepalmed myself into a coma if I hadn't read this post

    ReplyDelete
    Replies
    1. I've been there :D
      Hit my head multiple time on the monitor before getting OpenCV to work.

      Delete
  2. Just checked the conda-forge link and they have updated to v 3.2. FYI. Thanks for the post!

    ReplyDelete
  3. One more, could you include the link to the OpenCV tutorial you completed successfully? Thanks!

    ReplyDelete
  4. The installation runs perfectly, but when I import it I get an error saying DLL load failed:The specified module could not be found

    ReplyDelete
  5. I am running 2.7 python but it not working for me

    ReplyDelete
  6. Thanks!! Your post solved all my issues

    ReplyDelete