dlib实现脸部分割与人脸对齐

本文介绍了如何利用dlib库进行人脸检测、68个关键点检测,接着详细讲述了矩形框和不规则形状的人脸分割方法,并探讨了基于关键点的人脸对齐技术。通过示例代码展示了整个流程,包括从检测到的矩形框到最终的人脸对齐结果。
摘要由CSDN通过智能技术生成

参考资料

  1. 【Dlib】人脸检测、特征点检测、人脸对齐、人脸识别
  2. 深度学习与人脸识别之-脸部分割与校正
  3. github项目FaceSegmentation
  4. http://dlib.net/face_alignment.py.html
  5. 知乎问答

关键点检测

首先获取模型,下载地址在,我使用的是获取脸部68个关键点的模型shape_predictor_68_face_landmarks.dat
68关键点位置示意图如下:

首先贴出python代码

"""
代码功能:
1. 用dlib人脸检测器检测出人脸,返回的人脸矩形框
2. 对检测出的人脸进行关键点检测并用圈进行标记
3. 将检测出的人脸关键点信息写到txt文本中
"""
import cv2
import dlib
import numpy as np


predictor_model = 'shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()# dlib人脸检测器
predictor = dlib.shape_predictor(predictor_model)

# cv2读取图像
test_img_path = "input/Messi.jpg"
output_pos_info = "output_pos_info/Messi.txt"
img = cv2.imread(test_img_path)
file_handle = open(output_pos_info, 'a')
# 取灰度
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# 人脸数rects(rectangles)
rects = detector(img_gray, 0)


for i in range(len(rects)):
    landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])
    for idx, point in enumerate(landmarks):
        # 68点的坐标
        pos = (point[0, 0], point[0, 1])
        print(idx+1, pos)
        pos_info = str(point[0, 0]) + ' ' + str(point[0, 1]) + '\n'
        file_handle.write(pos_info)
        # 利用cv2.circle给每个特征点画一个圈,共68个
        cv2.circle(img, pos, 3, color=(0, 255, 0))
        # 利用cv2.putText输出1-68
        #font = cv2.FONT_HERSHEY_SIMPLEX
        #cv2.putText(img, str(idx+1), pos, font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)

file_handle.close()
cv2.imwrite("output/Messi_keypoints.png", img)

大致过程如下:先用人脸检测器获取到人脸矩形框rectangles,再用68点shape模型获取full_object_detection对象。最后将关键点标记出来,

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值