• 基于Dlib训练自已的人脸数据集提高人脸识别的准确率


    前言

    由于图像的质量、光线、角度等因素影响。这时如果使用官方提供的模型做人脸识别,就会导至识别率不是很理想。人脸识别的准确率与图像的清晰度和质量有关。如果图像模糊、光线不足或者有其他干扰因素,Dlib 可能无法正确地识别人脸。为了确保图像质量良好,可以使用更清晰的图像、改善光照条件或使用图像增强技术来提高图像质量。但这些并不是本篇章要讲述的内容。那么除去图像质量和光线不足等因素,如何解决准确率的问题呢?答案就是需要自已收集人脸并进行训练自已的识别模型。

    模型训练

    要使用Dlib训练自己的人脸数据集,可以按照以下步骤进行:

    1. 数据收集:收集一组包含人脸的图像,并对每个人脸进行标记。可以使用Dlib提供的标记工具来手动标记每个人脸的位置。

    2. 数据准备:将数据集划分为训练集和测试集。确保训练集和测试集中的图像具有不同的人脸,并且每个人脸都有相应的标记。

    3. 特征提取:使用Dlib提供的人脸特征提取器,如dlib.get_frontal_face_detector()和dlib.shape_predictor(),对每个图像进行人脸检测和关键点定位。可以使用这些关键点来提取人脸特征。

    4. 特征向量生成:对于每个人脸,使用关键点和人脸图像来生成一个唯一的特征向量。可以使用Dlib的face_recognition模块中的face_encodings()函数来生成特征向量。

    5. 训练分类器:使用生成的特征向量和相应的标签来训练分类器。可以使用Dlib的svm_c_trainer()或者其他分类器进行训练。确保使用训练集进行训练,并使用测试集进行验证。

    6. 评估准确率:使用测试集对训练好的分类器进行评估,计算准确率、召回率等指标来评估人脸识别的性能。

    以下是一个简单的例子,展示了如何使用Dlib训练自己的人脸数据集:

    导入必要的库

    1. import dlib
    2. import os
    3. import numpy as np
    4. from sklearn import svm

    定义数据集路径和模型路径

    1. dataset_path = "path_to_dataset"
    2. model_path = "path_to_save_model"

    加载人脸检测器和关键点定位器

    1. detector = dlib.get_frontal_face_detector()
    2. predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    收集数据集中的图像和标签

    1. images = []
    2. labels = []
    3. # 遍历数据集目录
    4. for person_name in os.listdir(dataset_path):
    5. person_path = os.path.join(dataset_path, person_name)
    6. if os.path.isdir(person_path):
    7. # 遍历每个人的图像
    8. for image_name in os.listdir(person_path):
    9. image_path = os.path.join(person_path, image_name)
    10. # 加载图像
    11. img = dlib.load_rgb_image(image_path)
    12. # 人脸检测和关键点定位
    13. dets = detector(img)
    14. for det in dets:
    15. shape = predictor(img, det)
    16. # 生成特征向量
    17. face_descriptor = np.array(face_recognition.face_encodings(img, [shape])[0])
    18. # 添加到训练集
    19. images.append(face_descriptor)
    20. labels.append(person_name)
    21. # 转换为numpy数组
    22. images = np.array(images)
    23. labels = np.array(labels)

    设置训练分类器

    1. # 训练分类器
    2. classifier = svm.SVC(kernel='linear', probability=True)
    3. classifier.fit(images, labels)

    保存模型

    dlib.save_linear_kernel(model_path, classifier)
    

    完整代码

    1. import dlib
    2. import os
    3. import numpy as np
    4. from sklearn import svm
    5. dataset_path = "path_to_dataset"
    6. model_path = "path_to_save_model"
    7. detector = dlib.get_frontal_face_detector()
    8. predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    9. images = []
    10. labels = []
    11. # 遍历数据集目录
    12. for person_name in os.listdir(dataset_path):
    13. person_path = os.path.join(dataset_path, person_name)
    14. if os.path.isdir(person_path):
    15. # 遍历每个人的图像
    16. for image_name in os.listdir(person_path):
    17. image_path = os.path.join(person_path, image_name)
    18. # 加载图像
    19. img = dlib.load_rgb_image(image_path)
    20. # 人脸检测和关键点定位
    21. dets = detector(img)
    22. for det in dets:
    23. shape = predictor(img, det)
    24. # 生成特征向量
    25. face_descriptor = np.array(face_recognition.face_encodings(img, [shape])[0])
    26. # 添加到训练集
    27. images.append(face_descriptor)
    28. labels.append(person_name)
    29. # 转换为numpy数组
    30. images = np.array(images)
    31. labels = np.array(labels)
    32. # 训练分类器
    33. classifier = svm.SVC(kernel='linear', probability=True)
    34. classifier.fit(images, labels)
    35. images = []
    36. labels = []
    37. # 遍历数据集目录
    38. for person_name in os.listdir(dataset_path):
    39. person_path = os.path.join(dataset_path, person_name)
    40. if os.path.isdir(person_path):
    41. # 遍历每个人的图像
    42. for image_name in os.listdir(person_path):
    43. image_path = os.path.join(person_path, image_name)
    44. # 加载图像
    45. img = dlib.load_rgb_image(image_path)
    46. # 人脸检测和关键点定位
    47. dets = detector(img)
    48. for det in dets:
    49. shape = predictor(img, det)
    50. # 生成特征向量
    51. face_descriptor = np.array(face_recognition.face_encodings(img, [shape])[0])
    52. # 添加到训练集
    53. images.append(face_descriptor)
    54. labels.append(person_name)
    55. # 转换为numpy数组
    56. images = np.array(images)
    57. labels = np.array(labels)
    58. # 训练分类器
    59. classifier = svm.SVC(kernel='linear', probability=True)
    60. classifier.fit(images, labels)
    61. images = []
    62. labels = []
    63. # 遍历数据集目录
    64. for person_name in os.listdir(dataset_path):
    65. person_path = os.path.join(dataset_path, person_name)
    66. if os.path.isdir(person_path):
    67. # 遍历每个人的图像
    68. for image_name in os.listdir(person_path):
    69. image_path = os.path.join(person_path, image_name)
    70. # 加载图像
    71. img = dlib.load_rgb_image(image_path)
    72. # 人脸检测和关键点定位
    73. dets = detector(img)
    74. for det in dets:
    75. shape = predictor(img, det)
    76. # 生成特征向量
    77. face_descriptor = np.array(face_recognition.face_encodings(img, [shape])[0])
    78. # 添加到训练集
    79. images.append(face_descriptor)
    80. labels.append(person_name)
    81. # 转换为numpy数组
    82. images = np.array(images)
    83. labels = np.array(labels)
    84. # 训练分类器
    85. classifier = svm.SVC(kernel='linear', probability=True)
    86. classifier.fit(images, labels)
    87. #保存模型
    88. dlib.save_linear_kernel(model_path, classifier)

    除了使用SVM分类器,你还可以使用其他分类器进行人脸识别模型的训练。以下是一些常见的分类器:

    1. 决策树分类器(Decision Tree Classifier):基于树结构的分类器,可以通过一系列的决策来对样本进行分类。

    2. 随机森林分类器(Random Forest Classifier):由多个决策树组成的集成学习模型,通过投票或平均预测结果来进行分类。

    3. K最近邻分类器(K-Nearest Neighbors Classifier):根据样本之间的距离来进行分类,将未知样本分类为其最近的K个邻居中最常见的类别。

    4. 朴素贝叶斯分类器(Naive Bayes Classifier):基于贝叶斯定理的概率分类器,假设特征之间相互独立,通过计算后验概率进行分类。

    5. 神经网络分类器(Neural Network Classifier):由多层神经元组成的模型,通过反向传播算法进行训练,可以用于复杂的分类任务。

    这些分类器都有各自的优缺点和适用场景,你可以根据你的数据集和需求选择合适的分类器进行训练。

  • 相关阅读:
    Linux下最新版MySQL 8.0的下载与安装(详细步骤)
    【STM32训练—WiFi模块】第一篇、STM32驱动ESP8266WiFi模块获取网络时间
    [从零学习汇编语言] - 内中断
    IO流高级流
    k8s-namespace-基本概念
    【组件专题】初识COM组件
    数量关系 --- 方程
    力扣-2512.奖励最顶尖的k名学生
    在docker容器中 运行docker命令no such file
    [南京大学2022操作系统-P11] 操作系统上的进程 (最小 Linux; fork, execve 和 exit)
  • 原文地址:https://blog.csdn.net/qq_39312146/article/details/133419940