How to install and configure OpenCV 3.x and Dlib 19.x with Python 3.5+ on Ubuntu 16.04 (Linux/Debian) for Face Recognition

Computer Vision and machine learning are the latest buzz word worldwide and the technologies behind these domains are growing rapidly. Well, OpenCV is one the most popular softwares when it comes to face recognition. At first, configuring openCV on linux environment is a bit challenging task in itself especially when you see so many different dependencies and versions available on the internet.

Like most of the first-timers, I also tried various permutation and combination with python and openCV and spent lots of odd hours with just setting and messing things up. So, I thought I might not be the only person who is facing this type of trouble with basic configuration stuff. So, after reading so many blog posts and all the experiments, I finally decided to remove all the confusions with respect to openCV, python, ubuntu, and dlib and made my mind to write this blog post so as to help the computer vision community.

Well, before starting things up, I would suggest and recommend you guys to use python 3 as of nowadays most of the companies and technocrats are migrating their codebase from python 2.x to 3.x in order to stay updated and get support in near future. I am using this combination only i.e.; OpenCV 3.1.0 with Python 3.5.2 on Ubuntu 16.04 as this is the reliable and stable version I found.

What is OpenCV ?

OpenCV Logo

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements and it can possibly do many more interesting things.

Pre-requisites

  1. Ubuntu 16.04 should be pre-installed on your system
  2. Well, Ubuntu 16.04 comes with pre-installed python 2.7.12 and python3.5.2. Open the terminal and run the following command in order to verify which python version you are using –
// To see which python 2.x version is present (default)
python

// To see which python 3.x version is present
python3

// To see which default python version you are using
which python

// To exit from python console run
exit()

Steps to configure OpenCV 3.1.0 and Dlib 19.7 with python 3.5.2 (Tested)

Step #1 – Update System Packages

sudo apt-get update
sudo apt-get upgrade

Step #2 – Install Dependencies Before Configuring OpenCV

// Remove any previous installations of x264

# For encoding video streams into the H.264/MPEG-4 AVC format
sudo apt-get remove x264 libx264-dev
 

// Start installing dependencies now
 
# Install CMAKE
sudo apt-get install build-essential checkinstall cmake pkg-config yasm

# GNU Fortran compiler
sudo apt-get install git gfortran

# Libraries for the coding and manipulation of images and implementing an interface for reading and writing PNG (Portable Network Graphics), JPEG,  format files.
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
 

// If you are using Ubuntu 14.04
sudo apt-get install libtiff4-dev

// If you are using Ubuntu 16.04
sudo apt-get install libtiff5-dev
 
# Install Video Codecs, Streamer Plugins, QT, MP3 encoders that converts audio to the MP3 file format
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
 

// Optional dependencies

# Protocol Buffers for faster serialization of structured data 
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev

# To allow access to digital camera by external programs
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

Step #3 – Install python libraries – pip | numpy

sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo -H pip2 install -U pip numpy
sudo -H pip3 install -U pip numpy

Step #4 – Setup Virtual Environment (Recommended)

# Install virtual environment
sudo pip2 install virtualenv virtualenvwrapper
sudo pip3 install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"  >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
  
## ------- For Python 2.x ------- ##

# Create virtual environment
mkvirtualenv env-py2 -p python2
workon env-py2
  
# Install matplotlib, scikit-image, scikit-learn within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn
  
# Exit from virtual environment
deactivate
## ------- End ------- ##
  

# ------- For Python 3.x -------

# Create virtual environment
mkvirtualenv env-py3 -p python3
workon env-py3
  
# Install matplotlib, scikit-image, scikit-learn within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn
  
# Exit from virtual environment
deactivate
## ------- End ------- ##

Step #5 – Download and Configure OpenCV 3.1.0

// Download and Unzip OpenCV 3.1.0 at home (~) directory
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip opencv.zip


// Download and Install OpenCV_Contrib file at home (~) directory
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
unzip opencv_contrib.zip

Step #5.1 – (Important) Few Bug Fixes before start making build of OpenCV 3.1.0

a) Goto directory – “opencv-3.1.0/modules/python/” and replace “common.cmake” file with this file.

b) Goto directory – “opencv_contrib-3.1.0/modules/tracking/include/opencv2/tracking/” and replace “onlineMIL.hpp” file with this file.

