Pages

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!

10 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
  2. I have done what you said, but when typing import dlib, it's saying that I don't have that module. How to slove it?

    ReplyDelete
  3. UnsatisfiableError: The following specifications were found to be in conflict:
    - dlib=19.4
    - pytorch-cpu
    Use "conda info " to see the dependencies for each package.

    giving this error

    ReplyDelete
  4. I have installed Anaconda and dlib. But When I tried to import dlib, this error is coming. please tell me solution of this.
    Traceback (most recent call last):
    File "", line 1, in
    File "C:\Anacoda3\lib\site-packages\dlib\__init__.py", line 1, in
    from .dlib import *
    ImportError: DLL load failed: The specified module could not be found.

    ReplyDelete