基于Dlib库的人脸检测模型训练(更新中)

基于Dlib库的猫脸检测模型训练

官方参考链接:https://github.com/davisking/dlib/blob/master/python_examples/train_object_detector.py

准备数据集:

训练:

import os
import dlib


# 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 = 5
# 线程数,你电脑有4核的话就填4
options.num_threads = 4
options.be_verbose = True
options.epsilon = 0.1

# 获取路径
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. 训练器超参数设置:dlib.simple_object_detector_training_options():

1,C:svm的C参数,默认为1;
2,add_left_right_image_flips:#bool型,训练图像是否左右翻转,默认False;
3,be_verbose:#bool型,是否在屏幕上输出训练过程中的日志信息,默认False;
4,detection_window_size:#滑动窗口的大小,默认80*80;
5,epsilo: #训练终止误差,默认0.01;
6,num_threads:#训练的线程数目,默认4

2.训练检测器核心函数: dlib.train_simple_object_detector():

1,第一个参数dataset_filename为训练数据路径
2,第二个参数detector_output_filename为训练完成的检测器保存路径,
3,第三个参数为训练的超参数设置,是一个dlib.simple_object_detector_training_options对象。

测试:

# -*- coding: utf-8 -*-

import os
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()

文件夹目录格式:

自己看

  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值