| ''' |
| 测试第一步,将50wild图片,使用MTCNN进行检测、截取,得到50cropped后的数据集 |
| ''' |
| import os |
| import torch |
| from facenet_pytorch import MTCNN |
| from PIL import Image |
| from tqdm import tqdm |
| from concurrent.futures import ThreadPoolExecutor |
|
|
| |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| mtcnn = MTCNN(keep_all=False, device=device) |
|
|
| |
| data_dir = '../../../datasets/classification/LFWPairs/lfw-py/lfw_test_template_50_wild' |
| save_dir = '../../../datasets/classification/LFWPairs/lfw-py/lfw_test_template_50_cropped' |
| error_log_path = '../../../datasets/classification/LFWPairs/lfw-py/lfw_error_log_selected_50.txt' |
|
|
| |
| os.makedirs(save_dir, exist_ok=True) |
|
|
| |
| def crop_and_save_faces(image_path, save_path): |
| try: |
| |
| image = Image.open(image_path).convert('RGB') |
|
|
| |
| boxes, _ = mtcnn.detect(image) |
|
|
| if boxes is not None: |
| for i, box in enumerate(boxes): |
| x1, y1, x2, y2 = map(int, box) |
| if x2 > x1 and y2 > y1: |
| face = image.crop((x1, y1, x2, y2)) |
| os.makedirs(os.path.dirname(save_path), exist_ok=True) |
| face.save(save_path) |
| else: |
| |
| with open(error_log_path, 'a') as f: |
| f.write(f"未检测到人脸: {image_path}\n") |
| except Exception as e: |
| |
| with open(error_log_path, 'a') as f: |
| f.write(f"处理 {image_path} 时出错: {e}\n") |
|
|
| |
| for root, dirs, files in os.walk(data_dir): |
| for file in files: |
| if file.lower().endswith(('jpg', 'jpeg', 'png')): |
| if 'test' in root or 'template' in root: |
| image_path = os.path.join(root, file) |
| relative_path = os.path.relpath(image_path, data_dir) |
| save_path = os.path.join(save_dir, relative_path) |
|
|
| |
| with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor: |
| list(tqdm(executor.map(lambda img: crop_and_save_faces(img, os.path.join(save_dir, os.path.relpath(img, data_dir))), [image_path]), total=1)) |
|
|
| print("所有人脸提取完成并保存到: ", save_dir) |
| print("错误日志已保存到: ", error_log_path) |
|
|