9

Sorry if this is basic but I'm trying to install dlib to use with python as mentioned in (http://blog.dlib.net/2014/04/dlib-187-released-make-your-own-object.html) "Make your own object detector in Python!".

I downloaded the install files, unzipped, and used cmake as in the install instructions (http://dlib.net/compile.html)

cd examples
mkdir build
cd build
cmake ..
cmake --build . --config Release

which seemed to work fine

However typing "import dlib" in Python just gives ImportError: No module named dlib.

Any idea how I tell Python how to find / use the thing?

Tim 333
  • 942
  • 1
  • 8
  • 9

10 Answers10

8

Just a note for reference that the easiest way to install is now to use:

sudo python setup.py install
liezlp
  • 81
  • 1
  • 1
5

Instead of manual file editing, you can pass -DPYTHON_LIBRARY:FILEPATH=/path/to/your/libpython2.7.dylib to cmake.

What ./compile_dlib_python_module.bat does is

mkdir build
cd build
cmake ../../tools/python

So, just run the commands by one by one and instead of

cmake ../../tools/python

run

cmake ../../tools/python -DPYTHON_LIBRARY:FILEPATH=/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib
Kulbir Saini
  • 3,926
  • 1
  • 26
  • 34
5

Dlib installation instructions for OSX (python3)

brew uninstall boost-python
brew uninstall boost
brew install boost-python --with-python3 --without-python

Installing default dlib

pip3 install dlib

Following Gives Some speedup Installing dlib with AVX, SSE2, SSE4 instructions enabled (download the source code from dlib.net and execute from within the directory).

python setup.py install --yes USE_AVX_INSTRUCTIONS --yes USE_SSE2_INSTRUCTIONS --yes USE_SSE4_INSTRUCTIONS
Ankur Jain
  • 507
  • 3
  • 9
4

On ubuntu I had to do the following:

sudo apt-get install libboost-python-dev cmake

cd to dlib-18.15/python_examples

Then:

./compile_dlib_python_module.bat 

I then copied the dlib.so to dist-packages so it would be in my path.

sudo cp dlib.so /usr/local/lib/python2.7/dist-packages/

According to the documentation compile_dlib_python_module.bat will work on any os once you have both CMake and boost-python installed.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Thanks - I did the Mac equivalent and it worked except Python import dlib now segfaults. Onward... – Tim 333 May 05 '15 at 19:38
  • Had a quick google and it seems it is related to boost-python, can you add the full traceback to pastebin? – Padraic Cunningham May 05 '15 at 19:45
  • Here's the traceback http://pastebin.com/wwcMVXML the brew boost file was boost-1.56.0.mavericks.bottle.1.tar.gz and I'm on osx 10.9.5. Thanks in advance if you can figure anything – Tim 333 May 05 '15 at 20:35
  • can you import from the system python? – Padraic Cunningham May 05 '15 at 20:44
  • I just tried the system python and it also segfaulted with a slightly different error http://pastebin.com/t74tD28e . I'm guessing it may be something to do with OSX Mavericks not being compatible with something perhaps – Tim 333 May 05 '15 at 22:57
  • @Tim333 Did you figure anything out related to this? I just tried compiling on OSX and I'm getting the same seg-fault error. – Adrian Rosebrock May 18 '15 at 22:15
  • @AdrianRosebrock, how did you install boost-python? – Padraic Cunningham May 18 '15 at 22:31
  • @PadraicCunninghamI installed via homebrew. I detailed my exact process here: http://www.pyimagesearch.com/2015/04/27/installing-boost-and-boost-python-on-osx-with-homebrew/ – Adrian Rosebrock May 19 '15 at 23:13
  • @AdrianRosebrock not yet – Tim 333 May 19 '15 at 23:28
  • @AdrianRosebrock. what python was boost-python built with? – Padraic Cunningham May 19 '15 at 23:38
  • @PadraicCunningham boost-python was built with brew version of Python, as far as I can tell. Although I do "brew info boost-python" I am seeing that it looks like it was compiled with Python3 support but not Python support which is odd: ==> Options --c++11 Build using C++11 mode --universal Build a universal binary --with-python3 Build with python3 support --without-python Build without python support --HEAD Install HEAD version – Adrian Rosebrock May 20 '15 at 10:48
  • Never mind that last comment, I misunderstood the options switches. This is the formula boost-python was built with: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/boost-python.rb – Adrian Rosebrock May 20 '15 at 11:28
  • I was able to get it to work! Please see my answer to the question for the details. – Adrian Rosebrock May 20 '15 at 12:06
4

I finally got it to work! I'll be posting a detailed blog post about this later, but here's the gist for now. Basically, when I manually inspected the output of cmake, dlib was compiling and linking against the system version of Python not the Homebrew version of python.

If you're interested in the details, it appears that cmake was trying to compile and link against /usr/lib/libpython2.7.dylib. However, that is the system version of Python. It should be compiling a linking against /usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib which is the Homebrew version of Python.

I manually edited CMakeFiles/dlib_.dir/build.make and /CMakeFiles/dlib_.dir/link.txt to point to the Homebrew dylib file instead of the system one. I was able to compile dlib and then run it without a segfault. And furthermore, I was able to run the object detectors without a problem.

Adrian Rosebrock
  • 907
  • 2
  • 9
  • 18
1

You are compiling the C++ example programs. The python examples are in the python_examples folder. Also, each example has instructions at the top that tell you how to use it.

Davis King
  • 4,731
  • 1
  • 25
  • 26
  • Thanks. I subsequently found the python_examples instructions, brew installed boost-python, ran compile_dlib_python_module.bat, started Python and typed import dlib and Python segfaulted ("Segmentation fault: 11" Identifier: Python Version: 2.7.8 (2.7.8) Code Type: X86-64 (Native) Parent Process: bash [8652] Responsible: Terminal [184] Crashed Thread: 0 Dispatch queue: com.apple.main-thread) Any ideas? – Tim 333 May 05 '15 at 19:35
1

If you use Conda, it's much easier.

conda install -c menpo dlib

https://anaconda.org/menpo/dlib

mesutpiskin
  • 1,771
  • 2
  • 26
  • 30
0

for Python 3 support use:

python setup.py install --yes DPYTHON3
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Bogdan
  • 1
0

If you already compiled dlib source code from github by cmake command. Then you want to run python program to call dlib api.

You should compile dlib Python API as below,

sudo python setup.py install

Or

sudo python setup.py install --yes USE_AVX_INSTRUCTIONS

if you have a CPU that supports AVX instructions, since this makes some things run faster. Note that you need to have boost-python installed to compile the Python API.(Linked from dlib README.md)

It works for me.

Luna Kong
  • 3,065
  • 25
  • 20
0

I encounted the same error with you.

When I looked at the folder "C:\Anaconda3\Lib\site-packages", I found "dlib-19.8.0-py3.4-win-amd64.egg\", it means I have compiled dlib successfully, but there is no "dlib\" folder under the "C:\Anaconda3\Lib\site-packages", it is inside "dlib-19.8.0-py3.4-win-amd64.egg\" folder, so the solution is simple: just copy the "dlib\" folder to path "C:\Anaconda3\Lib\site-packages", the Python interpreter will find the module and import it successfully.

Hope it will help!

enter image description here

Moon Zoe
  • 53
  • 4