Error to run a library to python

2020-02-15 07:49发布

问题:

I follow the steps according to http://npatta01.github.io/2015/08/10/dlib/ but when I try to run (I use sudo),

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

take back error. Firstly, the error was

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

to line 8. Now, the error is Illegal instruction (core dumped) but I don't know why. Please, help me to add the library correctly.

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]))

回答1:

As I see in your code:

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

First line works and the second does not. This means that dlib is installed, but it is compiled with no GUI support

In dlib's source code we see that if macro DLIB_NO_GUI_SUPPORT is defined - there will be no "image_window" function in dlib module. This macro is defined automatically if CMake scripts can't find X11 libraries

You need to ensure that dlib is compiled with GUI support. To make it, first - install libx11-dev into your system if you are working on Linux, or XQuartz for Mac

When building dlib with running python setup.py install --yes DLIB_JPEG_SUPPORT - check its messages. If there are errors or warnings - fix them



回答2:

I am answering this question because I ran into the same issue by doing

conda install -c conda-forge dlib

and

pip install dlib

I tried searched and got several helpful links and below one link was saved my day. So listing down the details here too..

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

It will be better to compile the latest code from Github than installing it from conda / pip. This is to ensure that dlib is compiled with GUI support.

Install dependencies

sudo apt-get update

Install Boost

sudo apt-get install libboost-all-dev

Install other dependencies ( May be most of them will already have installed in your system)

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

Build the latestst code of dlib from Github. Assumptions: - Ubuntu 16.04 or higher - don't have an nVidia GPU and don't have Cuda and cuDNN installed and don't want GPU acceleration

Clone the code from github:

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

Build the main dlib library:

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

Build and install the Python extensions:

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

make sure are pointing to the right python( if you have anaconda installed on top of Ubuntu's vanilla python then you should install the package pointing anaconda).

if you are still facing a gcc error like below

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

then make sure you are installing the below python package

conda install libgcc

At this point, you should be able to run python and type import dlib successfully.