最近在做人脸相关课题,找个地方保存一下部分代码。
用的dlib是81个关键点,GitHub网址:https://github.com/codeniko/shape_predictor_81_face_landmarks
#读取视频,检测一帧中的人脸,并在图像中标出关键点,同时打印出关键点的坐标。
import dlib
import numpy as np
import cv2
from matplotlib import pyplot as plt
cap = cv2.VideoCapture('视频路径')
predictor_path = "shape_predictor_81_face_landmarks.dat的路径"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
while(True):
ret, frame = cap.read()
# 取灰度
img_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# 人脸数rects
rects = detector(img_gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(frame, rects[i]).parts()])
for idx, point in enumerate(landmarks):
# 81点的坐标
pos =