18(1)基于dlib进行人脸关键点检测( 图片 )
Tommy老师视频链接
凯哥英语视频
# 1.导入库
import cv2
import matplotlib.pyplot as plt
import dlib
# 2.读取一张图片
image = cv2.imread('../../images/tom.jpg')
# 3.调用人脸检测器
detector = dlib.get_frontal_face_detector()
# 4.加载预测关键点模型(68个关键点)
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# 5.灰度转换
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 6.人脸检测
faces = detector(gray, 1) # 0 原始大小 1 放大一倍
# 7.循环,遍历每一张人脸,给人脸绘制矩形框和关键点
for face in faces:
# 8.绘制矩形框
cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 5)
# 9.预测关键点
shape = predictor(image, face)
for pt in shape.parts():
# 10.获取关键点坐标
pt_position = (pt.x, pt.y)
# 11.显示/绘制关键点
cv2.circle(image, pt_position, 2, (0, 0, 255), -1)
# 12.显示整个效果图
plt.imshow(image)
plt.axis('off')
plt.show()
别的也没啥说的
ok,那就这样吧~
欢迎各位大佬留言吐槽,也可以深入交流~