如何使用 dlib 进行 Python 导入(使用 cmake 和 osx)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30042174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:52:55  来源:igfitidea点击:

How to get Python import working with dlib (using cmake and osx)

pythoncmakedlib

提问by Tim 333

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!".

对不起,如果这是基本的,但我正在尝试安装 dlib 以与 python 一起使用,如(http://blog.dlib.net/2014/04/dlib-187-released-make-your-own-object.html)“用 Python 制作你自己的物体检测器!”。

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

我下载了安装文件,解压缩并按照安装说明使用了 cmake ( 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.

然而,在 Python 中输入“import dlib”只会给出 ImportError: No module named dlib。

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

知道我如何告诉 Python 如何找到/使用这个东西吗?

回答by Davis King

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.

您正在编译 C++ 示例程序。python 示例位于 python_examples 文件夹中。此外,每个示例的顶部都有说明,告诉您如何使用它。

回答by Padraic Cunningham

On ubuntu I had to do the following:

在 ubuntu 上,我必须执行以下操作:

sudo apt-get install libboost-python-dev cmake

cd to dlib-18.15/python_examples

光盘到 dlib-18.15/python_examples

Then:

然后:

./compile_dlib_python_module.bat 

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

然后我将其复制dlib.so到 dist-packages 以便它在我的路径中。

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

According to the documentation compile_dlib_python_module.batwill work on any os once you have both CMakeand boost-pythoninstalled.

根据该文件compile_dlib_python_module.bat可以在任何操作系统上的工作,一旦你有两个CMakeboost-python安装。

回答by Adrian Rosebrock

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 notthe Homebrew version of python.

我终于让它工作了!稍后我将发布一篇关于此的详细博客文章,但这是现在的要点。基本上,当我手动检查 cmake 的输出时,dlib 正在编译和链接 Python 的系统版本,而不是Python的 Homebrew 版本。

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.dylibwhich is the Homebrew version of Python.

如果您对细节感兴趣,似乎 cmake 正在尝试编译和链接/usr/lib/libpython2.7.dylib. 但是,这是 Python 的系统版本。它应该编译一个链接,/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib它是 Python 的 Homebrew 版本。

I manually edited CMakeFiles/dlib_.dir/build.makeand /CMakeFiles/dlib_.dir/link.txtto 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.

我手动编辑CMakeFiles/dlib_.dir/build.make/CMakeFiles/dlib_.dir/link.txt指向 Homebrew dylib 文件而不是系统文件。我能够编译 dlib,然后在没有段错误的情况下运行它。此外,我能够毫无问题地运行物体检测器。

回答by Maciek Str?mich

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

相反,手动档的编辑,你可以传递-DPYTHON_LIBRARY:FILEPATH=/path/to/your/libpython2.7.dylibcmake

What ./compile_dlib_python_module.batdoes is

什么./compile_dlib_python_module.bat

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

回答by liezlp

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

仅供参考,最简单的安装方法是现在使用:

sudo python setup.py install

回答by Bogdan

for Python 3 support use:

对于 Python 3 支持使用:

python setup.py install --yes DPYTHON3

回答by mesutpiskin

If you use Conda, it's much easier.

如果你使用 Conda,那就简单多了。

conda install -c menpo dlib

https://anaconda.org/menpo/dlib

https://anaconda.org/menpo/dlib

回答by Ankur Jain

Dlib installation instructions for OSX (python3)

OSX (python3) 的 Dlib 安装说明

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

Installing default dlib

安装默认的 dlib

pip3 install dlib

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

以下给出了一些加速安装启用 AVX、SSE2、SSE4 指令的 dlib(从 dlib.net 下载源代码并从目录中执行)。

python setup.py install --yes USE_AVX_INSTRUCTIONS --yes USE_SSE2_INSTRUCTIONS --yes USE_SSE4_INSTRUCTIONS

回答by Luna Kong

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

如果您已经通过 cmake 命令从 github编译了dlib源代码。然后你想运行python程序来调用dlib api。

You should compile dlib Python API as below,

你应该编译 dlib Python API,如下所示,

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 dlibREADME.md)

如果您有一个支持 AVX 指令的 CPU,因为这会使某些事情运行得更快。请注意,您需要安装 boost-python 才能编译 Python API。(链接自dlibREADME.md)

It works for me.

这个对我有用。

回答by Moon Zoe

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.

当我看着的文件夹"C:\Anaconda3\Lib\site-packages",我发现"dlib-19.8.0-py3.4-win-amd64.egg\",这意味着我已经成功地编译DLIB,但没有"dlib\"在该文件夹"C:\Anaconda3\Lib\site-packages",它里面"dlib-19.8.0-py3.4-win-amd64.egg\"的文件夹,因此该解决方案很简单:刚才复制的"dlib\"文件夹路径"C:\Anaconda3\Lib\site-packages",Python解释器会发现模块并成功导入。

Hope it will help!

希望它会有所帮助!

enter image description here

在此处输入图片说明