-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_dlib_train_object_detector.py
85 lines (71 loc) · 3.7 KB
/
main_dlib_train_object_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import pathlib
import glob
import dlib
options = dlib.simple_object_detector_training_options()
# The trainer is a kind of support vector machine and therefore has the usual
# SVM C parameter. In general, a bigger C encourages it to fit the training
# data better but might lead to overfitting. You must find the best C value
# 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
# Tell the code how many CPU cores your computer has for the fastest training.
options.num_threads = 4
options.be_verbose = True
training_xml_path = os.path.join("assets/dlib/train_person", "train.xml")
testing_xml_path = os.path.join("assets/dlib/test_person", "test.xml")
# 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
# dataset and also contains the positions of the face boxes. To create your
# own XML files you can use the imglab tool which can be found in the
# tools/imglab folder. It is a simple graphical tool for labeling objects in
# images with boxes. To see how to use it read the tools/imglab/README.txt
# file. But for this example, we just use the train.xml file included with
# dlib.
svm_output = "output/dlib/detector.svm"
pathlib.Path("output/dlib").mkdir(parents=True, exist_ok=True)
dlib.train_simple_object_detector(training_xml_path, svm_output, options)
print("") # Print blank line to create gap from previous output
print("Training accuracy: {}".format(
dlib.test_simple_object_detector(training_xml_path, svm_output)))
print("Testing accuracy: {}".format(
dlib.test_simple_object_detector(testing_xml_path, svm_output)))
# Now let's use the detector as you would in a normal application. First we
# will load it from disk.
detector = dlib.simple_object_detector(svm_output)
# We can look at the HOG filter we learned. It should look like a face. Neat!
win_det = dlib.image_window()
win_det.set_image(detector)
print("Showing detections on the images in the faces folder...")
win = dlib.image_window()
for f in glob.glob(os.path.join("assets/dlib/test_person", "*.jpg")):
print("Processing file: {}".format(f))
img = dlib.load_rgb_image(f)
dets = detector(img)
print("Number of person 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)
dlib.hit_enter_to_continue()
# Next, suppose you have trained multiple detectors and you want to run them
# efficiently as a group. You can do this as follows:
detector1 = dlib.fhog_object_detector(svm_output)
# In this example we load detector.svm again since it's the only one we have on
# hand. But in general it would be a different detector.
detector2 = dlib.fhog_object_detector(svm_output)
# make a list of all the detectors you want to run. Here we have 2, but you
# could have any number.
detectors = [detector1, detector2]
image = dlib.load_rgb_image("assets/dlib/test_person" + '/1.jpg')
[boxes, confidences, detector_idxs] = dlib.fhog_object_detector.run_multiple(detectors, image, upsample_num_times=1, adjust_threshold=0.0)
for i in range(len(boxes)):
print("detector {} found box {} with confidence {}.".format(detector_idxs[i], boxes[i], confidences[i]))
# We could save this detector to disk by uncommenting the following.
#detector2.save('detector2.svm')
# Now let's look at its HOG filter!
win_det.set_image(detector2)
dlib.hit_enter_to_continue()