In this post, we will provide step by step instructions on how to install Dlib on Windows.
Compiling Python modules such as NumPy, SciPy etc. is a tedious task. Anaconda is a great Python distribution which comes with a lot of pre-compiled Python packages. So we will use Anaconda as our Python distribution.
An alternative to Anaconda is that you install official Python library and use Christoph Gohlke’s awesome repository to install pre-compiled Python modules.
This tutorial is based on using Anaconda so we may not be able to help if you choose to use Gohlke’s precompiled binaries or if you compile Python libraries from source.
If you intend to use Dlib only in C++ projects, you can skip Python installation part.
Now let’s go through the steps to install Dlib. Follow our previous post Install OpenCV3 on Windows to complete Step 1, 2 and 3.
Step 1: Install Visual Studio 2015
Step 2: Install CMake v3.8.2
Step 3: Install Anaconda 3
Step 4: Download Dlib
Download Dlib v19.6 from http://dlib.net/files/dlib-19.6.zip
Step 5: Build Dlib library
Extract this compressed file. Open Windows PowerShell or Command Prompt and move to the directory where you have extracted this file.
If you are running these commands on Command Prompt replace ` (backtick) with ^ (caret).
cd dlib-19.6\ mkdir build cd build # This is a single command. Backticks are used for line continuation cmake -G "Visual Studio 14 2015 Win64" ` -DJPEG_INCLUDE_DIR=..\dlib\external\libjpeg ` -DJPEG_LIBRARY=..\dlib\external\libjpeg ` -DPNG_PNG_INCLUDE_DIR=..\dlib\external\libpng ` -DPNG_LIBRARY_RELEASE=..\dlib\external\libpng ` -DZLIB_INCLUDE_DIR=..\dlib\external\zlib ` -DZLIB_LIBRARY_RELEASE=..\dlib\external\zlib ` -DCMAKE_INSTALL_PREFIX=install .. cmake --build . --config Release --target INSTALL cd ..
Dlib will be installed within dlib-19.6\build\install directory. We will use CMake to build Dlib examples but you can use Visual Studio too. This directory (dlib-19.6\build\install) contains include and library folders which you can specify in Visual Studio to build projects using Dlib.
Step 6: Update user environment variable – dlib_DIR
This environment variable is needed for CMake to find out where Dlib is installed. CMake looks for a file named dlibConfig.cmake within directory dlib_DIR to find Dlib’s include and library directories.
- Press Windows Super key, search for “environment variables”.
- Click Environment Variables in System Properties window.
- Click New in “User Variables” (in upper half of window).
- Under variable name write dlib_DIR and under variable value write full path to directory dlib-19.6\build\install\lib\cmake\dlib
On my machine path is: D:\programming\dlib-19.6\build\install\lib\cmake\dlib
This directory contains file “dlibConfig.cmake”. This is used by CMake to configure dlib_LIBS and dlib_INCLUDE_DIRS variables to generate project files. Before assigning the value to variable dlib_DIR make sure that this path has file dlibConfig.cmake.
- Now click ok to save and close environment variables window.
Note: If you have an open Command Prompt/Power Shell window before these values were updated, you have to close and open a new Command Prompt/Power Shell window again.
Step 7: Build Dlib examples
We will use our CMakeLists.txt file instead of one which is shipped with Dlib.
Download modified CMakeLists.txt file and put it in dlib-19.6\examples directory and replace the default one with this one. Then follow the steps given below:
cd dlib-19.6/examples mkdir build cd build cmake -G "Visual Studio 14 2015 Win64" .. cmake --build . --config Release cd ../..
Once build is complete, it will generate executables for all examples in examples\build\Release folder.
Step 8: Test Dlib’s C++ example
We will test Face Landmark Detection demo to check whether we have installed Dlib correctly.
Download trained model of facial landmarks from Dlib’s website. Extract this file (shape_predictor_68_face_landmarks.dat.bz2) to Dlib’s root directory (dlib-19.6).
cd examples\build .\Release\face_landmark_detection_ex.exe ..\..\shape_predictor_68_face_landmarks.dat ..\faces\2008_001009.jpg
Step 9: Install Dlib’s Python module (Only Anaconda 3)
Compiling Python bindings for Dlib from source is non-trivial. You have to compile Boost.Python from scratch and configure some environment variables (such as BOOST_ROOT and BOOST_LIBRARYDIR) before you can compile Python module of Dlib. We are skipping that part for now. A complete tutorial on how to build Dlib Python bindings from source will be released in future.
To save time and efforts it is suggested to use Anaconda 3. You can install a compiled binary of dlib v19.4 from Anaconda. At the time this article was updated Dlib’s latest available version on Anaconda’s conda-forge repository is 19.4. So we will install v19.4 instead of 19.6
conda install -c conda-forge dlib=19.4
Step 10: Test Dlib’s Python example
cd dlib-19.6\python_examples python face_landmark_detection.py ..\shape_predictor_68_face_landmarks.dat ..\examples\faces\
Subscribe & Download Code
If you liked this article and would like to download code (C++ and Python) and example images used in all posts in this blog, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.