• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Install Dlib on MacOS

Vaibhaw Singh Chandel
May 31, 2017 16 Comments
Compile Install Tutorial

May 31, 2017 By 16 Comments

Install Dlib on MacOS
In this post, we will provide step by step instructions on how to install Dlib on MacOS and OSX. The installation instructions are slightly different for different versions of the operating system and XCode. Please choose the right one for you.

Install Dlib on MacOS >= 10.11 & XCode >= 8

Step 1: Install OS libraries

brew cask install xquartz
brew install gtk+3 boost
brew install boost-python --with-python3

Step 2: Install Python 2 and/or Python 3

brew install python python3
brew link python
brew link python3
 
# check whether Python using homebrew install correctly
which python2   # it should output /usr/local/bin/python2
which python3  # it should output /usr/local/bin/python3
 
# check Python versions
python2 --version
python3 --version

Python version (2.6 or 2.7, 3.5 or 3.6) installed on your machine is required to determine path of Python’s site-packages. It will be used later.

Step 3: Install Python libraries

We are going to use a Virtual Environment to install Python libraries. It is generally a good practice to separate your project environment and global environment.

# Install virtual environment
pip install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
source ~/.bash_profile
 
############ For Python 2 ############
# create virtual environment
mkvirtualenv facecourse-py2 -p python2
workon facecourse-py2
 
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
 
# quit virtual environment
deactivate
 
############ For Python 3 ############
# create virtual environment
mkvirtualenv facecourse-py3 -p python3
workon facecourse-py3
 
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
 
# quit virtual environment
deactivate

Step 4: Install Dlib

Step 4.1: Compile C++ library

brew install dlib

Step 4.2: Compile Python module

Now activate “facecourse-py2” or “facecourse-py3” virtual environment based on whether you were following the process for Python2 or Python3.

############ For Python 2 ############
workon facecourse-py2

############ For Python 3 ############
workon facecourse-py3

We will now install Dlib using pip.

pip install dlib

Installation is complete. You can now exit from virtualenv using deactive.

deactivate

 

Install Dlib on MacOS 10.10 & XCode < 8

Step 1: Install OS libraries

brew cask install xquartz
brew install gtk+3 boost
brew install boost-python --with-python3

Step 2: Install Python 2 and/or Python 3

brew install python python3
brew link python
brew link python3
 
# check whether Python using homebrew install correctly
which python2   # it should output /usr/local/bin/python2
which python3  # it should output /usr/local/bin/python3
 
# check Python versions
python2 --version
python3 --version

Python version (2.6 or 2.7, 3.5 or 3.6) installed on your machine is required to determine path of Python’s site-packages. It will be used later.

Step 3: Install Python libraries

We are going to use a Virtual Environment to install Python libraries. It is generally a good practice to separate your project environment and global environment.

# Install virtual environment
pip install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
source ~/.bash_profile
 
############ For Python 2 ############
# create virtual environment
mkvirtualenv facecourse-py2 -p python2
workon facecourse-py2
 
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
 
# quit virtual environment
deactivate
 
############ For Python 3 ############
# create virtual environment
mkvirtualenv facecourse-py3 -p python3
workon facecourse-py3
 
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
 
# quit virtual environment
deactivate

Step 4: Compile DLib

Step 4.1: Compile C++ binary

Dlib uses few C++11 features which XCode 7 does not support. We have patched Dlib v19.4 by replacing this feature with Dlib’s internal function. This patched version of Dlib compiles with XCode 7. So if you are using XCode 7, use branch v19.4-thread-local-patch of our Dlib fork in your projects.

Davis King, creator of Dlib, recommends using CMake for using Dlib in your code. But if you want to use Dlib as a library follow these steps:

git clone https://github.com/vaibhawchandel/dlib.git
cd dlib
git checkout v19.4-thread-local-patch
mkdir build
cd build
cmake ..
cmake --build . --config Release
make install
cd ..

Now you can use pkg-config to provide path to Dlib’s include directory and link Dlib library file.

pkg-config --libs --cflags dlib-1

Step 4.2: Compile Python module

Now activate “facecourse-py2” or “facecourse-py3” virtual environment based on whether you were following the process for Python2 or Python3.

############ For Python 2 ############
workon facecourse-py2

############ For Python 3 ############
workon facecourse-py3

Now run the following command to build Python module.

python setup.py install

For consistency we have installed Python and C++ binaries of Dlib using same source code.

Since we installed dlib in facecource-py3/facecourse-py2 virtualenv, dlib was installed only in this virtualenv and not globally. So whenever you want to use dlib, you have to activate the virtualenv using workon command as we used earlier in this post.

Now exit from virtualenv

deactivate

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in all posts of 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.

Subscribe Now

Tags: C++ dlib Dlib 19.4 Dlib 19.5 Dlib 19.6 macos osx Python

Filed Under: Compile, Install, Tutorial

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching
  • Classification with Localization: Convert any Keras Classifier to a Detector

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy