dlib库实现人脸的关键点检测-pycharm+opencv(python语言)

本文介绍如何使用dlib库结合pycharm和opencv,在Python环境下实现人脸关键点的检测,包括68个和81个关键点的精确定位。通过安装必要的软件包和数据集,文中提供了一段示例代码,演示了从图像读取到关键点检测及可视化的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实现效果
能够基于不同的数据集实现68个关键点检测和81个关键点检测。
68个关键点检测效果如下:
在这里插入图片描述
81个关键点检测的效果如下:

在这里插入图片描述
二、安装环境配置
(1). Anaconda+pycharm
(2). 在pycharm中下载如下库:
(pycharm安装第三方库的方法参考:
https://blog.csdn.net/weixin_43978384/article/details/120204053

  1. python-opencv2
    负责图像处理以及绘制图形等功能;
  2. cmake
    安装dlib库之前需要安装cmake;
    3.dlib
    负责识别人脸的关键点;

三、需要下载并使用训练的数据集
(1)、 68个关键点检测的数据集
链接:
https://pan.baidu.com/s/1ILxsaUAGRvzGnjnUsCfThA
提取码:abc7
(2)、 81个关键点检测的数据集
链接:
https://pan.baidu.com/s/1woyabe3B5KVoZzOohWkhjg
提取码:abc7

四、代码实现

import cv2
import dlib
import time
import numpy as np

filepath='D:/Users/13279/Desktop/dlib_faceDetection/'   #the picture`s path
filepath1='D:/Users/13279/Desktop/dlib_faceDetection/68/'   #the 68 points detectors path
filepath2='D:/Users/13279/Desktop/dlib_faceDetection/shape_predictor_81_face_landmarks-master/'  #the 81 points detectors path

img=cv2.imread(filepath+"detection.JPG")
height,width,channels=img.shape
rescale=0.5
grayImg=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#get face detectors
faceDector=dlib.get_frontal_face_detector()
#get landmark detectors
#landmarkPredcitor=dlib.shape_predictor(filepath1+"shape_predictor_68_face_landmarks.dat")
landmarkPredcitor=dlib.shape_predictor(filepath2+"shape_predictor_81_face_landmarks.dat")

faces=faceDector(grayImg,1)
time_start=time.time()
for face in faces:
    #get 68 landmarks for each face
    landmarks=landmarkPredcitor(img,face)
    print(len(landmarks.parts()))
    #go through all the points and print them
    for pt in landmarks.parts():
        pt_pos=(pt.x,pt.y)
        cv2.circle(img,pt_pos,2,(0,255,0),1)

time_end=time.time()
print("It takes {} to detect with dlib model!".format(time_end-time_start))
img=cv2.resize(img,(int(width*rescale),int(height*rescale)))

cv2.imshow("image",img)

cv2.waitKey(0)
cv2.destroyAllWindows()


这篇文章永久免费,对所有用户开放,csdn不要再给我篡改用户权限了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值