注意点:
- dlib.get_frontal_face_detector( ) 获取人脸检测器
- dlib.shape_predictor( ) 预测人脸关键点
人脸关键点模型,下载地址:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
face_landmark_detection.py
# 1 加入库
import cv2
import matplotlib.pyplot as plt
import dlib
# 2 读取一张图片
image = cv2.imread("Tom2.jpeg")
# 3 调用人脸检测器
detector = dlib.get_frontal_face_detector()
# 4 加载预测关键点模型(68个关键点)
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 5 灰度转换
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 6 人脸检测
faces = detector(gray, 1)
# 7 循环,遍历每一张人脸,给人脸绘制矩形框和关键点
for face in faces: #(x, y, w, h)
# 8 绘制矩形框
cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (0,255,0), 5)
# 9 预测关键点
shape = predictor(image, face)
# 10 获取到关键点坐标
for pt in shape.parts():
# 获取横纵坐标
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()
face_landmark_detection_video.py
# 1 加入库
import cv2
import dlib
# 2 打开摄像头
capture = cv2.VideoCapture(0)
# 3 获取人脸检测器
detector = dlib.get_frontal_face_detector()
# 4 获取人脸关键点检测模型
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
while True:
# 5 读取视频流
ret, frame = capture.read()
# 6 灰度转换
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 7 人脸检测
faces = detector(gray, 1)
# 8 绘制每张人脸的矩形框和关键点
for face in faces:
# 8.1 绘制矩形框
cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0,255,0), 3)
# 8.2 检测到关键点
shape = predictor(gray, face)
# 8.3 获取关键点的坐标
for pt in shape.parts():
# 每个点的坐标
pt_position = (pt.x, pt.y)
# 8.4 绘制关键点
cv2.circle(frame, pt_position, 3, (255,0,0), -1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 9 显示效果
cv2.imshow("face detection landmark", frame)
capture.release()
cv2.destroyAllWindows()
- 基于face_recognition进行人脸关键点检测
- face_recognition 使用世界上最简单的人脸识别工具,它使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。
(1)Github 地址:https://github.com/ageitgey/face_recognition
(2)官方指南:
https://face-recognition.readthedocs.io/en/latest/readme.html
(3)源码实现:
https://face-recognition.readthedocs.io/en/latest/face_recognition.html
(4)安装:
pip install face_recognition
face_landmark_rg.py
# 1 加入库
import face_recognition
import cv2
import matplotlib.pyplot as plt
# 2 方法:显示图片
def show_image(image, title):
plt.title(title)
plt.imshow(image)
plt.axis("off")
# 3 方法:绘制Landmars关键点
def show_landmarks(image, landmarks):
for landmarks_dict in landmarks:
for landmarks_key in landmarks_dict.keys():
for point in landmarks_dict[landmarks_key]:
cv2.circle(image, point, 2, (0,0,255), -1)
return image
# 4 主函数
def main():
# 5 读取图片
image = cv2.imread("Tom.jpeg")
# 6 图片灰度转换
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 7 调用face_recognition库中的方法:face_landmarks()
face_marks = face_recognition.face_landmarks(gray, None, "large")
print(face_marks)
# 8 绘制关键点
img_result = show_landmarks(image.copy(), face_marks)
# 9 创建画布
plt.figure(figsize=(9,6))
plt.suptitle("Face Landmarks with face_recognition", fontsize=14, fontweight="bold")
# 10 显示整体效果
show_image(img_result, "landmarks")
plt.show()
if __name__ == '__main__':
main()