import os
import cv2
import dlib
import numpy as np
from imutils import face_utils
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("./pth/shape_predictor_68_face_landmarks.dat")
frame = cv2.imread('img.png')
cv2.imshow('frame', frame)
cv2.waitKey()
dets = detector(frame, 1)
for det in dets:
# cv2.rectangle 参数1:图片, 参数2:左上角坐标, 参数2:左上角坐标, 参数3:右下角坐标, 参数4:颜色(R,G,B), 参数2:粗细
# cv2.rectangle(frame_rec, (det.left(), det.top()), (det.right(), det.bottom()), (0, 255, 0), 2) # 人脸识别框
shape = predictor(frame, det)
# for pt in shape.parts(): # 显示全部关键点
# pt_position = (pt.x,pt.y)
# cv2.circle(frame, pt_position, 1, (0, 0, 255), -1) # 5:点的半径;-1:实心圆
# shape = face_utils.shape_to_np(shape)
# for (a, b) in shape:
# cv2.circle(frame, (a, b), 1, (0, 0, 255), -1) #draw facial landmarks
landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
for idx, point in enumerate(landmarks):
if idx in [17,21,22,26,30,48,54] :
# 68点的坐标
pos = (point[0, 0], point[0, 1])
# 利用cv2.circle给每个特征点画一个圈,共68个
cv2.circle(frame, pos, 2, color=(0, 0, 255))
# 利用cv2.putText输出1-68
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, str(idx + 1), pos, font, 0.5, (255, 0, 0), 1, cv2.LINE_AA)
print("index=" + str(idx + 1) + " x=" + str(pos[0]) + " y=" + str(pos[1]))
cv2.imshow('frame', frame)
cv2.waitKey()
68关键点----显示部分关键点
于 2023-03-17 11:55:00 首次发布