Tuesday, October 25, 2016

Installing Dlib on Anaconda Python on Windows

Updated: 03/Jul/2017

Dlib is a Machine Learning library, primarily written in C++, but has a Python package also. It has many useful and optimized algorithms useful for machine learning, linear algebra, data structures, image processing and many more available out-of-the-box.
"Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real world problems. It is used in both industry and academia in a wide range of domains including robotics, embedded devices, mobile phones, and large high performance computing environments. Dlib's open source licensing allows you to use it in any application, free of charge." - dlib.net
One of the most popular features in Dlib is the Facial Landmark Detection. Dlib installation ships with a pre-trained shape predictor model named shape_predictor_68_face_landmarks.dat, which as the name suggests, is trained to detect 68 facial keypoints including eyes, eyebrows, mouth, nose, face outline etc.

Dlib's Facial Landmark Detection is action
Dlib's Facial Landmark Detection is action
You can view the sample code for face landmark detection here at the Dlib website, and download the pre-trained model from: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
Of course, Dlib is capable of much more than face landmark detection. I'm hoping to dig in to some cool features of Dlib in later posts.

But first, we need to install it.

The Dlib homepage suggests to use pip to install the Dlib Python Interface,
 pip install dlib  

But, what it doesn't say is what are the dependencies that are required to install it. You will need to have CMake installed with a C++ compiler in order for the python package to install.
Now, that's probably not be a problem on Linux - just have cmake and gcc packages installed and you're done.

On Windows however, it's a different story.
I have attempted multiple times to get the compilation working, with CMake Windows binaries with MinGW for C++ compiling, but still I wasn't able to get it working.

After many days trying to compile, I gave up and looked to see whether there's any Anaconda only solutions for installing Dlib.
Finally, I found a pre-built Windows binary package for Dlib on Anaconda.org by the group conda-forge - https://anaconda.org/conda-forge/dlib
The package from conda-forge was Dlib v19.0, while the latest version of Dlib source was v19.2 at the time of this writing.
While v19.2 had several improvements, v19.0 still had a fairly decent feature set. And it's better than not being able to compile the library, so I decided to give it a try.

Update: Conda-forge has released the package for Dlib 19.4.
Read about it here: OpenCV 3.2 and Dlib 19.4 Packages Now Available from Conda-Forge

You can install it using,
 conda install -c conda-forge dlib=19.4  

I had no issues or package conflicts while installing it, and it worked perfectly.

Once you get to know a little bit of the internals of the Dlib Python interface, it can be flexible enough to be combined with other libraries such as OpenCV. Check here to see my post on How to combine Dlib's Face Landmark Detection with OpenCV.

Related posts:
Getting Dlib Face Landmark Detection working with OpenCV
Extracting individual Facial Features from Dlib Face Landmarks

Related links:

1 comment: