Installing Dlib for Python
Python has a rich list of packages available for a variety of functionality like web scraping, web development, image processing etc. Especially for face recognition python has different packages some of which are OpenCV, dlib, face_recognition. This post will help you in the installation of dlib in your python environment.
You may wonder when OpenCV is available why the need for dlib? Dlib is much more advanced and has better algorithms for face detection and recognition. The detection and recognition is much accurate as compared to transform based recognition in OpenCV.
But at the same time it has a lot of dependencies which needs to be resolved first to install it in your PC. Else you won’t be able to use dlib or face_recognition packages (face_recognition uses inbuilt dlib). These are the steps that I have followed to install dlib. I did have a lot of problem while installing. I will highlight them so that you do not fall in the same trap.
Before starting you need to have a desktop or laptop with Nvidia graphics card. If not, though you may be able to install dlib, it will use CPU for Deep Learning Algorithms which will take insane amount of time😅. Also you need to have Anaconda or Python installed. So lets get started with one step at a time!
Step 1: Upgrade Nvidia GPU Drivers
The first step is simple. Update your GPU drivers to the latest version found at their official site — https://www.nvidia.in/Download/index.aspx?lang=en-in. This was around 500MB for my MX150 graphic card😥
Step 2: Install Visual Studio and Visual Studio Build Tools (2019, 2017 or 2015)
This is an important step as Dlib requires Visual Studio Build Tools to be available. This is because it requires the compiler for compiling through Cmake. I have installed the Visual Studio 2019 Community edition (Visual Studio and Visual Studio Build Tools both are different. Install both as honestly I have not tried with just Visual Studio Build Tools). Beware of what to install! You don’t need the whole Visual Studio to be installed. Just install the default options in C++ build tools as shown below. I messed up at this stage as I did not install any of the components 😅. Especially do not forget to install the Windows 10 SDK as it has the compilers! This is huge and it takes around 7 to 8GB in size 😥.
Step 3: Download the Nvidia CUDA Toolkit and cuDNN for your GPU Drivers
This step is necessary for utilizing the GPU for Deep Learning. Install the CUDA Toolkit for your GPU from the site — https://developer.nvidia.com/cuda-downloads . I would suggest to download the 10.1 version in the Legacy Release rather than the latest one. 10.1 is compatible with tensorflow which has yet to support the latest version. The Toolkit size is around 3gb 😣. Just double click the exe and let it run through its stages.
After this you need to install the cuDNN from Nvidia. This requires registration and creating account in nvidia.Developer. Go to this — https://developer.nvidia.com/cudnn and create your account and download the cuDNN zip file. This is around 200–300 MB 😅.
After unzipping you need to copy the files of cuDNN into the folder of ‘C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1’ where the Toolkit was installed. For correct steps go through the installation procedure for Windows in the site — https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html
Step 4: Install CMake software
Install CMake from this site — https://cmake.org/download/ . Its important to select the option for creation of environment variable. After installation check if the environment variable is created for this. Press Windows + R and type control sysdm.cpl. Go to Advanced and click on Environment Variables. In User Variable click on Edit and see if below path is added else add it manually (the path where you have installed CMake).
Step 5: Finally Time to Install Dlib
After installing all the above software, you are now ready to install dlib in your python environment. Open your Anaconda prompt (as I am using Anaconda 😅) and as a precaution first install cmake as ‘pip install cmake’. Then type ‘pip install dlib’. It may download the dependencies for these and then install dlib. Congratulations!! You can now use dlib in python!
If you want to check whether your dlib is utilizing GPU check with below commands. As shown below it should show dlib.DLIB_USE_CUDA as True.
Honestly I had a lot of trouble while installing dlib. After lot of many tries and losing huge amount of my precious data I was able to chalk these steps. I hope that by following them you were able to install dlib without any fuss!
Originally published at http://evrythngunder3d.wordpress.com