def get_face_chips(self, img, face_rects, facial_points=None, output_square=False): if facial_points is None: if self.aligner is None: raise Exception('FaceAligner.aligner is not initialized') rects, facial_points = self.aligner.align_face(img, face_rects) reference_5pts = None output_size = (96, 112) # (w, h) not (h,w) if output_square: output_size = (112, 112) reference_5pts = get_reference_facial_points(output_size) face_chips = [] for facial_5pts in facial_points: facial_5pts = np.reshape(facial_5pts, (2, -1)) dst_img = warp_and_crop_face(img, facial_5pts, reference_5pts, output_size) face_chips.append(dst_img) return face_chips
def get_aligned_face_chips(self, img_list, facial_points_list, output_square=True): """Get aligned face chips in a image list. Params: img_list: a list of input images, each image is a numpy array facial_points_list: a list of face landmarks, has the same length as img_list, each one is for one image output_square: whether to output square face chips Return: a list of aligned face roi chips (eacho one is a numpy array), the output list has the same length of input pts_with_angles """ face_chips = [] output_size = (96, 112) # (w, h) not (h,w) if output_square: output_size = (112, 112) reference_5pts = get_reference_facial_points(output_size) for img, facial_points in zip(img_list, facial_points_list): facial_5pts = np.reshape(facial_points, (5, -1)) dst_img = warp_and_crop_face(img, facial_5pts, reference_5pts, output_size) face_chips.append(dst_img) return face_chips
#GT_RECT = [68, 68, 182, 182] #GT_AREA = (GT_RECT[2] - GT_RECT[0] + 1) * (GT_RECT[3] - GT_RECT[1] + 1) overlap_thresh = 0.3 only_align_missed = False do_align = True # crop settings, set the region of cropped faces default_square = True padding_factor = 0.25 outer_padding = (0, 0) output_size = (224, 224) # get the referenced 5 landmarks position in the crop settings reference_5pts = get_reference_facial_points(output_size, padding_factor, outer_padding, default_square) aligned_save_dir = './gender_crop_data' file_root_path = '../../../mtcnn-caffe-good/mtcnn_aligner/result_after_align/' if not osp.exists(file_root_path): print('ERROR: webface root dir not found!!!') else: if not osp.exists(aligned_save_dir): print('mkdir for aligned faces, aligned root dir: ', aligned_save_dir) os.makedirs(aligned_save_dir) count = 30049 for root, dirs, files in os.walk(file_root_path): for file in files[30049:]:
import cv2 import json import os import os.path as osp #from matplotlib import pyplot as plt from fx_warp_and_crop_face import get_reference_facial_points, warp_and_crop_face # crop settings, set the region of cropped faces output_square = True padding_factor = 0.25 output_padding = (0, 0) output_size = (224, 224) # get the referenced 5 landmarks position in the crop settings referenced_5pts = get_reference_facial_points(output_size, padding_factor, output_padding, output_square) img_root_dir = '' landmark_fn = r'./mtcnn_fd_rlt_test_imgs.json' aligned_save_dir = './faces' + '-mtcnn-aligned-224x224' log_fn1 = 'align_succeeded_list.txt' log_fn2 = 'align_failed_list.txt' log_align_params = 'align_params.txt' fp_in = open(landmark_fn, 'r') img_list = json.load(fp_in) fp_in.close() if not osp.exists(aligned_save_dir):