基于Python,dlib实现人脸检测

dilb 在做人脸检测人脸识别这块,也是用到比较多的。face_recognition就是基于dlib实现的。
这篇文章将使用Python和dlib实现人脸检测

配置环境

Python 3
Dlib 下载地址 选择相应的版本 使用命令(例如)

pip install dlib-19.8.1-cp36-cp36m-win_amd64.whl

训练模型 训练模型用于是人脸识别的关键,用于查找图片的关键点。下载地址
下载文件:shape_predictor_68_face_landmarks.dat.bz2

代码实现

#coding=utf-8
#图片检测 - Dlib版本
import cv2
import dlib
import time
t=time.time()
path = "../../face_photos/yiqi.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
    "../shape_predictor_68_face_landmarks.dat"
)
dets = detector(gray, 1)
for face in dets:
    #在图片中标注人脸,并显示
    left = face.left()
    top = face.top()
    right = face.right()
    bottom = face.bottom()
    cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
    cv2.imshow("image", img)
print('所用时间为{}'.format(time.time()-t))
cv2.waitKey(0)
#cv2.destroyAllWindows()
time.sleep(5)

效果图

所用时间 1.4秒左右 还是有点慢的

更改路径

里面只需要将 path后面的路径换成需要检测人脸的照片
将predictor 后面的路径 改成 上面提到的人脸检测模型的路径

打印关键点

#coding=utf-8
#图片检测 - Dlib版本
import cv2
import dlib
import time
t=time.time()
path = "../../face_photos/liushishi1.jpeg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
    "../shape_predictor_68_face_landmarks.dat"
)

dets = detector(gray, 1)
for face in dets:
    shape = predictor(img, face)  # 寻找人脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 1, (0, 255, 0), 2)
    cv2.imshow("image", img)
print('所用时间为{}'.format(time.time()-t))
cv2.waitKey(0)
#cv2.destroyAllWindows()
time.sleep(5)

效果图

在这里插入图片描述

人脸识别

人脸识别还没有来得及去实现
看了一下这篇文章写的还挺详细可以参考 https://blog.csdn.net/wc781708249/article/details/78562902

其他

其他人脸识别模块介绍 https://blog.csdn.net/Nirvana_6174/article/details/89599441

http://niehen.cn/category/face-reco/

本人接下来一段时间承接人脸图像处理,IOT开发等相关项目(毕设 比赛等)
有需要可联系qq:1639206518

  • 4
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值