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.
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.
Ubuntu 16.04 should be pre-installed on your system
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)
// To see which python 3.x version is present
// To see which default python version you are using
// To exit from python console runexit()
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
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
// Download and Unzip OpenCV 3.1.0 at home (~) directory
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
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) –
# Goto opencv-3.1.0 directorycd opencv-3.1.0
# Create and goto build directory
# 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
# Substitute 4 by output of nproc
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
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/ -typef -name "cv2*.so"
The output should look something like this –
##----- For Python 2 -----#### binary installed in dist-packages
## binary installed in site-packages
##----- For Python 3 -----#### binary installed in dist-packages
## binary installed in site-packages
* 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 ##-----##
##----- For Python 3 -----##
// Enter into python3.5 console
python3.5# Now, import cv2 and check version in python console
# 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 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
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
# Open python console
python3.5# Import openCVimport cv2
cv2.__version__ # Should show the output OpenCV 3.1.0import 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!
AIMagnifi is an Artificial Intelligence and Computer Vision based company. We primarily aim to target the unexplored combination of retail sector with the power of AI. We currently have an essence of expertise in the areas of Artificial Intelligence, Computer Vision, Facial Recognition, Machine Learning, Deep Learning and Data Analytics. We target to bring out the best outcomes out of these areas that would ultimately personalize and enhance the experience of the end-user. For further details visit our website - http://aimagnifi.com