基于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

欢迎加入人工智能-人脸识别 技术交流群 894243022

在这里插入图片描述

该文章有使用链接,如有侵权还请见谅。使用本文章或代码还请声明。

猜你喜欢

转载自blog.csdn.net/Nirvana_6174/article/details/89599136