<-- Go Back

Setting up OpenCV with Python 3

Things change. Half a year ago, I was happy with running OpenCV on Python 2. But not anymore. As hard as it is for me to admit, it is time to switch to Python 3. So here is my supposedly easy to follow tutorial on setting up OpenCV on Python 3.

Prerequisites

First of all, get the latest package listings from the repository. If you are on Fedora, you can safely skip this. If you are on Debian, do the following:

$ sudo apt-get update

Let's make sure Python 3 and its dev-libs are installed. If on Debian, run

$ sudo apt-get install python3 python3-dev

If you are on fedora,

$ sudo dnf install python3 python3-devel

Now, let us install the python package manager, more commonly called pip.
On Debian, you would want to do this,

$ sudo apt-get install python3-pip

On Fedora, you can do

$ sudo dnf install python3-pip

Now we can proceed to install Virtual Env.

Setting up Virtualenv

Virtualenv is a container inside which you can set a Python environment complete with libraries that are independent of the rest of the system. Setting up Virtualenv is worth it, as it allows us to maintain multiple versions of libraries parallelly without conflicts.

On Fedora, do

$ sudo dnf install python3-virtualenv

If you are on Debian, do

$ sudo apt-get install python3-venv

Once installed, go to someplace where you want to keep the files for your project. Create a directory for the same and initialize a virtual env inside the directory. It can be done as follows:

$ mkdir opencv
$ cd opencv
$ python3   -m venv .

To make a virtual env work for us, we need to activate it. This is done by calling the activate script.

$ source bin/activate

Once this is properly done, your prompt should change to something that resembles

(opencv) $

That should do it. Let's install the libraries.

Installing the libraries

Now we address the elephant in the room. Let us set up OpenCV and some of the essential packages to make it work for us.

(opencv) $
$ pip install opencv-contrib-python # the OpenCV package
$ pip install imutils # convenience functions
$ pip install matplotlib # easily plot the resultant images
$ pip install jupyter # easy, powerful programming environment
$ pip install numpy # for managing all those image arrays

Lets code!

If you want an easy to use programming environment for learning OpenCV, you just installed it. We are, of course, talking about Jupyter Notebook. To start, do

(opencv) $ jupyter notebook

As always, once you are done coding, you can quit by pressing “Ctrl + C”, then deactivate Virtualenv by running “deactivate” command

((opencv) $ deactivate
There you go !!!