You would begin by importing dlib and scikit-image:
import dlib from skimage import ioThen 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.top(), d.right(), d.bottom()We can also view the results graphically by running:
win = dlib.image_window() win.set_image(img) win.add_overlay(faces)
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.