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\
If Visual Studio 2017 is used, then in step 5, the command
cmake -G “Visual Studio 14 2015 Win64”
should be replaced by
cmake -G “Visual Studio 15 2017 Win64”.
As you discovered, we ran into problems compiling sample code using VS 2017.
I encounter problem building both dlib-19.6 and 19.7 in both VS 2015 and 2017.
In VS 2015 building process is running only in X86 mode (I launch “cmake ..”), and examples compiling stops when come to dnn_*s.
When try “cmake -G “Visual Studio 14 2015 Win64″” it says “The C compiler identification is unknown” and builds nothing. The same problem with VS 2017. I am in Windows 10.
I’m also seeing errors trying to follow the tutorial using VS2105 and dlib-19.7. Here’s one of the errors…
dlib.lib(cublas_dlibapi.obj) : error LNK2001: unresolved external symbol cudaGetErrorString [D:dlib-19.7examplesbuil
ddnn_mmod_ex.vcxproj]
Looks like the cuda stuff is not linking correctly. Anyone know how to fix this?
Finally it’s built ok, I mean dlib19.7 with VS 2015. As I got, I needed only build “examples”, I followed advice directly from dlib.
Thanks, Andrey. We are going to update this post and have also posted a notice on the top because this might be more complicated than needed.
Nevertheless, after I installed nvidia card, cuda and cudnn, I wanted to build dlib19.7 with VS2105 to enjoy high speed performance with dnn_ examples. This time I failed…Despite I successfully used all that graphical acceleration when worked with Darknet. I see, next post of David Emerson about the same proplem.
VS2017 will go into an infinite loop when it compiling dlib . Too bad… any ideas on how to solve it?
if this cannot be solved. Then I suppose the article https://learnopencv.com/install-opencv3-on-windows/ should be updated as it told us that we can use VS2017 too.
VS2017 will go into an infinite loop when it compiling the examples in dlib . Too bad… any ideas on how to solve it?
if this cannot be solved. Then I suppose the article https://learnopencv.com/… should be updated as it told us that we can use VS2017 too.
If VS2017 is used, there are problems in building the targets for the following projects:
– dnn_metric_learning_ex
– dnn_introduction2_ex
– dnn_face_recognition_ex
– dnn_imagenet_ex
You are on fire Tak :). Yes, we ran into the same problem and that is why we backed out of VS 2017. DNNs are important.
I have the same problem. Did you solve it?
No. And I think this cannot be solved until MS had released an update on VS2017
Hi @tak_chan:disqus , how to build dlib for python on windows? When I ran “python setup.py install” from “Developer Command Prompt for VS 2017”, I got this result “Unknown compiler version – please run configure tests and report the results”. How to pass it?
I built dlib successfully on Windows 10.
At the first time, I could not build it with VS 2017 – problem 1. At the same time, I also built opencv with contrib (use GPU). It also was not successful – problem 2. Because I installed CUDA 8.0 (It only support from VS 2015 or earlier). when installing VS 2015, I solved problem 2. I think dlib has some API that write for cuda 8.0. Of couse, after installing VS 2015, I built dlib successfully.
for step 9, I just cannot run the sample code over a single file.
Instead, I have to use the command:
python face_landmark_detection.py ..shape_predictor_68_face_landmarks.dat ..examplesfaces
As a note, the image 2007_007763.jpg is there in the folder and in fact it is the first image to be processed by running the sample with the 2nd argument set as ..examplesfaces
Thank, Tak. We will update the instructions.
Hi,
I also have problem with step 10 (Test Dlib’s Python example). Images are processed and face landmark detection results are displayed in Windows Power Shell correctly but I can’t visually verify them for several images as they disappear very quickly. It looks like I’m pressing enter button twice (actually, one time), so the script “skips” them and shows me result for next image every time I hit enter button. Could you help me with that please?
On Step 8.2.1: Backup original files :
when I write “mv” command on Command Prompt , it gives this error : ” ‘mv’ is not recognized as an internal or external command,operable program or batch file.”
What should I do ?
Thanks in advance.
I am getting below error, May I Know the reason.
exception thrown!
Unable to load image in file ..faces2008_001009.jpg.
You must #define DLIB_JPEG_SUPPORT and link to libjpeg to read JPEG files.
Do this by following the instructions at http://dlib.net/compile.html.
Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.
So don’t #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions
field in Visual Studio’s Property Pages window so it takes effect for your entire application.
hey,it all worked up to step 9. Then I got an ImportError: No module named dlib from python. Any suggestions?
Which environment do you use? Which python? I think I can support you
Thx for this amazing post.
If I installed dlib using the following command:
conda install -c conda-forge dlib=19.4
is dlib using my gpu or not? Is there a way to know?
Hi @disqus_LbHNiACLBS:disqus, dlib can use GPU. If your PC has not GPU, it will not build some modules for GPU (CUDA)
Hi everyone, Anaconda 3 – for python 3.x.x. How to install dlib’s python module for python 2.7.x? Help me!
You just need to create environment with python 2. Default conda3 root env comes with python 3.6
Anaconda 3 has dlib version19.4 how to install newer version of dlib 19.5 ?
hi, i did install opencv for two days! i didn’t use anaconda ! Is it possible to run dlib without anaconda? I used pip it showed error is there any alternate way ? I’m having phython 2.7.13
Yes, you can do it. Can you show me about the error that you saw?
I think I can support for you!
Mode LastWriteTime Length Name
—- ————- —— —-
d—– 10/5/2017 5:08 AM build
CMake Error: Could not create named generator Visual Studio 15 2017 Win64
Error: could not load cache
dlib does not support VS 2017. You should use VS 2015.
It should be noted that you can ‘pip install dlib’, which will install a windows binary if you are using the official 64bit python 3.6 from python.org.
Also, these instructions for compiling the C++ examples are wildly overcomplicated. Why don’t you follow the instructions on the official dlib webpage? http://dlib.net/compile.html. Instead, you modified the cmake files to be much more complex than they need to be. These instructions are basically wrong. First, you should use the modern cmake dlib::dlib target. That will properly propagate all appropriate compiler settings to the target executable. So do not use dlib_LIBS and DLIB_INCLUDE_DIRS. The right way to do this is shown in the official dlib CMakeLists.txt example: http://dlib.net/examples/CMakeLists.txt.html.
Second, and most importantly, building it this way with visual studio breaks visual studio’s debug mode. For example, if you open the visual studio project generated by cmake with visual studio’s IDE it will be in debug mode by default, because that’s how visual studio works. If you then click on build in the visual studio IDE it will attempt to build a debug project, which links to the MS debug runtime libraries. However, it will *also link to the release mode build of dlib you have in your folder*. That is super bad. You are not allowed to mix visual studio release and debug modes in one application. This is not a dlib limitation, it’s just how visual studio works. If you try to do it anyway you will get either weird errors at linktime, or more likely, you will get bizarre crashes or other runtime bugs showing up. Then I’m going to get questions from confused dlib users because they followed this bad advice. For more information, here is a great article on the issues sourrnding these different runtimes in visual studio http://siomsystems.com/mixing-visual-studio-versions/
If you instead use the official dlib CMakeLists.txt that comes with the examples you won’t have any of these problems with mixing visual studio’s runtimes, you won’t have to setup environment variables either. It will just work. Please update your post to reflect this. I get lots of confused users posting questions about “how to install/use dlib” all the time and essentially all of them are ignoring the very simple and correct instructions at http://dlib.net/compile.html and are instead doing something obviously wrong, like the things suggested in this post. I would really appreciate it if there was less confusing instructions on the net about how to compile dlib. So please update this post.
I realize that compiling C++ programs can be tricky for people unfamiliar with C++ and it’s various compilers and build systems. For instance, the issues with visual studio’s different runtimes and mixing them is not at all familiar to the python community. So don’t take what I’m saying the wrong way. I’m not trying to be a hardass about this stuff. I know it’s tricky. But my point is that it doesn’t need to be. There are simple ways to compile C++ programs. The central challenge is getting people the right information, which is what I’m trying to do here.
I don’t want to use Anaconda, is there any way to install dlib module for python on Windows
Hi, in Step 6: part 4 you show your path as:
D:programmingdlib-19.6buildinstalllibcmakedlib
I have followed everything up until this point but my “build” folder does not have an “install” folder.
I can however find a “config” folder at:
dlib-19.6builddlibconfig
This folder does contain the “dlibConfig.cmake” file as well as a “dlibConfigVersion.cmake” file.
Should I put the path to this folder?
Where can i get the prebuilt wheel sir ??
Hi, in step 10, i got the following error messages, could you help me.
D:OpenCVdlib-19.6python_examples>
D:OpenCVdlib-19.6python_examples>
D:OpenCVdlib-19.6python_examples>python face_landmark_detection.py ..shape_p
redictor_68_face_landmarks.dat ..examplesfaces
Traceback (most recent call last):
File “face_landmark_detection.py”, line 49, in
from skimage import io
File “C:ProgramDataAnaconda3libsite-packagesskimageio__init__.py”, line
7, in
from .manage_plugins import *
File “C:ProgramDataAnaconda3libsite-packagesskimageiomanage_plugins.py”
, line 28, in
from .collection import imread_collection_wrapper
File “C:ProgramDataAnaconda3libsite-packagesskimageiocollection.py”, li
ne 12, in
from PIL import Image
File “C:ProgramDataAnaconda3libsite-packagesPILImage.py”, line 56, in
from . import _imaging as core
ImportError: DLL load failed: 找不到指定的模組。
bro, i also encounter this error, did u fix it?
bro, i fixed this by this:
1. pip uninstall pillow
2. pip install pillow==4.1.1
3. run again above command, u will encounter another error :
can’t open …shape_predictor_68_face_landmarks.dat
fix steps:
1.download from http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
2. extract it to dir ~dlib-19.6
good luck!
Thanks, it was fixed after re-install the pillow package
I love you so muchhhhhh.
I wasted 3 days to install dlib and i am going to give up. Thank you very very much.
But i have one step different: In step 9: conda install -c conda-forge dlib=19.4 doesn’t work with me. Instead of this, i use pip install dlib and it worked.
Hello, i am trying to follow your tutorial for installing Dlib on windows 10 but stuck with this error:
C:UsersrohitDocumentsdlib-19.6build>cmake -G “Visual Studio 15 2017 Win 64” -T host=x64 ^
More? -DJPEG_INCLUDE_DIR=..dlibexternallibjpeg ^
More? -DJPEG_LIBRARY=..dlibexternallibjpeg ^
More? -DPNG_PNG_INCLUDE_DIR=..dlibexternallibpng ^
More? -DPNG_LIBRARY_RELEASE=..dlibexternallibpng ^
More? -DZLIB_INCLUDE_DIR=..dlibexternalzlib ^
More? -DZLIB_LIBRARY_RELEASE=..dlibexternalzlib ^
More? -DCMAKE_INSTALL_PREFIX=install ..
CMake Error: Could not create named generator Visual Studio 15 2017 Win 64
Generators
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be “Win64” or “ARM”.
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be “Win64” or “ARM”.
Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be “Win64” or “ARM”.
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be “Win64” or “ARM”.
Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
Optional [arch] can be “Win64” or “IA64”.
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be “Win64” or “IA64”.
Visual Studio 8 2005 [arch] = Deprecated. Generates Visual Studio 2005
project files. Optional [arch] can be
“Win64”.
Borland Makefiles = Generates Borland makefiles.
NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks – MinGW Makefiles = Generates CodeBlocks project files.
CodeBlocks – NMake Makefiles = Generates CodeBlocks project files.
CodeBlocks – NMake Makefiles JOM
= Generates CodeBlocks project files.
CodeBlocks – Ninja = Generates CodeBlocks project files.
CodeBlocks – Unix Makefiles = Generates CodeBlocks project files.
CodeLite – MinGW Makefiles = Generates CodeLite project files.
CodeLite – NMake Makefiles = Generates CodeLite project files.
CodeLite – Ninja = Generates CodeLite project files.
CodeLite – Unix Makefiles = Generates CodeLite project files.
Sublime Text 2 – MinGW Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 – NMake Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 – Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 – Unix Makefiles
= Generates Sublime Text 2 project files.
Kate – MinGW Makefiles = Generates Kate project files.
Kate – NMake Makefiles = Generates Kate project files.
Kate – Ninja = Generates Kate project files.
Kate – Unix Makefiles = Generates Kate project files.
Eclipse CDT4 – NMake Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 – MinGW Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 – Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 – Unix Makefiles= Generates Eclipse CDT 4.0 project files.
I am having Windows-10 and MS-Visual Studio 17 Cmake version 3.11.0. Can you tell me what i am doing wrong here.
Good afternoon. tell me how to connect the dlib library in visual studio 2017. connect as zdeshttps: / / http://www.youtube.com / watch?v=BKGkA_K1KPA . get errors : error LNK2001: unresolved external symbol “USER_ERROR__inconsistent_build_configuration__see_dlib_faq_2”
Good afternoon. Step 5. (I made a mistake in the previous post . I’m using Visual Studio 14 2015 Win64)
PS C:Usersboris102buildbuildbuilddlib-19.12buildbuildbuildbuild> cd build
PS C:Usersboris102buildbuildbuilddlib-19.12buildbuildbuildbuildbuild>
PS C:Usersboris102buildbuildbuilddlib-19.12buildbuildbuildbuildbuild> # This is a single command. Backticks a
re used for line continuation
PS C:Usersboris102buildbuildbuilddlib-19.12buildbuildbuildbuildbuild> cmake -G “Visual Studio 14 2015 Win64”
`
>> -DJPEG_INCLUDE_DIR=C:Usersboris102builddlib-19.12dlibexternallibjpeg `
>> -DJPEG_LIBRARY=C:Usersboris102builddlib-19.12externallibjpeg `
>> -DPNG_PNG_INCLUDE_DIR=C:Usersboris102builddlib-19.12dlibexternallibpng `
>> -DPNG_LIBRARY_RELEASE=C:Usersboris102builddlib-19.12dlibexternallibpng `
>> -DZLIB_INCLUDE_DIR=C:Usersboris102builddlib-19.12dlibexternalzlib `
>> -DZLIB_LIBRARY_RELEASE=C:Usersboris102builddlib-19.12dlibexternalzlib `
>> -DCMAKE_INSTALL_PREFIX=install ..
>>
cmake: the name “cmake” is not recognized as the name of a cmdlet, function, script file, or running program. Check the rights
innosti spelling of the name, as well as the availability and the correctness of the path, and then try again.
line: 1 character: 1
+ cmake -G “Visual Studio 14 2015 Win64” `
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (cmake:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:Usersboris102buildbuildbuilddlib-19.12buildbuildbuildbuildbuild> cmake –build . –config Release –targ
et INSTALL
cmake : Имя “cmake” не распознано как имя командлета, функции, файла сценария или выполняемой программы. Проверьте прав
ильность написания имени, а также наличие и правильность пути, после чего повторите попытку.
строка:1 знак:1
+ cmake –build . –config Release –target INSTALL
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (cmake:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:Usersboris102buildbuildbuilddlib-19.12buildbuildbuildbuildbuild> cd install
https://uploads.disquscdn.com/images/7944749d11e704fcee89d80f2518dfa5ded692a2ee9395ec7a51a4ba15877f2f.png
I have a problem in Step 6. Its says “Build FAILED” and this is what appears