import dlib
import cv2 as cv
import numpy as np
import os

# 提取人脸特征
def extract_face_features(image_path):
    img=cv.imread(image_path)
    detections=detector(img,1)
    face_features=[]
    for rect in detections:
        shape=predictor(img,rect)
        face_descriptor=face_rec_model.compute_face_descriptor(img,shape)
        face_features.append(face_descriptor)
    return face_features

# 获取图片路径
def get_img_path(directory,extension=None):
    if extension==None:
        extension=['.jpg','.jpeg','.png']
    files=[]
    for root, dirs, file_names in os.walk(directory):
        for file_name in file_names:
            if any(file_name.lower().endswith(ext) for ext in extension):
                files.append(os.path.join(root,file_name))
    return files

# 加载dlib模型
detector = dlib.get_frontal_face_detector()
predictor=dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat')
face_rec_model=dlib.face_recognition_model_v1('实验八/models/dlib_face_recognition_resnet_model_v1.dat')