最前面的话

感谢弦弦子的一位粉丝

说明

记得第三关需要选择更换代码文件!我盯着数据集看了好久都不知道要干什么…

注意:本博客仅供参考!

第一关:检测人脸特征点

任务描述

本关任务:

1.理解人脸特征点含义;

2.了解人脸特征点检测基本原理;

3.使用Dlib人脸特征点模型,获取人脸特征点。

编程要求

请在右侧编辑器中的BEGIN-END之间编写代码,使用Dlib检测人脸特征点并打印:

·导入OpenCV和Dlib库;

·读取指定image_path图像;

·将图片转化为灰度图;

·使用正向人脸检测器检测并获取人脸;

·使用训练好的能检测68个人脸特征点的模型,检测特征点;

·打印出对应的特征点(打印函数已经默认写好,无需修改)。

测试说明

补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。

代码

import cv2
import dlib

# 读取图片
img_path = "step1/image/face.jpg"
img = cv2.imread(img_path)
# 转换为灰阶图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 正向人脸检测器
detector = dlib.get_frontal_face_detector()
# 使用训练完成的68个特征点模型
predictor_path = "data/shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)

# 使用检测器来检测图像中的人脸
faces = detector(gray, 1)

print("人脸数: ", len(faces))

for i, face in enumerate(faces):
    print("第", i+1, "个人脸的矩形框坐标:\n","left:", face.left(), "right:", face.right(), "top:", face.top(), "bottom:", face.bottom())
    # 获取人脸特征点
    shape = predictor(img, face)
    print("第", i+1, '个人脸的特征点:')
    print(shape.parts())

第二关:绘制人脸特征点

任务描述

本关任务:使用OpenCV绘制识别出的人脸特征点。

编程要求

请在右侧编辑器中的BEGIN-END之间编写代码,使用OpenCV绘制人脸特征点,并保存图片到指定路径:

·圆心半径为 1;

·颜色设定为绿色(0,255,0);

·粗细为 2。

测试说明

补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。

代码

import cv2
import dlib

# 读取图片
img_path = "step2/image/face.jpg"
img = cv2.imread(img_path)

# 转换为灰阶图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 正向人脸检测器
detector = dlib.get_frontal_face_detector()

# 使用训练完成的68个特征点模型
predictor_path = "data/shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)

# 使用检测器来检测图像中的人脸
faces = detector(gray, 1)
for i, face in enumerate(faces):
    # 获取人脸特征点
    shape = predictor(img, face)

    '''****************BEGIN****************'''
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 1, (0, 255, 0), 2)
    '''**************** END ****************'''
    cv2.imwrite('step2/out/face.jpg', img)

第三关:训练人脸特征点模型

任务描述

本关任务:使用Dlib,自己动手训练一个人脸特征点识别模型。

编程要求

恭喜你学习完如何训练自己的人脸特征点检测器,请在右侧编辑器中的BEGIN-END之间编写代码,完成一个人脸检测器,并应用到新的图片上:

·定义模型训练需要的参数, 其中:oversampling_amount 参数设置为100,正则化nu设置为0.05,数的深度 tree_depth 设置为1,be_verbose属性设置为假(False);

·调用训练模型函数;

·调用测试模型函数;

·使用刚才训练完成的人脸特征点检测器进行检查;

·获取每一个人脸区域内的特征点,使用OpenCV绘制特征点, 点的颜色设定为蓝色(0, 0, 255),粗细为3;

·保存图片到指定路径(代码中已经写好路径)。

测试说明

补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。

代码

import os
import sys
import cv2
import dlib
import glob

# 数据集路径
faces_folder = 'step3/dataSet'

'''****************BEGIN****************'''
# 1. 定义模型训练需要的参数
options = dlib.shape_predictor_training_options()
'''**************** END ****************'''

'''****************BEGIN****************'''
# 2.参数设定
# 通过对训练样本进行随机变形扩大样本数目
options.oversampling_amount = 100
# 通过增加正则化(将nu调小)和使用更小深度的树来降低模型的容量
options.nu = 0.05
options.tree_depth = 1
options.be_verbose = False
'''**************** END ****************'''

# 3. 调用测试模型函数
# 训练集路径
training_xml_path = os.path.join(
    faces_folder, "training_with_face_landmarks.xml")
'''****************BEGIN****************'''
dlib.train_shape_predictor(training_xml_path, "predictor.dat", options)
'''**************** END ****************'''

print('模型训练完成')

# 4. 使用人脸特征点检测器模型

# 人脸区域检测器
detector = dlib.get_frontal_face_detector()
# 从本地导入人脸特征点检测器
'''****************BEGIN****************'''
predictor = dlib.shape_predictor("predictor.dat")
'''**************** END ****************'''

# 检测人脸以及人脸特征点
faces_folder = 'step3/image/'

for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
    print("处理文件: {}".format(f))

    # 加载图片
    img = dlib.load_rgb_image(f)
    # 检测图片
    dets = detector(img, 1)

    print("检测到的人脸个数为: {}".format(len(dets)))

    # 遍历图片中识别出的每一个人脸
    for k, d in enumerate(dets):
        # 打印人脸区域位置
        print("人脸区域 {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))

        # 获取每一个人脸区域内的特征点
        shape = predictor(img, d)

        # 遍历所有点,绘制特征点
        for pt in shape.parts():
            pt_pos = (pt.x, pt.y)
            '''****************BEGIN****************'''
            cv2.circle(img, pt_pos, 2, (0, 0, 255), 3)
            '''**************** END ****************'''

        # 图片保存路径
        save_path = 'step3/out/face-landmark.jpg'
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        '''****************BEGIN****************'''
        # 保存图片
        cv2.imwrite(save_path, img)
        '''**************** END ****************'''
        if os.path.exists(save_path):
            print('保存检测后图片成功')