Tuesday, October 25, 2016

Installing Dlib on Anaconda Python on Windows

Update - 16/Dec/2019: There is now an easier way to install Dlib from the official pip package. Check out the new tutorial How to Build and Install the Latest Version of Dlib on Anaconda on Windows


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 much 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 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 in action
Dlib's Facial Landmark Detection in 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
(Make sure to unzip the .bz2 file once you download it)

Of course, Dlib is capable of much more than face landmark detection. I'm hoping to dig into some cool features of Dlib in later posts.

But first, we need to install it.


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

But, what it doesn't clearly say is the dependencies that are required to install it. You will need to have CMake installed with a C++ compiler for the python package to install.


Now, that's probably not 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. My first attempt was using CMake Windows binaries with MinGW for C++ compiling. But unfortunately, I wasn't able to get it working with MinGW.

(I later found out that I could make the pip install work by having Visual Studio installed with CMake. More on that later)

After several attempts, I thought of checking whether there are any Anaconda packages for Dlib. While there were no official Dlib conda packages, I was able to find a pre-built Windows binary package for Dlib on Anaconda.org by conda-forge. You can check it out here: https://anaconda.org/conda-forge/dlib

You can install the conda-forge Dlib package by running,
 conda install -c conda-forge dlib  

Conda-forge has made sure to resolve all the dependency conflicts, so I had no issues with the installation. They have pre-built binaries for Windows, Linux, and Mac OS, and Python 2.7 through 3.8. The current package (at the time of this writing) is based on Dlib v19.19.

If you do need the latest version, however, you can get the pip install working by using Visual Studio as the C++ compiler coupled with CMake on Windows. Check out the tutorial here: How to Build and Install the Latest Version of Dlib on Anaconda on Windows.

Dlib has a lot of awesome features. 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 tutorial on How to combine Dlib's Face Landmark Detection with OpenCV.



Related posts:
How to Build and Install the Latest Version of Dlib on Anaconda on Windows
Getting Dlib Face Landmark Detection working with OpenCV
Extracting individual Facial Features from Dlib Face Landmarks

Related links:
http://dlib.net/
https://anaconda.org/conda-forge/dlib



Build Deeper: The Path to Deep Learning

Learn the bleeding edge of AI in the most practical way: By getting hands-on with Python, TensorFlow, Keras, and OpenCV. Go a little deeper...

Get your copy now!

4 comments:

  1. are these commands run in cmd or anaconda prompt?

    ReplyDelete
    Replies
    1. If you have anaconda added to your path, then you can run these in cmd. Otherwise, you need to run these in the anaconda prompt.

      Delete