将库运行到python时出错

时间:2016-09-27 16:08:54

标签: python attributeerror dlib illegal-instruction

我按照http://npatta01.github.io/2015/08/10/dlib/的步骤进行操作,但是当我尝试运行时(我使用sudo),

python python_examples/face_detector.py examples/faces/2007_007763.jpg

收回错误。 首先,错误是

AttributeError: 'module' object has no attribute 'image_window' 

到第8行。 现在,错误是Illegal instruction (core dumped),但我不知道为什么。 请帮我正确添加库。

import sys

import dlib
from skimage import io


detector = dlib.get_frontal_face_detector()
win = dlib.image_window()

for f in sys.argv[1:]:
    print("Processing file: {}".format(f))
    img = io.imread(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time.  This will make everything bigger and allow us to detect more
    # faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for i, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            i, d.left(), d.top(), d.right(), d.bottom()))

    win.clear_overlay()
    win.set_image(img)
    win.add_overlay(dets)
    dlib.hit_enter_to_continue()


# Finally, if you really want to you can ask the detector to tell you the score
# for each detection.  The score is bigger for more confident detections.
# The third argument to run is an optional adjustment to the detection threshold,
# where a negative value will return more detections and a positive value fewer.
# Also, the idx tells you which of the face sub-detectors matched.  This can be
# used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0):
    img = io.imread(sys.argv[1])
    dets, scores, idx = detector.run(img, 1, -1)
    for i, d in enumerate(dets):
        print("Detection {}, score: {}, face_type:{}".format(
            d, scores[i], idx[i]))

2 个答案:

答案 0 :(得分:0)

正如我在您的代码中看到的那样:

detector = dlib.get_frontal_face_detector()
win = dlib.image_window()

第一行有效,第二行没有。这意味着已安装dlib,但它是在没有GUI支持的情况下编译的

In dlib's source code我们看到如果定义了宏DLIB_NO_GUI_SUPPORT - dlib模块中将没有“image_window”函数。如果CMake脚本找不到X11库

,则会自动定义此宏

您需要确保使用GUI支持编译dlib。要做到这一点,首先 - 如果您正在使用Linux,请将libx11-dev安装到您的系统中,或者将XQuartz for Mac安装到您的系统中

在运行python setup.py install --yes DLIB_JPEG_SUPPORT构建dlib时 - 检查其消息。如果有错误或警告 - 修复它们

答案 1 :(得分:0)

我正在回答这个问题,因为我通过

遇到了同样的问题
conda install -c conda-forge dlib

pip install dlib

我尝试搜索并获得了几个有用的链接,下面一个链接已保存了一天。所以在这里列出详细信息..

https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf

从Github编译最新代码比从conda / pip安装更好。这是为了确保在GUI支持下编译dlib。

安装依赖项

sudo apt-get update

安装Boost

sudo apt-get install libboost-all-dev

安装其他依赖项(可能大多数已安装在您的系统中)

apt-get install -y --fix-missing     build-essential     cmake     gfortran     git     wget     curl     graphicsmagick     libgraphicsmagick1-dev     libatlas-dev     libavcodec-dev     libavformat-dev     libboost-all-dev     libgtk2.0-dev     libjpeg-dev     liblapack-dev     libswscale-dev     pkg-config     python3-dev     python3-numpy     software-properties-common zip

apt-get clean

从Github构建最新的dlib代码。     假设:          - Ubuntu 16.04或更高版本          - 没有nVidia GPU,没有安装Cuda和cuDNN,也不想要GPU加速

从github克隆代码:

git clone https://github.com/davisking/dlib.git

构建主dlib库:

cd dlib
    mkdir build; cd build; cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1; cmake --build .

构建并安装Python扩展:

cd ..
    python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

确保指向正确的python(如果你在Ubuntu的vanilla python上安装了anaconda,那么你应该安装指向anaconda的包。)

如果您仍然面临如下所示的gcc错误

lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found

然后确保安装下面的python包

conda install libgcc

此时,您应该能够运行python并成功输入import dlib。