c) Goto directory – “opencv_contrib-3.1.0/modules/tracking/src/” and replace “onlineMIL.cpp” file this file.

* Note – We are replacing these files because there exist few build errors while making the build of openCV-3.1.0. So, it’s better to fix them priorly. Here are the GitHub links for above-mentioned build fixes (just for reference, don’t need to do anything if you have replaced the files as stated above) –

  1. https://github.com/opencv/opencv_contrib/issues/618
  2. https://github.com/opencv/opencv_contrib/pull/680/commits/395db9e7211d58473beafa6a9643d3cd49ad4eb4
  3. https://github.com/opencv/opencv/issues/6016

Step #5.2 – Start making build of OpenCV

# Goto opencv-3.1.0 directory
cd opencv-3.1.0

# Create and goto build directory
mkdir build
cd build

# Run CMAKE command
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules \
      -D BUILD_EXAMPLES=ON ..

# To find out number of CPU cores in your machine
nproc

# Substitute 4 by output of nproc
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

Step #5.3 – Create Symlink between system environment and virtual environment

After build completion, cross-check that “cv2.so” file should be present inside site-packages or dist-packages. Here’s how you can check by running the following command –

find /usr/local/lib/ -type f -name "cv2*.so"

The output should look something like this –

##----- For Python 2 -----##
## binary installed in dist-packages 
/usr/local/lib/python2.6/dist-packages/cv2.so
/usr/local/lib/python2.7/dist-packages/cv2.so
## binary installed in site-packages
/usr/local/lib/python2.6/site-packages/cv2.so
/usr/local/lib/python2.7/site-packages/cv2.so
  
##----- For Python 3 -----##
## binary installed in dist-packages
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so
/usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so
## binary installed in site-packages
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
/usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so

* Note – It is suggested to cross-verify the exact path before moving to next step. Now, run these commands to create symlink between virtual environment and system environment –

##----- For Python 2.7 -----##
cd ~/.virtualenvs/env-py2/lib/python2.7/site-packages
ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
  
##----- For Python 3.5 -----##
cd ~/.virtualenvs/env-py3/lib/python3.5/site-packages
ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

Congratulations!! If you have successfully run your code till here then most likely you might have successfully configured OpenCV 3.1.0 on your Ubuntu 16.04. Now, let’s proceed and test the setup.

// Activate your virtual environment

##----- For Python 2 ##-----##
workon env-py2 
##----- For Python 3 -----##
workon env-py3

// Enter into python3.5 console 
python3.5

# Now, import cv2 and check version in python console
import cv2
cv2.__version__

# If OpenCV3 is installed correctly,
# above command should give output 3.1.0
## ***** If this executes perfectly then congratulations! *****##
## ***** Now, You are all set to start next step of configuring Dlib 19.7 *****##

Step #6 – Install and configure 19.7 (latest till Dec’2017)

Dlib C++ Library Logo

Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real-world problems. It has been developed by Davis King. From a computer vision perspective, has a number of state-of-the-art implementations, including:

  • Facial landmark detection
  • Correlation tracking
  • Deep metric learning

If you have followed above mentioned steps strictly, then trust me you are just one step away from configuring dlib. Just run the following command and it’s done –

# Install dlib 19.7 (latest till Dec'2017)
pip install dlib

If you are still facing any problem then I would suggest you read out this awesome Pyimagesearch – blog post by Adrian Rosebrock which will give you step by step guide to install and configure dlib.

Step #7 – Run and test the full configured setup

# Goto virtual environment as per your python version which you are using
workon env-py2 
workon env-py3

# Open python console
python3.5

# Import openCV
import cv2
cv2.__version__ # Should show the output OpenCV 3.1.0

import dlib
dlib.__version__ # Should show the output dlib 19.7

Step #8 – This is it, You are done now! Enjoy the power of computer vision!

If you found this blog post helpful, please do like and subscribe for latest updates. If you have any questions, feel free to drop them below in the comments section. I would suggest you to read, explore and gain more knowledge about artificial intelligence, machine learning and computer vision on AIMagnifi blog.

Facebook | Twitter | Instagram | Linked IN

Special heartful thanks & credits to Pyimagesearch | Learnopencv |Github (For bug fixing — OpenCV) for helping the Computer Vision community worldwide.

Top