# 对静态人脸图像文件进行68个特征点的标定
# Author: coneypo
# GitHub: https://github.com/coneypo/Dlib_face_detection_from_camera
import dlib # 人脸识别的库 Dlib
import numpy as np # 数据处理的库 numpy
import cv2 # 图像处理的库 OpenCv
# Dlib 检测器和预测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('F:/XiangMu2024/myface68/shape_predictor_68_face_landmarks.dat')
# 读取图像文件
img_rd = cv2.imread("F:/XiangMu2024/myface68/IMG_20200427_120607.jpg")
#img_rd = cv2.imread("F:/XiangMu2024/myface68/640.jpg")
img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY)
# 人脸数
faces = detector(img_gray, 0)
# 待会要写的字体
font = cv2.FONT_HERSHEY_SIMPLEX
# 标 68 个点
if len(faces) != 0:
# 检测到人脸
for i in range(len(faces)):
# 取特征点坐标
landmarks = np.matrix([[p.x, p.y] for p in predictor(img_rd, faces[i]).parts()])
for idx, point in enumerate(landmarks):
# 68 点的坐标
pos = (point[0, 0], point[0, 1])
# 利用 cv2.circle 给每个特征点画一个圈,共 68 个
cv2.circle(img_rd, pos, 2, color=(139, 0, 0))
# 利用 cv2.putText 写数字 1-68
cv2.putText(img_rd, str(idx + 1), pos, font, 0.2, (187, 255, 255), 1, cv2.LINE_AA)
cv2.putText(img_rd, "faces: " + str(len(faces)), (20, 50), font, 1, (0, 0, 0), 1, cv2.LINE_AA)
else:
# 没有检测到人脸
cv2.putText(img_rd, "no face", (20, 50), font, 1, (0, 0, 0), 1, cv2.LINE_AA)
# 窗口显示
# 取 0 可以拖参数动缩放窗口,为 1 不可以 https://blog.csdn.net/qq_43436296/article/details/126786430
# cv2.namedWindow("image", 0)
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.imshow("image", img_rd)
k=cv2.waitKey(0)
## k = cv2.waitKey(0) & 0xFF # 64位机器
if k == 27: # 按下esc时,退出
cv2.destroyAllWindows()
elif k == ord('s'): # 按下s键时保存并退出
cv2.imwrite("F:/XiangMu2024/myface68/messigray.png",img_rd)
cv2.destroyAllWindows()
01-15