Python dlib learning (6): training model

In the foreword, the
previous blogs all use the trained model provided by the official dlib for target recognition.
-python dlib learning (1): face detection
-python dlib learning (2): face feature point calibration
-python dlib learning (3): calling cnn face detection
-python dlib learning (4): single target tracking
-python dlib learning (5): Compare the face
directly into the subject, this time we have to train a model ourselves. Use dlib to train a HOG-based object recognizer.

The
first thing to prepare for a data set is the data set, here is a very convenient tool imglab.
This tool is provided in the official dlib source code, and you can download it if you want.
1. In the source code downloaded from github, the file path is: dlib / tools / imglab.
2. Here I provide another download link to extract this toolkit for download. http://download.csdn.net/download/hongbin_xu/10103900
(recommended to download from the official website)
This file has been downloaded by default.
Enter the directory and enter the following command:

mkdir build
cd build
cmake ..
cmake --build . --config Release
1
2
3
4

Compile it again:

sudo make install
1 After the
installation is complete, you can use the imglab command directly in the console.

Next, use imglab to label the data set.
I collected some pictures of cats online for training. The label is on the cat's face, so this is equivalent to identifying the cat's face (laughs). The data set needs to be collected in advance. The data set I use here will be attached at the end of the article. Please download it if you need it.

Use imglab to first create the xml file that we want to use to record the tags.
Enter the current directory:

imglab -c mydata.xml ./
1
mydata.xml: This is the name of the xml file, just pick one.
/: This is the directory of your data set, I created it directly in the data set folder, so Directly specify the current folder

Then two files will be generated in the folder: an xml file and an xsl file. As shown below:

Next, open this xml file:

imglab mydata.xml
1

The tool software will then start.

Let's label each picture one by one.
The operation is very simple. After pressing the shift key, the left mouse button will drag to draw a frame; first release the left button, the frame will be recorded, if the shift key is released first, the operation will not be recorded.

Finally, open your xml file, which has been marked with tag information:


Program and result
training model
#-*-coding: utf-8-*-
import os
import sys
import glob
import dlib
import cv2

# options is used to set training parameters and modes
options = dlib.simple_object_detector_training_options ()
# Since faces are left / right symmetric we can tell the trainer to train a
# symmetric detector. This helps it get the most value out of the training
# data .
options.add_left_right_image_flips = True
# Support vector machine C parameters, usually the default is 5. Change the parameters yourself to achieve the best effect
options.C = 5
# Number of threads, if your computer has 4 cores, fill in 4
options. num_threads = 4
options.be_verbose = True

# 获取路径
current_path = os.getcwd()
train_folder = current_path + '/cats_train/'
test_folder = current_path + '/cats_test/'
train_xml_path = train_folder + 'cat.xml'
test_xml_path = test_folder + 'cats.xml'

print("training file path:" + train_xml_path)
# print(train_xml_path)
print("testing file path:" + test_xml_path)
# print(test_xml_path)

# 开始训练
print("start training:")
dlib.train_simple_object_detector(train_xml_path, 'detector.svm', options)

print("") # Print blank line to create gap from previous output
print("Training accuracy: {}".format(
dlib.test_simple_object_detector(train_xml_path, "detector.svm")))

print ("Testing accuracy: {}". format (
dlib.test_simple_object_detector (test_xml_path, "detector.svm")))
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
Run the program:
start training

Then wait for a little while, the data set I used is not large, just run on my notebook not long after.

It can be seen that the accuracy rate is almost 70%. The model is saved in detector.svm.

Test model
#-*-coding: utf-8-*-

import os
import sys
import dlib
import cv2
import glob

detector = dlib.simple_object_detector("detector.svm")

current_path = os.getcwd()
test_folder = current_path + '/cats_test/'

for f in glob.glob(test_folder+'*.jpg'):
print("Processing file: {}".format(f))
img = cv2.imread(f, cv2.IMREAD_COLOR)
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])
dets = detector(img2)
print("Number of faces detected: {}".format(len(dets)))
for index, face in enumerate(dets):
print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))

left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)
cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)
cv2.imshow(f, img)

k = cv2.waitKey (0)
cv2.destroyAllWindows ()
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 The
code is very simple, Not to repeat.

Recognition results:

 

Due to factors such as lighting, angle, etc., the training results still need to be improved. The data set I put is very small, and increasing the data set can improve this problem to some extent.

Tucao:
Shouldn't this be called "cat" face recognition?
( ̄_ ̄)

The download link of the dataset I used:
http://download.csdn.net/download/hongbin_xu/10103946

original link: https://blog.csdn.net/hongbin_xu/article/details/78443289

Guess you like

Origin www.cnblogs.com/Ph-one/p/12759153.html