前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用 dlib 计算人脸68个关键点

用 dlib 计算人脸68个关键点

作者头像
用户6021899
发布2022-11-18 14:01:38
1.3K0
发布2022-11-18 14:01:38
举报

dlib 是较流行的人脸识别的开源库,使用c++编写,里面包含了许多的机器学习算法,。其官网链接是 dlib C++ Library - Machine Learning

dlib 也提供了python 接口,可通过pip 在线安装dlib,前提是要先安装VisualStudio, 还需先安装python库 cmake 和 boost。

dlib 提供了用于计算人脸68个关键点模型文件shape_predictor_68_face_landmarks.dat (需要单独下载)。68个点的位置如下:

代码语言:javascript
复制
import dlib
import cv2

# 与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
detector = dlib.get_frontal_face_detector()

# 使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor(r'C:\Python39\Lib\site-packages\dlib\shape_predictor_68_face_landmarks.dat')
# cv2读取图片
img = cv2.imread("K:/image (46).png")
if img is None:
    print("the image file does not exist")

# 与人脸检测程序相同,使用detector进行人脸检测 dets为返回的结果
dets = detector(img, 1)
# 使用enumerate 函数遍历序列中的元素以及它们的下标
# 下标k即为人脸序号
# left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
# top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for k, d in enumerate(dets):
    print("dets{}".format(d))
    print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        k, d.left(), d.top(), d.right(), d.bottom()))

    # 使用predictor进行人脸关键点识别 shape为返回的结果
    shape = predictor(img, d)
    # 获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
    print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))

    # 绘制特征点
    for index, pt in enumerate(shape.parts()):
        print('Part {}: {}'.format(index, pt))
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 1, (255, 0, 0), 2)
        #利用cv2.putText输出1-68
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(index+1),pt_pos,font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)

cv2.imshow('img', img)
k = cv2.waitKey()
cv2.destroyAllWindows()

示例图片:

还可以可以同一图片几张脸一起检测:

检测图片的代码稍做改动就可以检测摄像头实时视频:

代码语言:javascript
复制
import dlib
import cv2
import numpy as np

# 与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
detector = dlib.get_frontal_face_detector()
# 使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor(r'C:\Python39\Lib\site-packages\dlib\shape_predictor_68_face_landmarks.dat')

font = cv2.FONT_HERSHEY_SIMPLEX

def faceDetect(img):
    global fdetector, predictor, font
    dets = detector(img, 1)
    # 使用enumerate 函数遍历序列中的元素以及它们的下标
    # k即为人脸序号
    # left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
    # top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
    for k, d in enumerate(dets):
        # print(f"dets{d}")
        # print(f"Detection {k}: Left: {d.left()} Top: {d.top()} Right: {d.right()} Bottom: {d.bottom()}")
        # 使用predictor进行人脸关键点识别 shape为返回的结果
        shape = predictor(img, d)
        # 获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
        #print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
        # 绘制特征点
        for index, pt in enumerate(shape.parts()):
            # print(f'Part {index}: {pt}')
            pt_pos = (pt.x, pt.y)
            cv2.circle(img, pt_pos, 1, (255, 0, 0), 2)
            #利用cv2.putText输出1-68
            # cv2.putText(img, str(index+1),pt_pos,font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)
    return img[:, : :-1, :] #mirrorred
    # return img
cameraCapture = cv2.VideoCapture(0)
print(cameraCapture)
fps = 60
size = int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
#print(size)
videoWriter = cv2.VideoWriter("myvid.avi", cv2.VideoWriter_fourcc('X','V','I','D'), fps, size)
success, frame = cameraCapture.read()
numFramesRemaining = 1000* fps -1
while success and numFramesRemaining > 0:
    img = faceDetect(frame)
    videoWriter.write(img)
    cv2.imshow("my video", img)
    cv2.waitKey(1) #延迟
    success, frame = cameraCapture.read()
    numFramesRemaining -= 1
cameraCapture.release()
cv2.waitKey()
cv2.destroyAllWindows()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-08-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档