数据准备
是从猫脸关键点检测大赛下载的猫的数据集,训练集和测试集各上万张图片,不光含有图片,还有标记好的猫脸关键点的数据(用于关键点检测),我们先用图片自行打标记自行识别。
数据集百度网盘下载地址
链接:https://pan.baidu.com/s/1CjaAXx1yqse6Xf6sNG4Tkg
提取码:098k
图片标记
使用imglab进行标记,可参考https://blog.csdn.net/bashendixie5/article/details/110338937
训练集我标记了216张,测试集标记了41张。
训练代码
调整了多次C参数,不知道是因为图片数量的问题还是什么问题,C参数从5、10、15、20、25、30,准确率递增,由于没有使用gpu版本的dlib,有些慢,所以没有再增加。30的时候识别率大概再70%。猜测如果调整图片数量、质量、训练/测试数量比,应该能提高上来。
# 猫脸检测
import os
import sys
import glob
import dlib
import cv2
# options用于设置训练的参数和模式
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
# 支持向量机的C参数,通常默认取为5.自己适当更改参数以达到最好的效果
options.C = 30
# 线程数,你电脑有4核的话就填4
options.num_threads = 8
options.be_verbose = True
# 获取路径
# train_folder = 'E:/dlib-master/tools/imglab/build/Release/'
# test_folder = current_path + '/cats_test/'
train_xml_path = 'E:/dlib-master/tools/imglab/build/Release/mydataset.xml'
test_xml_path = 'E:/dlib-master/tools/imglab/build/Release/testdataset.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")))
测试代码
import os
import sys
import dlib
import cv2
import glob
detector = dlib.simple_object_detector("detector.svm")
#current_path = os.getcwd()
test_folder = 'E:/dlib-master/tools/imglab/build/Release/addimages/'
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()
测试结果,测试了19张图片,找到了8张,还可以。