Commit af82bc40 authored by Patrick Snape's avatar Patrick Snape
Browse files

Sort out PEP8 issues in the examples

parent 32ad0ffa
......@@ -14,9 +14,7 @@ letter to
San Francisco, California, 94105, USA.
Public domain dedications are not recognized by some countries. So
if you live in an area where the above dedication isn't valid then
you can consider the example programs to be licensed under the Boost
Software License.
......@@ -7,7 +7,8 @@
# face.
#
# The examples/faces folder contains some jpg images of people. You can run
# this program on them and see the detections by executing the following command:
# this program on them and see the detections by executing the
# following command:
# ./face_detector.py ../examples/faces/*.jpg
#
# This face detector is made using the now classic Histogram of Oriented
......@@ -20,14 +21,17 @@
#
#
# COMPILING THE DLIB PYTHON INTERFACE
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# you are using another python version or operating system then you need to
# compile the dlib python interface before you can use this file. To do this,
# run compile_dlib_python_module.bat. This should work on any operating system
# so long as you have CMake and boost-python installed. On Ubuntu, this can be
# done easily by running the command: sudo apt-get install libboost-python-dev cmake
# run compile_dlib_python_module.bat. This should work on any operating
# system so long as you have CMake and boost-python installed.
# On Ubuntu, this can be done easily by running the command:
# sudo apt-get install libboost-python-dev cmake
import dlib, sys
import sys
import dlib
from skimage import io
......@@ -35,18 +39,18 @@ detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
for f in sys.argv[1:]:
print("processing file: ", f)
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: ", len(dets))
for d in dets:
print(" detection position left,top,right,bottom:", d.left(), d.top(), d.right(), d.bottom())
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
raw_input("Hit enter to continue")
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#
# This simple example shows how to call dlib's optimal linear assignment problem solver.
# It is an implementation of the famous Hungarian algorithm and is quite fast, operating in
# O(N^3) time.
# This simple example shows how to call dlib's optimal linear assignment
# problem solver.
# It is an implementation of the famous Hungarian algorithm and is quite fast,
# operating in O(N^3) time.
#
# COMPILING THE DLIB PYTHON INTERFACE
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# you are using another python version or operating system then you need to
# compile the dlib python interface before you can use this file. To do this,
# run compile_dlib_python_module.bat. This should work on any operating system
# so long as you have CMake and boost-python installed. On Ubuntu, this can be
# done easily by running the command: sudo apt-get install libboost-python-dev cmake
# run compile_dlib_python_module.bat. This should work on any operating
# system so long as you have CMake and boost-python installed.
# On Ubuntu, this can be done easily by running the command:
# sudo apt-get install libboost-python-dev cmake
import dlib
# Let's imagine you need to assign N people to N jobs. Additionally, each person will make
# your company a certain amount of money at each job, but each person has different skills
# so they are better at some jobs and worse at others. You would like to find the best way
# to assign people to these jobs. In particular, you would like to maximize the amount of
# money the group makes as a whole. This is an example of an assignment problem and is
# what is solved by the dlib.max_cost_assignment() routine.
# So in this example, let's imagine we have 3 people and 3 jobs. We represent the amount of
# money each person will produce at each job with a cost matrix. Each row corresponds to a
# person and each column corresponds to a job. So for example, below we are saying that
# person 0 will make $1 at job 0, $2 at job 1, and $6 at job 2.
# Let's imagine you need to assign N people to N jobs. Additionally, each
# person will make your company a certain amount of money at each job, but each
# person has different skills so they are better at some jobs and worse at
# others. You would like to find the best way to assign people to these jobs.
# In particular, you would like to maximize the amount of money the group makes
# as a whole. This is an example of an assignment problem and is what is solved
# by the dlib.max_cost_assignment() routine.
# So in this example, let's imagine we have 3 people and 3 jobs. We represent
# the amount of money each person will produce at each job with a cost matrix.
# Each row corresponds to a person and each column corresponds to a job. So for
# example, below we are saying that person 0 will make $1 at job 0, $2 at job 1,
# and $6 at job 2.
cost = dlib.matrix([[1, 2, 6],
[5, 3, 6],
[4, 5, 0]])
# To find out the best assignment of people to jobs we just need to call this function.
# To find out the best assignment of people to jobs we just need to call this
# function.
assignment = dlib.max_cost_assignment(cost)
# This prints optimal assignments: [2, 0, 1]
# which indicates that we should assign the person from the first row of the cost matrix to
# job 2, the middle row person to job 0, and the bottom row person to job 1.
print("optimal assignments: ", assignment)
# which indicates that we should assign the person from the first row of the
# cost matrix to job 2, the middle row person to job 0, and the bottom row
# person to job 1.
print("Optimal assignments: {}".format(assignment))
# This prints optimal cost: 16.0
# which is correct since our optimal assignment is 6+5+5.
print("optimal cost: ", dlib.assignment_cost(cost, assignment))
print("Optimal cost: {}".format(dlib.assignment_cost(cost, assignment)))
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#
# This example shows how to use dlib to learn to do sequence segmentation. In a sequence
# segmentation task we are given a sequence of objects (e.g. words in a sentence) and we
# are supposed to detect certain subsequences (e.g. the names of people). Therefore, in
# the code below we create some very simple training sequences and use them to learn a
# sequence segmentation model. In particular, our sequences will be sentences represented
# as arrays of words and our task will be to learn to identify person names. Once we have
# our segmentation model we can use it to find names in new sentences, as we will show.
# This example shows how to use dlib to learn to do sequence segmentation. In
# a sequence segmentation task we are given a sequence of objects (e.g. words in
# a sentence) and we are supposed to detect certain subsequences (e.g. the names
# of people). Therefore, in the code below we create some very simple training
# sequences and use them to learn a sequence segmentation model. In particular,
# our sequences will be sentences represented as arrays of words and our task
# will be to learn to identify person names. Once we have our segmentation
# model we can use it to find names in new sentences, as we will show.
#
# COMPILING THE DLIB PYTHON INTERFACE
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# you are using another python version or operating system then you need to
# compile the dlib python interface before you can use this file. To do this,
# run compile_dlib_python_module.bat. This should work on any operating system
# so long as you have CMake and boost-python installed. On Ubuntu, this can be
# done easily by running the command: sudo apt-get install libboost-python-dev cmake
import dlib
# run compile_dlib_python_module.bat. This should work on any operating
# system so long as you have CMake and boost-python installed.
# On Ubuntu, this can be done easily by running the command:
# sudo apt-get install libboost-python-dev cmake
import sys
import dlib
# The sequence segmentation models we work with in this example are chain structured
# conditional random field style models. Therefore, central to a sequence segmentation
# model is some method for converting the elements of a sequence into feature vectors.
# That is, while you might start out representing your sequence as an array of strings, the
# dlib interface works in terms of arrays of feature vectors. Each feature vector should
# capture important information about its corresponding element in the original raw
# sequence. So in this example, since we work with sequences of words and want to identify
# names, we will create feature vectors that tell us if the word is capitalized or not. In
# our simple data, this will be enough to identify names. Therefore, we define
# sentence_to_vectors() which takes a sentence represented as a string and converts it into
# an array of words and then associates a feature vector with each word.
# The sequence segmentation models we work with in this example are chain
# structured conditional random field style models. Therefore, central to a
# sequence segmentation model is some method for converting the elements of a
# sequence into feature vectors. That is, while you might start out representing
# your sequence as an array of strings, the dlib interface works in terms of
# arrays of feature vectors. Each feature vector should capture important
# information about its corresponding element in the original raw sequence. So
# in this example, since we work with sequences of words and want to identify
# names, we will create feature vectors that tell us if the word is capitalized
# or not. In our simple data, this will be enough to identify names.
# Therefore, we define sentence_to_vectors() which takes a sentence represented
# as a string and converts it into an array of words and then associates a
# feature vector with each word.
def sentence_to_vectors(sentence):
# Create an empty array of vectors
vects = dlib.vectors()
for word in sentence.split():
# Our vectors are very simple 1-dimensional vectors. The value of the single
# feature is 1 if the first letter of the word is capitalized and 0 otherwise.
if (word[0].isupper()):
# Our vectors are very simple 1-dimensional vectors. The value of the
# single feature is 1 if the first letter of the word is capitalized and
# 0 otherwise.
if word[0].isupper():
vects.append(dlib.vector([1]))
else:
vects.append(dlib.vector([0]))
return vects
# Dlib also supports the use of a sparse vector representation. This is more efficient
# than the above form when you have very high dimensional vectors that are mostly full of
# zeros. In dlib, each sparse vector is represented as an array of pair objects. Each
# pair contains an index and value. Any index not listed in the vector is implicitly
# associated with a value of zero. Additionally, when using sparse vectors with
# dlib.train_sequence_segmenter() you can use "unsorted" sparse vectors. This means you
# can add the index/value pairs into your sparse vectors in any order you want and don't
# need to worry about them being in sorted order.
# Dlib also supports the use of a sparse vector representation. This is more
# efficient than the above form when you have very high dimensional vectors that
# are mostly full of zeros. In dlib, each sparse vector is represented as an
# array of pair objects. Each pair contains an index and value. Any index not
# listed in the vector is implicitly associated with a value of zero.
# Additionally, when using sparse vectors with dlib.train_sequence_segmenter()
# you can use "unsorted" sparse vectors. This means you can add the index/value
# pairs into your sparse vectors in any order you want and don't need to worry
# about them being in sorted order.
def sentence_to_sparse_vectors(sentence):
vects = dlib.sparse_vectors()
vects = dlib.sparse_vectors()
has_cap = dlib.sparse_vector()
no_cap = dlib.sparse_vector()
no_cap = dlib.sparse_vector()
# make has_cap equivalent to dlib.vector([1])
has_cap.append(dlib.pair(0,1))
# Since we didn't add anything to no_cap it is equivalent to dlib.vector([0])
has_cap.append(dlib.pair(0, 1))
# Since we didn't add anything to no_cap it is equivalent to
# dlib.vector([0])
for word in sentence.split():
if (word[0].isupper()):
if word[0].isupper():
vects.append(has_cap)
else:
vects.append(no_cap)
......@@ -77,57 +83,50 @@ def print_segment(sentence, names):
sys.stdout.write("\n")
# Now let's make some training data. Each example is a sentence as well as a set of ranges
# which indicate the locations of any names.
names = dlib.ranges() # make an array of dlib.range objects.
segments = dlib.rangess() # make an array of arrays of dlib.range objects.
sentences = []
sentences.append("The other day I saw a man named Jim Smith")
# We want to detect person names. So we note that the name is located within the
# range [8, 10). Note that we use half open ranges to identify segments. So in
# this case, the segment identifies the string "Jim Smith".
# Now let's make some training data. Each example is a sentence as well as a
# set of ranges which indicate the locations of any names.
names = dlib.ranges() # make an array of dlib.range objects.
segments = dlib.rangess() # make an array of arrays of dlib.range objects.
sentences = ["The other day I saw a man named Jim Smith",
"Davis King is the main author of the dlib Library",
"Bob Jones is a name and so is George Clinton",
"My dog is named Bob Barker",
"ABC is an acronym but John James Smith is a name",
"No names in this sentence at all"]
# We want to detect person names. So we note that the name is located within
# the range [8, 10). Note that we use half open ranges to identify segments.
# So in this case, the segment identifies the string "Jim Smith".
names.append(dlib.range(8, 10))
segments.append(names)
names.clear() # make names empty for use again below
# make names empty for use again below
names.clear()
sentences.append("Davis King is the main author of the dlib Library")
names.append(dlib.range(0, 2))
segments.append(names)
names.clear()
sentences.append("Bob Jones is a name and so is George Clinton")
names.append(dlib.range(0, 2))
names.append(dlib.range(8, 10))
segments.append(names)
names.clear()
sentences.append("My dog is named Bob Barker")
names.append(dlib.range(4, 6))
segments.append(names)
names.clear()
sentences.append("ABC is an acronym but John James Smith is a name")
names.append(dlib.range(5, 8))
segments.append(names)
names.clear()
sentences.append("No names in this sentence at all")
segments.append(names)
names.clear()
# Now before we can pass these training sentences to the dlib tools we need to convert them
# into arrays of vectors as discussed above. We can use either a sparse or dense
# representation depending on our needs. In this example, we show how to do it both ways.
use_sparse_vects = False
# Now before we can pass these training sentences to the dlib tools we need to
# convert them into arrays of vectors as discussed above. We can use either a
# sparse or dense representation depending on our needs. In this example, we
# show how to do it both ways.
use_sparse_vects = False
if use_sparse_vects:
# Make an array of arrays of dlib.sparse_vector objects.
training_sequences = dlib.sparse_vectorss()
......@@ -139,46 +138,49 @@ else:
for s in sentences:
training_sequences.append(sentence_to_vectors(s))
# Now that we have a simple training set we can train a sequence segmenter. However, the
# sequence segmentation trainer has some optional parameters we can set. These parameters
# determine properties of the segmentation model we will learn. See the dlib documentation
# for the sequence_segmenter object for a full discussion of their meanings.
# Now that we have a simple training set we can train a sequence segmenter.
# However, the sequence segmentation trainer has some optional parameters we can
# set. These parameters determine properties of the segmentation model we will
# learn. See the dlib documentation for the sequence_segmenter object for a
# full discussion of their meanings.
params = dlib.segmenter_params()
params.window_size = 3
params.use_high_order_features = True
params.use_high_order_features = True
params.use_BIO_model = True
# This is the common SVM C parameter. Larger values encourage the trainer to attempt to
# fit the data exactly but might overfit. In general, you determine this parameter by
# cross-validation.
# This is the common SVM C parameter. Larger values encourage the trainer to
# attempt to fit the data exactly but might overfit. In general, you determine
# this parameter by cross-validation.
params.C = 10
# Train a model. The model object is responsible for predicting the locations of names in
# new sentences.
# Train a model. The model object is responsible for predicting the locations
# of names in new sentences.
model = dlib.train_sequence_segmenter(training_sequences, segments, params)
# Let's print out the things the model thinks are names. The output is a set of ranges
# which are predicted to contain names. If you run this example program you will see that
# it gets them all correct.
for i in range(len(sentences)):
print_segment(sentences[i], model(training_sequences[i]))
# Let's also try segmenting a new sentence. This will print out "Bob Bucket". Note that we
# need to remember to use the same vector representation as we used during training.
test_sentence = "There once was a man from Nantucket whose name rhymed with Bob Bucket"
# Let's print out the things the model thinks are names. The output is a set
# of ranges which are predicted to contain names. If you run this example
# program you will see that it gets them all correct.
for i, s in enumerate(sentences):
print_segment(s, model(training_sequences[i]))
# Let's also try segmenting a new sentence. This will print out "Bob Bucket".
# Note that we need to remember to use the same vector representation as we used
# during training.
test_sentence = "There once was a man from Nantucket " \
"whose name rhymed with Bob Bucket"
if use_sparse_vects:
print_segment(test_sentence, model(sentence_to_sparse_vectors(test_sentence)))
print_segment(test_sentence,
model(sentence_to_sparse_vectors(test_sentence)))
else:
print_segment(test_sentence, model(sentence_to_vectors(test_sentence)))
# We can also measure the accuracy of a model relative to some labeled data. This
# statement prints the precision, recall, and F1-score of the model relative to the data in
# training_sequences/segments.
print("Test on training data:", dlib.test_sequence_segmenter(model, training_sequences, segments))
# We can also do 5-fold cross-validation and print the resulting precision, recall, and F1-score.
print("cross validation:", dlib.cross_validate_sequence_segmenter(training_sequences, segments, 5, params))
# We can also measure the accuracy of a model relative to some labeled data.
# This statement prints the precision, recall, and F1-score of the model
# relative to the data in training_sequences/segments.
print("Test on training data: {}".format(
dlib.test_sequence_segmenter(model, training_sequences, segments)))
# We can also do 5-fold cross-validation and print the resulting precision,
# recall, and F1-score.
print("Cross validation: {}".format(
dlib.cross_validate_sequence_segmenter(training_sequences, segments, 5,
params)))
......@@ -14,23 +14,21 @@
# come to the top of the ranked list.
#
# COMPILING THE DLIB PYTHON INTERFACE
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# you are using another python version or operating system then you need to
# compile the dlib python interface before you can use this file. To do this,
# run compile_dlib_python_module.bat. This should work on any operating system
# so long as you have CMake and boost-python installed. On Ubuntu, this can be
# done easily by running the command: sudo apt-get install libboost-python-dev cmake
# run compile_dlib_python_module.bat. This should work on any operating
# system so long as you have CMake and boost-python installed.
# On Ubuntu, this can be done easily by running the command:
# sudo apt-get install libboost-python-dev cmake
import dlib
# Now let's make some testing data. To make it really simple, let's suppose that
# we are ranking 2D vectors and that vectors with positive values in the first
# dimension should rank higher than other vectors. So what we do is make
# Now let's make some testing data. To make it really simple, let's suppose
# that we are ranking 2D vectors and that vectors with positive values in the
# first dimension should rank higher than other vectors. So what we do is make
# examples of relevant (i.e. high ranking) and non-relevant (i.e. low ranking)
# vectors and store them into a ranking_pair object like so:
data = dlib.ranking_pair()
# Here we add two examples. In real applications, you would want lots of
# examples of relevant and non-relevant vectors.
......@@ -53,8 +51,10 @@ rank = trainer.train(data)
# Now if you call rank on a vector it will output a ranking score. In
# particular, the ranking score for relevant vectors should be larger than the
# score for non-relevant vectors.
print("ranking score for a relevant vector: ", rank(data.relevant[0]))
print("ranking score for a non-relevant vector: ", rank(data.nonrelevant[0]))
print("Ranking score for a relevant vector: {}".format(
rank(data.relevant[0])))
print("Ranking score for a non-relevant vector: {}".format(
rank(data.nonrelevant[0])))
# The output is the following:
# ranking score for a relevant vector: 0.5
# ranking score for a non-relevant vector: -0.5
......@@ -70,14 +70,11 @@ print(dlib.test_ranking_function(rank, data))
# The ranking scores are computed by taking the dot product between a learned
# weight vector and a data vector. If you want to see the learned weight vector
# you can display it like so:
print("weights: \n", rank.weights)
print("Weights: {}".format(rank.weights))
# In this case the weights are:
# 0.5
# -0.5
# In the above example, our data contains just two sets of objects. The
# relevant set and non-relevant set. The trainer is attempting to find a
# ranking function that gives every relevant vector a higher score than every
......@@ -94,7 +91,6 @@ print("weights: \n", rank.weights)
# to the trainer. Therefore, each ranking_pair would represent the
# relevant/non-relevant sets for a particular query. An example is shown below
# (for simplicity, we reuse our data from above to make 4 identical "queries").
queries = dlib.ranking_pairs()
queries.append(data)
queries.append(data)
......@@ -104,7 +100,6 @@ queries.append(data)
# We can train just as before.
rank = trainer.train(queries)
# Now that we have multiple ranking_pair instances, we can also use
# cross_validate_ranking_trainer(). This performs cross-validation by splitting
# the queries up into folds. That is, it lets the trainer train on a subset of
......@@ -112,9 +107,8 @@ rank = trainer.train(queries)
# splits and returns the overall ranking accuracy based on the held out data.
# Just like test_ranking_function(), it reports both the ordering accuracy and
# mean average precision.
print("cross validation results: ", dlib.cross_validate_ranking_trainer(trainer, queries, 4))
print("Cross validation results: {}".format(
dlib.cross_validate_ranking_trainer(trainer, queries, 4)))
# Finally, note that the ranking tools also support the use of sparse vectors in
# addition to dense vectors (which we used above). So if we wanted to do
......@@ -131,19 +125,20 @@ samp = dlib.sparse_vector()
# increasing order and no index value shows up more than once. If necessary,
# you can use the dlib.make_sparse_vector() routine to make a sparse vector
# object properly sorted and contain unique indices.
samp.append(dlib.pair(0,1))
samp.append(dlib.pair(0, 1))
data.relevant.append(samp)
# Now make samp represent the same vector as dlib.vector([0, 1])
samp.clear()
samp.append(dlib.pair(1,1))
samp.append(dlib.pair(1, 1))
data.nonrelevant.append(samp)
trainer = dlib.svm_rank_trainer_sparse()
rank = trainer.train(data)
print("ranking score for a relevant vector: ", rank(data.relevant[0]))
print("ranking score for a non-relevant vector: ", rank(data.nonrelevant[0]))
print("Ranking score for a relevant vector: {}".format(
rank(data.relevant[0])))
print("Ranking score for a non-relevant vector: {}".format(
rank(data.nonrelevant[0])))
# Just as before, the output is the following:
# ranking score for a relevant vector: 0.5
# ranking score for a non-relevant vector: -0.5
This diff is collapsed.
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example program shows how you can use dlib to make an object detector
# for things like faces, pedestrians, and any other semi-rigid object. In
# particular, we go though the steps to train the kind of sliding window
# object detector first published by Dalal and Triggs in 2005 in the paper
# Histograms of Oriented Gradients for Human Detection.
#
# This example program shows how you can use dlib to make an object
# detector for things like faces, pedestrians, and any other semi-rigid
# object. In particular, we go though the steps to train the kind of sliding
# window object detector first published by Dalal and Triggs in 2005 in the
# paper Histograms of Oriented Gradients for Human Detection.
#
# COMPILING THE DLIB PYTHON INTERFACE
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If
# you are using another python version or operating system then you need to
# compile the dlib python interface before you can use this file. To do this,
# run compile_dlib_python_module.bat. This should work on any operating system
# so long as you have CMake and boost-python installed. On Ubuntu, this can be
# done easily by running the command: sudo apt-get install libboost-python-dev cmake
import dlib, sys, glob
# run compile_dlib_python_module.bat. This should work on any operating
# system so long as you have CMake and boost-python installed.
# On Ubuntu, this can be done easily by running the command:
# sudo apt-get install libboost-python-dev cmake
import os
import sys
import glob
import dlib
from skimage import io
# In this example we are going to train a face detector based on the small
# faces dataset in the examples/faces directory. This means you need to supply
# the path to this faces folder as a command line argument so we will know
# where it is.
if (len(sys.argv) != 2):
print("Give the path to the examples/faces directory as the argument to this")
print("program. For example, if you are in the python_examples folder then ")
print("execute this program by running:")
print(" ./train_object_detector.py ../examples/faces")
if len(sys.argv) != 2:
print(
"Give the path to the examples/faces directory as the argument to this "
"program. For example, if you are in the python_examples folder then "
"execute this program by running:\n"
" ./train_object_detector.py ../examples/faces")
exit()
faces_folder = sys.argv[1]
# Now let's do the training. The train_simple_object_detector() function has a
# bunch of options, all of which come with reasonable default values. The next
# few lines goes over some of these options.
......@@ -46,10 +50,10 @@ options.add_left_right_image_flips = True
# empirically by checking how well the trained detector works on a test set of
# images you haven't trained on. Don't just leave the value set at 5. Try a
# few different C values and see what works best for your data.
options.C = 5
options.C = 5
# Tell the code how many CPU cores your computer has for the fastest training.
options.num_threads = 4
options.be_verbose = True
options.be_verbose = True
# This function does the actual training. It will save the final detector to
# detector.svm. The input is an XML file that lists the images in the training
......@@ -59,20 +63,22 @@ options.be_verbose = True
# images with boxes. To see how to use it read the tools/imglab/README.txt
# file. But for this example, we just use the training.xml file included with
# dlib.
dlib.train_simple_object_detector(faces_folder+"/training.xml", "detector.svm", options)
training_xml_path = os.path.join(faces_folder, "training.xml")
testing_xml_path = os.path.join(faces_folder, "testing.xml")
dlib.train_simple_object_detector(training_xml_path, "detector.svm", options)
# Now that we have a face detector we can test it. The first statement tests
# it on the training data. It will print(the precision, recall, and then)
# average precision.
print("\ntraining accuracy: {}".format(dlib.test_simple_object_detector(faces_folder+"/training.xml", "detector.svm")))
print("") # Print blank line to create gap from previous output
print("Training accuracy: {}".format(
dlib.test_simple_object_detector(training_xml_path, "detector.svm")))
# However, to get an idea if it really worked without overfitting we need to
# run it on images it wasn't trained on. The next line does this. Happily, we
# see that the object detector works perfectly on the testing images.
print("testing accuracy: {}".format(dlib.test_simple_object_detector(faces_folder+"/testing.xml", "detector.svm")))
print("Testing accuracy: {}".format(
dlib.test_simple_object_detector(testing_xml_path, "detector.svm")))
# Now let's use the detector as you would in a normal application. First we
# will load it from disk.
......@@ -84,39 +90,37 @@ win_det.set_image(detector)
# Now let's run the detector over the images in the faces folder and display the
# results.
print("\nShowing detections on the images in the faces folder...")
print("Showing detections on the images in the faces folder...")
win = dlib.image_window()
for f in glob.glob(faces_folder+"/*.jpg"):
print("processing file:", f)
for f in glob.glob(faces_folder + "/*.jpg"):
print("Processing file: {}".format(f))
img = io.imread(f)
dets = detector(img)
print("number of faces detected:", len(dets))
for d in dets:
print(" detection position left,top,right,bottom:", d.left(), d.top(), d.right(), d.bottom())
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
raw_input("Hit enter to continue")
# Finally, note that you don't have to use the XML based input to
# train_simple_object_detector(). If you have already loaded your training
# images and bounding boxes for the objects then you can call it as shown
# below.
# You just need to put your images into a list.
images = [io.imread(faces_folder + '/2008_002506.jpg'), io.imread(faces_folder + '/2009_004587.jpg') ]
images = [io.imread(faces_folder + '/2008_002506.jpg'),
io.imread(faces_folder + '/2009_004587.jpg')]
# Then for each image you make a list of rectangles which give the pixel
# locations of the edges of the boxes.
boxes_img1 = ([dlib.rectangle(left=329, top=78, right=437, bottom=186),
dlib.rectangle(left=224, top=95, right=314, bottom=185),
dlib.rectangle(left=125, top=65, right=214, bottom=155) ] )
boxes_img2 = ([dlib.rectangle(left=154, top=46, right=228, bottom=121 ),
dlib.rectangle(left=266, top=280, right=328, bottom=342) ] )
boxes_img1 = ([dlib.rectangle(left=329, top=78, right=437, bottom=186),
dlib.rectangle(left=224, top=95, right=314, bottom=185),
dlib.rectangle(left=125, top=65, right=214, bottom=155)])
boxes_img2 = ([dlib.rectangle(left=154, top=46, right=228, bottom=121),
dlib.rectangle(left=266, top=280, right=328, bottom=342)])
# And then you aggregate those lists of boxes into one big list and then call
# train_simple_object_detector().
boxes = [boxes_img1, boxes_img2]
......@@ -132,4 +136,5 @@ raw_input("Hit enter to continue")
# test_simple_object_detector(). If you have already loaded your training
# images and bounding boxes for the objects then you can call it as shown
# below.
print("Training accuracy: {}".format(dlib.test_simple_object_detector(images, boxes, "detector.svm")))
print("Training accuracy: {}".format(
dlib.test_simple_object_detector(images, boxes, "detector.svm")))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment