i m so sad

This commit is contained in:
2024-12-10 11:35:13 +08:00
parent 9286a9de68
commit 02ae03d147
4 changed files with 70 additions and 2 deletions
+7
View File
@@ -75,3 +75,10 @@ download(face_label_url,face_label_path)
# 解压数据集
decompress(os.path.join(face_dataset_path,face_dataset_url.split('/')[-1]),face_dataset_path)
decompress(os.path.join(face_label_path,face_label_url.split('/')[-1]),face_label_path)
# 下载ResNet模型
model_url1='https://github.com/davisking/dlib-models/raw/master/shape_predictor_68_face_landmarks.dat'
model_url2='https://github.com/davisking/dlib-models/raw/master/dlib_face_recognition_resnet_model_v1.dat'
model_path='models/'
download(model_url1,model_path)
download(model_url2,model_path)
+31
View File
@@ -0,0 +1,31 @@
import cv2 as cv
import os
import tqdm
# 处理图像
def pretrain(img_path,output_path):
img=cv.imread(img_path, cv.IMREAD_GRAYSCALE)
_,img=cv.threshold(img,127,255,cv.THRESH_BINARY)
img=cv.blur(img,(3,3))
os.makedirs(os.path.dirname(output_path), exist_ok=True)
cv.imwrite(output_path, img)
# 获取文件路径
def get_img_name(directory, extensions=None):
if extensions is None:
extensions = ['.png', '.jpg','.jpeg']
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 extensions):
files.append(os.path.join(root, file_name))
return files
# 处理人脸数据集
os.makedirs('cache/pretrained/face/', exist_ok=True)
face_files=get_img_name('cache/dataset/face/')
print('预处理人脸数据中:')
for img_path in tqdm.tqdm(face_files):
relative_path=os.path.relpath(img_path, 'cache/dataset/face/')
output_path=os.path.join('cache/pretrained/face', relative_path)
pretrain(img_path, output_path)
+31
View File
@@ -0,0 +1,31 @@
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')