博客
关于我
基于Dilb库 训练自己的68点人脸特征点检测
阅读量:114 次
发布时间:2019-02-26

本文共 2443 字,大约阅读时间需要 8 分钟。

基于 Dlib 库训练 68 点人脸特征点检测

准备训练集

要使用 Dlib 库训练自己的 68 点人脸特征点检测模型,首先需要一个标注工具。Dlib 提供了 imglab 工具,可以帮助我们快速完成标注工作。以下是使用 imglab 工具的步骤指南:

  • 安装 imglab 工具:在 Windows 环境下,确保使用 cmd 而不是 PowerShell 进行安装和配置。
  • 使用 imglab 工具进行标注:通过 imglab 工具对训练集中的每张面部图像进行标注,确保每个面部关键点的坐标准确无误。
  • 训练代码

    以下是基于 Dlib 库进行训练的完整代码示例:

    import osimport dlib# 定义当前工作目录current_path = os.getcwd()# 设置训练数据的路径faces_path = os.path.join(current_path, "examples/faces")# 初始化训练参数options = dlib.shape_predictor_training_options()options.oversampling_amount = 300  # 增加训练样本量options.nu = 0.05  # 调整正则项系数以防止过拟合options.tree_depth = 2  # 设置树的深度options.be_verbose = True  # 启用训练信息日志# 加载训练数据training_xml_path = os.path.join(faces_path, "training_with_face_landmarks.xml")# 开始训练模型dlib.train_shape_predictor(training_xml_path, "predictor.dat", options)# 输出训练结果print("Training accuracy: {}".format(dlib.test_shape_predictor(training_xml_path, "predictor.dat")))

    参数说明

    以下是 dlib.train_shape_predictor 函数的核心参数及其作用:

  • _cascade_depth:级联网络的层数,默认为 10 层。
  • _tree_depth:回归树的深度,叶子节点数量为 2^tree_depth
  • _num_trees_per_cascade_level:每级联中的回归树数量,默认为 500。
  • _nu:正则项系数,范围 (0, 1],默认为 0.1。
  • _oversampling_amount:通过随机变形扩大训练样本量,建议设置较大值以提高准确率。
  • _feature_pool_size:特征池的大小,范围 >1,值越大训练复杂度越高。
  • _lambda:控制回归树分裂节点的选择,值越小越倾向于选择近邻像素。
  • _num_test_splits:回归树节点分裂时随机生成的测试样本数量,值越大准确率越高。
  • _feature_pool_region_padding:特征池周围的扩展范围,值为 0 表示仅在关键点附近采样。
  • 测试代码

    以下是测试训练模型的代码示例:

    import osimport cv2import dlib# 定义当前工作目录current_path = os.getcwd()# 设置训练数据的路径faces_path = os.path.join(current_path, "examples/faces")# 加载训练好的模型文件predictor_path = os.path.join(faces_path, "predictor.dat")# 初始化人脸检测器和预测器detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor(predictor_path)# 遍历训练集中的所有图片for f in glob.glob(os.path.join(faces_path, "*.jpg")):    print("Processing file: {}".format(f))    img = cv2.imread(f)    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)        # 检测面部关键点    dets = detector(img_gray, 1)    print("Number of faces detected: {}".format(len(dets)))        for face in dets:        # 画出面部框        cv2.rectangle(img, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 3)                # 预测面部关键点        shape = predictor(img, face)                # 绘制面部关键点        for pt in shape.parts():            cv2.circle(img, (pt.x, pt.y), 2, (255, 0, 0), 1)

    注意事项

  • 数据标注:确保训练数据的标注准确,尤其是面部关键点的坐标。
  • 超参数调整:根据实际需求调整训练参数,如 _oversampling_amount_nu,以平衡准确率和过拟合风险。
  • 硬件优化:训练过程可能耗时较长,建议在有足够硬件性能的环境下运行。
  • 通过以上步骤和代码示例,你可以轻松实现基于 Dlib 库的 68 点人脸特征点检测模型训练和测试。

    转载地址:http://sgcu.baihongyu.com/

    你可能感兴趣的文章
    Mysql--逻辑架构
    查看>>
    MySql-2019-4-21-复习
    查看>>
    mysql-5.6.17-win32免安装版配置
    查看>>
    mysql-5.7.18安装
    查看>>
    MySQL-Buffer的应用
    查看>>
    mysql-cluster 安装篇(1)---简介
    查看>>
    mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
    查看>>
    mysql-connector-java各种版本下载地址
    查看>>
    mysql-EXPLAIN
    查看>>
    MySQL-Explain的详解
    查看>>
    mysql-group_concat
    查看>>
    MySQL-redo日志
    查看>>
    MySQL-【1】配置
    查看>>
    MySQL-【4】基本操作
    查看>>
    Mysql-丢失更新
    查看>>
    Mysql-事务阻塞
    查看>>
    Mysql-存储引擎
    查看>>
    mysql-开启慢查询&所有操作记录日志
    查看>>
    MySQL-数据目录
    查看>>
    MySQL-数据页的结构
    查看>>