Introduction

dlib is a C++ machine learning library that is used to create powerful real world application. It is used in many computer vision and machine learning applications, it can be used to detect facial landmarks such as eyes, nose, lips etc which is how snapchat lenses work. Snapchat lenses example Since its a C++ library it might seem a little hard to get it working with you iOS project. This post aims to explain how to add dlib to an existing iOS project. The process involves a bit of work so its important that one follows all the steps in order.

Building dlib

Assuming we already have an iOS project setup we will begin by downloading and building dlib from the dlib website. Start by downloading dlib from here and save it in a directory you can work from. We will need to have X11, and CMake installed on our system. If you do not have cmake you can istall it via homebrew.

brew install cmake
  • Navigate to the examples folder inside the dlib folder via your terminal.
  • Create a new folder called ‘build’
mkdir build && cd build
  • Run the following commands
cmake -G Xcode ..
cmake --build . --config Release

An Xcode project will be created under build/dlib_build

Dlib Xcode Project

Now that we have dlib built and setup lets start adding it to our Xcode project.

Adding dlib to your xcode project

In the root of your Xcode project create a folder called ‘lib’. Copy the ‘libdlib.a’ file from the dlib xcode project and paste it into the ‘lib’ folder. Copy the ‘dlib’ folder from the ‘dlib-19.2’ folder and paste it into the ‘lib’ folder.

Drag the libdlib.a file from the ‘lib’ folder and drop it into ur Xcode project. Once that is done it should show up in your linked libraries and frameworks section of ur Xcode project. This is all the files thats required to add dlib to your Xcode project but if you try to compile, it will not work because we need to set up some compile time flags and add library search paths for xcode to be able to compile the dlib library along with your app.

Configuration

In your Xcode project open ‘Build Settings’ tab and add the following under ‘Library Search Paths’ and ‘Header Search Paths’

$(PROJECT_DIR)/lib

This will let Xcode know where the dlib files are.

Next we need to add some preprocessor macros to help Xcode xompile the dlib files properly.

DLIB_NO_GUI_SUPPORT
DLIB_JPEG_SUPPORT
NDEBUG
DLIB_USE_BLAS
DLIB_USE_LAPACK

Preprocessor Macros

That is all you need to do to add dlib to your project. In some cases you might get ‘BITCODE Error’ in that case just set enable Bitcode to ‘NO’ in the Build Settings.

Conclusion

This post assumes that the reader is using XCode 8+ and Swift 3+ results may vary for older versions of xcode. To use dlib in your swift projects you would need to write a wrapper class. A great example is this github project. It uses dlibs facial feature detection. The author has created a ObjectiveC++ wrapper that he is then able to use with swift. I’ll probably do another post later on how to go about writing a wrapper class.