Search code examples
pythonperformanceobject-detectiondlibhistogram-of-oriented-gradients

Dlib's simple_object_detector() is running slowly


I have trained a SVM model for simple_object_detector(). But while inference on a video it is becoming too slow.

I went through a similar question: Why is dlib so slow finding an object? where an answer is saying to use USE_AVX_INSTRUCTIONS flag enabled while dlib installation. But it is not the case for me. As I found the flag is enabled by default. I also have come through this FAQ: Why is dlib slow where the solution is to choose the Release mode in Visual Studio but I am not using Visual Studio and just running the code from terminal.

But interesting thing is that if I run the builtin face detector dlib.get_frontal_face_detector() it runs completely fine with no lag. But the program only becomes slow while running simple_object_detector() trained on custom data.


Solution

  • Alright! I got the solution. Actually I missed to provide a flag while running the detector. Probably it is related to some optimization.

    First initialize the detector object:

    detector = dlib.simple_object_detector("/path/to/your/trained/SVM/detector")
    

    Now change the following line from this:

    detections = detector(gray_image)
    

    To this:

    detections = detector(gray_image, 0)
    

    Reference: https://github.com/davisking/dlib/issues/557#issuecomment-297679025