dlib 对静态人脸图像文件进行68个特征点的标定--- 中文详细解释

# 对静态人脸图像文件进行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()

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值