Installation notes – OpenCV in Python 3 and Ubuntu 17.04.

These are my installation notes for OpenCV in Ubuntu 17.04 linux for python3. These notes will help start a computer vision project from scratch. OpenCV is one of the most widely used libraries for image recognition tasks. The first step is to fire-up the linux terminal and type in the following lines of commands. These first set of commands will fulfill the OS dependencies required for installing OpenCV in Ubuntu 17.04.

Step1:

Update the available packages by running:

$ sudo apt-get update
$ sudo apt-get upgrade

Step2:

Install developer tools for compiling OpenCV 3.0

sudo apt-get install build-essential cmake git pkg-config

Step3:

Install packages for handling image and video formats:

$ sudo apt-get install libjpeg8-dev libtiff5-dev

Add installer repository from earlier version of Ubuntu to install libjasper and libpng and install these two packages:

$ echo "deb http://us.archive.ubuntu.com/ubuntu/ yakkety universe" | sudo tee -a /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install libjasper-dev
$ echo "deb http://mirrors.kernel.org/ubuntu/pool/main/libp/libpng/ xenial main" | sudo tee -a /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install libpng-dev

Install libraries for handling video files:

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

Step4:

Install GTK for OpenCV GUI and other package dependencies for OpenCV:

$ sudo apt-get install libgtk2.0-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install python3-pip

Step5:

The next step is to check if our python3 installation is properly configured. To check python3 configuration, we type in the following command in terminal:

python3.5-config --includes

The output from the terminal for the command above indicates the location pointers to target directory and current directory for the python configuration file. These two locations must match, before we should proceed with installation of OpenCV.

Example of a working python configuration location without any modification will look something like this:

-I/usr/include/python3.5m -I/usr/include/python3.5m

The output from the terminal has a specific order, with the first pointer indicating the target location and the second pointer indicating the current location of the config file. If the two location pointers don’t match, use cp shell command to copy the configuration file to the target location. We are using the sudo prefix to give administrative rights to the linux terminal to perform the copying procedure. If your linux environment is password protected, you will very likely need to use sudo prefix. Otherwise, one can simply the skip the sudo prefix and execute the rest of the line in terminal. I am using sudo here because it is required for my linux installation environment.

sudo cp /usr/include/some/location/python3.5m/pyconfig.h /usr/include/python3.5m/

Step6 (Optional):

Set-up a virtual environment for python:

$ sudo pip3 install virtualenv virtualenvwrapper

The next step is to update ~/.bashrc file. Open the file in text editor. Here we are using nano.

$ sudo nano ~/.bashrc

At the end of the file, past the following text to update the virtual environment parameters:

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Now, either open a new terminal window or enforce the changes made to bashrc file by:

$ source ~/.bashrc

Create a virtual environment for python named OpenCV:

$ mkvirtualenv OpenCV

Step7:

Add python developer tools and numpy to the python environment we want to run OpenCV. Run the following code below:

$ sudo apt-get install python3.5-dev
$ sudo pip3 install -U numpy

Step8:

Now we have to create a directory to copy the OpenCV source files from GitHub and download the source-files from GitHub to our local directory. This can be achieved using mkdir and cd commands along with git command. Here is an example:

$ cd ~
$ git clone https://github.com/opencv/opencv.git
$ cd opencv
$ git checkout 3.3.0

We also need OpenCV contrib-repo for access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) and newer OpenCV 3.0 features like text detection in images.

$ cd ~
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd opencv_contrib
$ git checkout 3.3.0

Step9:

The final step is to build, compile and install the packages from the files downloaded from GitHub. One thing to keep in mind here is that: we are now working in the newly created folder in terminal and not in the home directory. An example of linux terminal code to perform the installation of OpenCV is given below. Again, I am using sudo prefix to allow the terminal to have elevated privileges while executing the commands. It may not be necessary in all systems, depending on the nature of the linux installation and how system privileges are configured.

$ cd ~/opencv
$ cd ~/opencv
$ mkdir build
$ cd build
$ sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \ 
             -D CMAKE_INSTALL_PREFIX=/usr/local \ 
             -D INSTALL_C_EXAMPLES=ON \ 
             -D INSTALL_PYTHON_EXAMPLES=ON \ 
             -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ 
             -D BUILD_EXAMPLES=ON ..

Step10:

The final step of compiling and installing from the source is a very time consuming process. I have tried to speed this process up by using all the available processors to compile. This is achieved using the argument nproc –all for the make command.

Here are the command line instructions to install OpenCV from the installer we just built:

$ sudo make -j $(nproc --all)
$ sudo make install
$ sudo ldconfig

For the OpenCV to work in python, we need to update the binding files. Go to the install directory and get the file name for the OpenCV file installed. It is located in either dist-packages or site-packages.

The terminal command lines are the following:

cd /usr/local/lib/python3.5/dist-packages
ls

Now we need to update the bindings to the python environment we are going to use.  We also need to rename the symbolic link to cv2 to ensure we can import OpenCV in python as cv2. The terminal commands are as follows:

$ cd ~/.virtualenvs/OpenCV/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

Once OpenCV is installed, type cd / to return to home directory in terminal. Then type python3 to launch the python environment in your OS. Once you have launched python3 in your terminal, try importing OpenCV to verify the installation.

Let us deactivate the current environment by:

$ deactivate

First we need to ensure, we are in the correct environment. In this case we should activate the virtual environment called OpenCV and then launch the python terminal interpreter. Here are the terminal commands:

$ workon OpenCV
$ python

Let us try to import OpenCV and get the version number of the installed OpenCV version. Run these commands in the terminal:

import cv2
cv2.__version__

If the import command in python3 console returned no errors, then it means python3 can successfully import OpenCV. We will have to do some basic testing to verify if OpenCV installation has been successfully installed in your Ubuntu 17.04 linux environment. But, if one manages to reach up to this point, then it is a great start. Installing OpenCV is one of the first steps in preparing a linux environment to solve machine vision and machine learning problems.

A more concise version of the instructions is also available on GitHub. Using the Moad Ada dev-ops server for deep-learning, the linux environment comes pre-installed with OpenCV. This makes it easier for artificial intelligence & deep learning application developers to develop machine vision applications. The dev-ops server can be ordered here: Moad online store.

Leave a comment

Your email address will not be published. Required fields are marked *