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.
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 |
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:
- Installing OpenCV from source on Anaconda Python on Ubuntu 16.10
- Setting up TensorFlow on Windows
- OpenCV 3.2 and Dlib 19.4 Packages Now Available from Conda-Forge
- http://opencv.org/downloads.html
- https://anaconda.org/conda-forge/opencv
- https://anaconda.org/menpo/opencv3
Build Deeper: Deep Learning Beginners' Guide is the ultimate guide for anyone taking their first step into Deep Learning.
Get your copy now!
thank you. almost facepalmed myself into a coma if I hadn't read this post
ReplyDeleteI've been there :D
DeleteHit my head multiple time on the monitor before getting OpenCV to work.
Just checked the conda-forge link and they have updated to v 3.2. FYI. Thanks for the post!
ReplyDeleteOne more, could you include the link to the OpenCV tutorial you completed successfully? Thanks!
ReplyDeleteThe installation runs perfectly, but when I import it I get an error saying DLL load failed:The specified module could not be found
ReplyDeleteThank you!!! You saved me!!!
ReplyDeleteI am running 2.7 python but it not working for me
ReplyDeleteThanks!! Your post solved all my issues
ReplyDelete