How to setup Dlib C++ in the Visual Studio 2013 (text only, I will insert some photos soon)

Download Dlib

  • Go to http://dlib.net/, click on button Download dlib ver… at the end of the navigation bar.
  • Or you can download from the direct link: http://dlib.net/files/dlib-xx.xx.zip
  • In this tutor, I used release ver 19.1 (because from ver 19.2, dlib use C++ 11, supported from Visual Studio 2015 and newer)
  • Extract dlib-xx.xx.zip to a direction, like D:\

Download & Install Cmake and OpenCV (used for webcam_face_pose_ex example)

  1. Cmake
  2. OpenCV
    • Go to http://opencv.org/downloads.html
    • Dlib 19.1 uses opencv2, so that I downloaded and install the newest release of opencv2: 2.4.13
    • Run the file you downloaded, and install opencv in a directory, say C:\
    • You need to add the directory C:\opencv\build\x86\vc12\bin to your system PATH, too (see refer [2] for more details)

Use Cmake to build and generate Visual Studio Project

  • After installing Cmake, open CMake (cmake-gui)  from Start menu
  • Click on Browse Source..., and select your examples folder. In my case, it was: D:\dlib-19.1\examples
  • Click on Browse Build..., then create a folder in somewhere to store your build, and select it. In my case, it was: D:\demo_01
  • You can click on Generate button now, but if you do that, it will build all dlib examples. So we need to customize the CMakeLists.txt a little bit.
    • Open D:\dlib-19.1\examples\CMakeLists.txt
    • You will see some lines in from add_example(example_name).
    • Just remove which you don’t want to build
    • In my case, the final CMakeLists.txt file look like:
#
# This is a CMake makefile. You can find the cmake utility and
# information about it at http://www.cmake.org
#

cmake_minimum_required(VERSION 2.8.4)
PROJECT(examples)
include(../dlib/cmake)

# Tell CMake to compile a program. We do this with the ADD_EXECUTABLE()
# statement which takes the name of the output executable and then a list of
# .cpp files to compile. Here each example consists of only one .cpp file but
# in general you will make programs that const of many .cpp files.
ADD_EXECUTABLE(assignment_learning_ex assignment_learning_ex.cpp)
# Then we tell it to link with dlib.
TARGET_LINK_LIBRARIES(assignment_learning_ex dlib)

# Since there are a lot of examples I'm going to use a macro to simply this
# CMakeLists.txt file. However, usually you will create only one executable in
# your cmake projects and use the syntax shown above.
MACRO(add_example name)
 ADD_EXECUTABLE(${name} ${name}.cpp)
 TARGET_LINK_LIBRARIES(${name} dlib )
ENDMACRO()

#here we apply our macros 
add_example(face_landmark_detection_ex)

find_package(OpenCV QUIET)
if (OpenCV_FOUND)
 include_directories(${OpenCV_INCLUDE_DIRS})

 ADD_EXECUTABLE(webcam_face_pose_ex webcam_face_pose_ex.cpp)
 TARGET_LINK_LIBRARIES(webcam_face_pose_ex dlib ${OpenCV_LIBS} )
else()
 message("OpenCV not found, so we won't build the webcam_face_pose_ex example.")
endif()

if (DLIB_LINK_WITH_SQLITE3)
 add_example(sqlite_ex)
endif()

  • Now, click on Generate button –> Select your Visual Studio version –> Finish
  • In my case, some errors would be shown:
    • CUDA_TOOLKIT_ROOT_DIR not found or specified
    • OpenCV not found, so we won’t build the webcam_face_pose_ex example.
  • Click on OpenCV_DIR and change it to C:\opencv\build or you can create a OpenCV_DIR environment variable. Both are ok.
  • Generate again.
    • Configuring done
    • Generating done
  • Now, click on Open Project. Visual Studio will starts.

Build & Run

  • Click on Solution Explorer, choose your example, right-click, then Build.
  • In my case, I found my .exe file in D:\demo_01\Debug
  • Following the guide in your example.cpp to run the .exe. Good luck.

result

References:

[1] https://aleen42.gitbooks.io/personalwiki/content/qa/dlib.html
[2] https://marcomuraresearch.wordpress.com/2015/04/16/install-opencv-visual-studio/

2 comments

  1. ThuyKem · November 30, 2016

    Thank you for the article. Please help me, I do not find the .exe file in D: \ demo_01 \ Debug. Looking forward to hear from you

    Liked by 1 person

    • theprosperousheartblog · December 1, 2016

      @ThuyKem: D:\demo_01 is where you store the build, isn’t it? And was the project built successfully?

      Like

Leave a comment