Wednesday, April 9, 2014

Dlib 18.7 released: Make your own object detector in Python!

A while ago I boasted about how dlib's object detection tools are better than OpenCV's. However, one thing OpenCV had on dlib was a nice Python API, but no longer!  The new version of dlib is out and it includes a Python API for using and creating object detectors. What does this API look like? Well, lets start by imagining you want to detect faces in this image:

You would begin by importing dlib and scikit-image:
import dlib
from skimage import io
Then you load dlib's default face detector, the image of Obama, and then invoke the detector on the image:
detector = dlib.get_frontal_face_detector()
img = io.imread('obama.jpg')
faces = detector(img)
The result is an array of boxes called faces. Each box gives the pixel coordinates that bound each detected face. To get these coordinates out of faces you do something like:
for d in faces:
    print "left,top,right,bottom:", d.left(),, d.right(), d.bottom()
We can also view the results graphically by running:
win = dlib.image_window()

But what if you wanted to create your own object detector?  That's easy too.  Dlib comes with an example program and a sample training dataset showing how to this.  But to summarize, you do:
options = dlib.simple_object_detector_training_options()
options.C = 5  # Set the SVM C parameter to 5.  
dlib.train_simple_object_detector("training.xml","detector.svm", options)
That will run the trainer and save the learned detector to a file called detector.svm. The training data is read from training.xml which contains a list of images and bounding boxes. The example that comes with dlib shows the format of the XML file. There is also a graphical tool included that lets you mark up images with a mouse and save these XML files. Finally, to load your custom detector you do:
detector = dlib.simple_object_detector("detector.svm")
If you want to try it out yourself you can download the new dlib release here.


Manuel said...

This library looks amazing!

How can I go and install it? I cannot seem to find a good tutorial.

Davis King said...

The comment at the top of each python example tells you what to do to compile the library.

Tester said...

Hello and thanks for your work.

I tried to train a detector with the .py file you provided. It works well on about 10 images (each about 2000x2000, jpg), but it fails with "Memory Error" on more than 10 images.
Sorry if the solution to this problem is obvious.

OS: Windows 7 64bit (using 32bit Python 2.7)

Tester said...

Oh guess I forget an actual question: do you know why exactly this error occours and how I can prevent it while still training on more images? My goal is to train on some hundreds of images each of the same size.

manas dalal said...

I used the imglab exe to make the file with the boxes. while running the code to build the svm file on certain occasions it fails somewhere so i checked i changed the width and the height to random value it worked but that will increase the chances of misclassifications. How is it the bounding boxes are affecting this process of training?

Davis King said...

What happens when it fails? Is there an error message?

manas dalal said...

Hi Davis,

Theres absolutely no error message the last check point is when it counts the no of images and then the crash

manas dalal said...

so is there a certain aspect ratio to maintained while drawing the bounding box over the object? because certain occasions the default window size 80 x 80 does not seem to work unless changed to 50 x 50. What features should be common? similar height, width , aspect ratio , area etc..

Davis King said...

There is no error message at all? What happens? The program terminates and nothing is output to disk or the screen?

You should try to make all your boxes have a similar aspect ratio.

manas dalal said...

There is absolutely no message on the screen just crashes . i think most of the boxes are made to maintain the aspect ratio. I can share the xml with you if you wish to analyse it?

Davis King said...

Sure, if you can post a complete program that demonstrates the error you are seeing that would be great.

Anon Anon said...

How do I save the image to a file? I don't have a GUI.