dlib的人脸特征数据shape_predictor_5_face_landmarks.dat已经可以满足普通的人脸特征提取了
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 14 14:54:23 2021
@author:
"""
import dlib
import cv2
import os
predictor_path='shape_predictor_5_face_landmarks.dat'
detector=dlib.get_frontal_face_detector()
sp=dlib.shape_predictor(os.path.join("F:\code\Pythoncode\dlib\shape_predictor_5_face_landmarks.dat",predictor_path))
cap=cv2.VideoCapture(0)
while True:
ret,frame=cap.read()
if not cap.isOpened():
print("打开摄像头")
dets=detector(frame,1)
print(frame.shape)
num_faces=len(dets)
if num_faces==0:
print("没有发现人脸")
cv2.putText(frame,'faces:{}'.format(num_faces),(10,30),cv2.FONT_HERSHEY_PLAIN,2,(0,0,255),2)
faces=dlib.full_object_detections()
for detection in dets:
faces.append(sp(frame,detection))
print(detection.left())
cv2.rectangle(frame,(detection.left(),detection.top()),(detection.right(),detection.bottom()),[0,255,0],3)
cv2.putText(frame,'press ESC quit',(10,450),cv2.FONT_HERSHEY_PLAIN,2,(0,0,255),2)
cv2.imshow('image',frame)
if cv2.waitKey(10) == 27:
break
cv2.destroyAllWindows()