Skip to content

Instantly share code, notes, and snippets.

@innat
Last active November 24, 2018 15:53
Show Gist options
  • Save innat/4af8e9a3accfdcf2511ee4ced26a1d77 to your computer and use it in GitHub Desktop.
Save innat/4af8e9a3accfdcf2511ee4ced26a1d77 to your computer and use it in GitHub Desktop.
Installing OpenCV3 for Python3x

Supported Python versions

Python 2.7 is the only supported version in 2.x series. Python 3.x releases follow Numpy releases. For example Python 3.3 is no longer supported by Numpy so support for it has been dropped in opencv-python, too.

Currently, builds for following Python versions are provided:

  • 2.7
  • 3.4
  • 3.5
  • 3.6

If you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. cv2 module in the root of Python's site-packages), remove it before installation to avoid conflicts.

We'll cover two approach to have OpenCV 3 for Python 3.

- pip command 
- conda command 

Approach One -

Note : Open up your command prompt and type following - pip search opencv to ensure whether you python version are supported or not. Higly recommend.

Many binaries depend on the Microsoft Visual C++ 2010 (x64, x86, for CPython 3.4), or the Visual C++ 2017 (x64 or x86 for CPython 3.5, 3.6, and 3.7) redistributable packages.

Installation Process

  1. Download the Numpy version corresponding to your Python installation from here. i.e. : In my case, I’ve used numpy-1.13.1+mkl-cp36-cp36m-win_amd64.whl. Currently i'm using Python 3.6. (showed cp36..)

  2. Download the OpenCV version corresponding to your Python installation from here i.e. : In my case, I've used opencv_python-3.3.0-cp36-cp36m-win_amd64.whl. Notice, both files are for Python 3.6. (showed cp36..)

Now go to the folder where you downloaded these files and run the following :

pip install numpy-1.13.1+mkl-cp36-cp36m-win_amd64.whl
pip install opencv_python-3.3.0-cp36-cp36m-win_amd64.whl

Is everything goes well , congrats then , OpenCV 3 for Python 3 has successfully installed. At this point, you should be able to play with OpenCV and Python. Let's try some code, open up you terminal or python IDE and type following peice of code to verify installation.

import cv2
print(__version__)

If everything was correctly installed, you should see the version number of your OpenCV install, In my case this was 3.3.0.


Approach Two -

If you're using Anaconda Distribution , process are pretty much easy than before. But you may need to isntall Microsoft Visual C++ redistributable package accoriding to your python package. One thing should keep in mind that , the supported package of numpy an opencv for specific version of python is important to match. Like in my case , previously we choose numpy --- cp36 and opencv --- cp36 ; both are for Python 3.6.

Optional

Before proceed to install opencv, you can check whether opencv for python 3.x is available or not. We can check it by typing conda info opencv in command prompt and press enter of course, you'll see following -

opencv 3.3.1 py36h20b85fd_1
---------------------------
file name   : opencv-3.3.1-py36h20b85fd_1.tar.bz2
name        : opencv
version     : 3.3.1
build string: py36h20b85fd_1
build number: 1
channel     : https://repo.anaconda.com/pkgs/main/win-64
size        : 96.7 MB
arch        : None
constrains  : ()
license     : BSD 3-clause
license_family: BSD
md5         : e65c68524073445511ace8ade7ae3641
platform    : None
subdir      : win-64
timestamp   : 1512689066576
url         : https://repo.anaconda.com/pkgs/main/win-64/opencv-3.3.1-py36h20b85fd_1.tar.bz2
dependencies:
    jpeg >=9b,<10a
    libpng >=1.6.32,<1.7.0a0
    libtiff >=4.0.9,<5.0a0
    numpy >=1.11.3,<2.0a0
    python >=3.6,<3.7.0a0
    vc 14.*
    zlib >=1.2.11,<1.3.0a0

By this we can also get ensure that opencv 3.3.1 py36h20b85fd_1 is available. And this is available for python 3.6 currently.

Installation Process

Open up you command pormpt or Anaconda command prompt and type following commnad, it may take some time -

conda install -c conda-forge opencv

Is everything goes well , congrats then , OpenCV 3 for Python 3 has successfully installed. At this point, you should be able to play with OpenCV and Python.

Also we can do this by giving following command in anaconda command prompt -

pip install opencv-python

Once again, is everything goes well , congrats then , OpenCV 3 for Python 3 has successfully installed. At this point, you should be able to play with OpenCV and Python. Test it by wiht following code -

import cv2
print(__version__)

Same approach if you want to install opencv-contrib-python

However, we can also have OpenCV by another approach. Let's assuem you've installed Anaconda on your system. So , first Download OpenCV package from the official site, extract it and there you can find a file named cv2.pyd located in C:\OpenCV\build\python\2.7\x64 directoy. Just copy that file and paste it to Anacoand package's location, in my case - C:\Users\iPhoton\Anaconda3\Lib\site-packages\. It should work too.

Frequently Asked Question

Q: Do I need to install also OpenCV separately?

A: No, the packages are special wheel binary packages and they already contain statically built OpenCV binaries.

Q: Pip fails with Could not find a version that satisfies the requirement ...?

A: Most likely the issue is related to too old pip and can be fixed by running pip install --upgrade pip. Note that PyPI does not currently support ARM architecture so you can't install these packages for example on Raspberry Pi.

Q: Import fails on Windows: ImportError: DLL load failed: The specified module could not be found.?

A: If the import fails on Windows, make sure you have Visual C++ redistributable 201x(menstion above | choose according to your python version) installed.If you are using older Windows version than Windows 10 and latest system updates are not installed, Universal C Runtime might be also required.

If the above does not help, check if you are using Anaconda. Old Anaconda versions have a bug which causes the error, see this issue for a manual fix.

Q: I have some other import errors?

A: Make sure you have removed old manual installations of OpenCV Python bindings (cv2.so or cv2.pyd in site-packages).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment