python+OpenCv+dlib实现人脸68个关键点检测

 

pip install dlib==19.7.0

下载地址:

http://dlib.net/files/

dlib中为我们提供了关于人脸检测标注训练好的文件可在http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

这个是81的关键点:貌似不是很准,

https://github.com/codeniko/shape_predictor_81_face_landmarks

import sys
import os
import dlib
import glob
from skimage import io
import numpy as np
import cv2
import time


cap = cv2.VideoCapture(0)
# fourcc = cv2.VideoWriter_fourcc(*'XVID')

# out = cv2.VideoWriter('output.avi',fourcc, 20.0, (1280, 720))

predictor_path = 'shape_predictor_81_face_landmarks.dat'
predictor_path = 'shape_predictor_68_face_landmarks.dat'

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

while(cap.isOpened()):
    ret, frame = cap.read()
    frame=cv2.resize(frame,(500,400))
    # img_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    start = time.time()
    frame = cv2.flip(frame, 1)
    dets = detector(frame, 0)
    for k, point in enumerate(dets):

        shape = predictor(frame, point)
        landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
        for num in range(shape.num_parts):
            cv2.circle(frame, (shape.parts()[num].x, shape.parts()[num].y), 1, (0,255,0), -1)
    print("time ->", time.time()-start)
    cv2.imshow('frame', frame)
    # out.write(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        print("q pressed")
        break


cap.release()
# out.release()

cv2.destroyAllWindows()



 

你可能感兴趣的:(python,opencv)