How to install OpenCV 4.1.2 for OpenVINO and Python on Ubuntu 18.04

Reading Time: 3 minutes

Senior UX Designer for ADLINK Technology

Introduction

OpenVino (and other packages) require a more up to date version of OpenCV than that which is available in the Aptitude repositories for Ubuntu. This process is significantly more complex than the one required for installing OpenCV using Aptitude, but is necessary to get it working with OpenVino.

Step 1: Setup for install (if you have attempted an install before)

First clean any build directories and create an installation directory. This is to make sure that any build artifacts from any previous attempts are cleaned and reduces the risk of a failed build. 

$ cd ~
$ rm -rf opencv/build
$ rm -rf opencv_contrib/build
$ mkdir installation
$ mkdir installation/OpenCV_4_1_2-openvino

Step 2: Create an environment variable with OpenCV Home Directory (one time only)

Next, we will be storing the current working directory in a variable called OpenCV_Home_Dir. It’s only necessary to do this on the first time you follow this tutorial. 

$ echo "export OpenCV_Home_Dir=$(pwd)" >> ~/.bashrc
$ source ~/.bashrc

Step 3: Install Required Packages

Update Packages

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

Install OS Libraries

$ sudo apt-get -y remove x264 libx264-dev
$ sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
$ sudo apt-get -y install git gfortran
$ sudo apt-get -y install libjpeg8-dev libpng-dev libtiff-dev
$ sudo apt-get -y install software-properties-common

Now let’s try to install libjasper-dev:

$ sudo apt-get install libjasper-dev

If you receive an error about libjasper-devbeing missing then follow these instructions:

$ sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
$ sudo apt-get update
$ sudo apt-get install libjasper1 libjasper-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
$ sudo apt-get install libxine2-dev libv4l-dev
$ cd /usr/include/linux
$ sudo ln -s -f ../libv4l1-videodev.h videodev.h
$ cd $OpenCV_Home_Dir
$ sudo apt-get install libxvidcore-dev 
$ sudo apt-get install libgtk-3-dev libtbb-dev qt5-default
$ sudo apt-get install libatlas-base-dev
$ sudo apt-get install libx264-dev x264 v4l-utils

Install Python Libraries

$ sudo apt-get -y install python3.7-dev python3-pip
$ sudo -H pip3 install -U pip numpy
$ sudo apt-get -y install python3-testresources

Step 4: Download opencv and opencv_contrib Sources (first time only)

Clone the Repository

$ cd ~
$ git clone https://github.com/opencv/opencv.git
$ cd opencv
$ git fetch && git fetch --tags
$ git checkout 4.1.2-openvino
$ cd ..
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd opencv_contrib
$ git fetch && git fetch --tags
$ git checkout 4.1.2
$ cd ..

Create an alias for the virtual environment

$ cd $OpenCV_Home_Dir
$ python3 -m venv OpenCV_4_1_2-openvino
$ echo "# Virtual Environment Wrapper" >> ~/.bashrc
$ echo "alias workoncv-4_1_2-openvino=\"source $OpenCV_Home_Dir/OpenCV_4_1_2-openvino/bin/activate\"" >> ~/.bashrc

Install the prerequisites in the virtual environment

Activate the virtual environment

$ source $OpenCV_Home_Dir/OpenCV_4_1_2-openvino/bin/activate

Install the required Python packages into the virtual environment

$ pip install wheel numpy scipy matplotlib scikit-image scikit-learn ipython dlib

Exit the virtual environment

$ deactivate

Step 5: Compile and Install OpenCV with Extra Modules

Activate the virtual environment

$ source $OpenCV_Home_Dir/OpenCV_4_1_2-openvino/bin/activate

Make sure that OpenVINO is in your environment.

$ source /opt/intel/openvino/bin/setupvars.sh

Start by creating a build directory and switch to it.

$ cd ~/opencv
$ mkdir build && cd build

Create the build configuration and files by calling cmake.

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
              -D CMAKE_INSTALL_PREFIX=/usr/local \
              -D WITH_TBB=ON \
              -D WITH_V4L=ON \
              -D OPENCV_PYTHON3_INSTALL_PATH=$OpenCV_Home_DIR/OpenCV_4_1_2-openvino/lib/python3.6/site-packages \
              -D WITH_QT=ON \
              -D WITH_OPENGL=ON \
              -D OPENCV_EXTRA_MODULES_PATH=$OpenCV_Home_DIR/opencv_contrib/modules ..

Now we can build OpenCV which will take some time.

$ make -j$(nproc)

And finally we can install it.

$ sudo make install

Step 6: Testing the Install

To test the install we will use Python.

Activate the virtual environment

$ source $OpenCV_Home_Dir/OpenCV_4_1_2-openvino/bin/activate

Now open the Python shell and execute the following:

$ python
>>> import cv2
>>> print(cv2.__version__)
4.1.2-openvino

If there are no errors your installation is successful.

When you are done deactivate the virtual environment

$ deactivate

Stay in touch

Sign up to our email list to be notified of the latest industry news