Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
af82bc40
Commit
af82bc40
authored
Dec 11, 2014
by
Patrick Snape
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sort out PEP8 issues in the examples
parent
32ad0ffa
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
114 additions
and
114 deletions
+114
-114
LICENSE_FOR_EXAMPLE_PROGRAMS.txt
python_examples/LICENSE_FOR_EXAMPLE_PROGRAMS.txt
+0
-2
face_detector.py
python_examples/face_detector.py
+16
-12
max_cost_assignment.py
python_examples/max_cost_assignment.py
+29
-31
sequence_segmenter.py
python_examples/sequence_segmenter.py
+0
-0
svm_rank.py
python_examples/svm_rank.py
+21
-26
svm_struct.py
python_examples/svm_struct.py
+0
-0
train_object_detector.py
python_examples/train_object_detector.py
+48
-43
No files found.
python_examples/LICENSE_FOR_EXAMPLE_PROGRAMS.txt
View file @
af82bc40
...
...
@@ -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.
python_examples/face_detector.py
View file @
af82bc40
...
...
@@ -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"
)
python_examples/max_cost_assignment.py
View file @
af82bc40
#!/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
)))
python_examples/sequence_segmenter.py
View file @
af82bc40
This diff is collapsed.
Click to expand it.
python_examples/svm_rank.py
View file @
af82bc40
...
...
@@ -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
python_examples/svm_struct.py
View file @
af82bc40
This diff is collapsed.
Click to expand it.
python_examples/train_object_detector.py
View file @
af82bc40
#!/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
(
"
\n
training 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
(
"
\n
Showing 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"
)))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment