def unpack_bz2(src_path): data = bz2.BZ2File(src_path).read() dst_path = src_path[:-4] with open(dst_path, 'wb') as fp: fp.write(data) return dst_path if __name__ == "__main__": """ Extracts and aligns all faces from images using DLib and a function from original FFHQ dataset preparation step python align_images.py /raw_images /aligned_images """ landmarks_model_path = unpack_bz2( tf.keras.utils.get_file('shape_predictor_68_face_landmarks.dat.bz2', LANDMARKS_MODEL_URL, cache_subdir='temp')) RAW_IMAGES_DIR = sys.argv[1] ALIGNED_IMAGES_DIR = sys.argv[2] landmarks_detector = LandmarksDetector(landmarks_model_path) for img_name in os.listdir(RAW_IMAGES_DIR): raw_img_path = os.path.join(RAW_IMAGES_DIR, img_name) for i, face_landmarks in enumerate( landmarks_detector.get_landmarks(raw_img_path), start=1): face_img_name = '%s_%02d.png' % (os.path.splitext(img_name)[0], i) aligned_face_path = os.path.join(ALIGNED_IMAGES_DIR, face_img_name) image_align(raw_img_path, aligned_face_path, face_landmarks)
for img_name in os.listdir(RAW_IMAGES_DIR): print('Aligning %s ...' % img_name) try: raw_img_path = os.path.join(RAW_IMAGES_DIR, img_name) fn = face_img_name = '%s_%02d.png' % ( os.path.splitext(img_name)[0], 1) if os.path.isfile(fn): continue print('Getting landmarks...') for i, face_landmarks in enumerate( landmarks_detector.get_landmarks(raw_img_path), start=1): try: print('Starting face alignment...') face_img_name = '%s_%02d.png' % ( os.path.splitext(img_name)[0], i) aligned_face_path = os.path.join(ALIGNED_IMAGES_DIR, face_img_name) image_align(raw_img_path, aligned_face_path, face_landmarks, output_size=args.output_size, x_scale=args.x_scale, y_scale=args.y_scale, em_scale=args.em_scale, alpha=args.use_alpha) print('Wrote result %s' % aligned_face_path) except: print("Exception in face alignment!") except: print("Exception in landmark detection!")
def align_image(input_path, output_path): landmarks_model_path = unpack_bz2(get_file('shape_predictor_68_face_landmarks.dat.bz2', LANDMARKS_MODEL_URL, cache_subdir='temp')) landmarks_detector = LandmarksDetector(landmarks_model_path) for i, face_landmarks in enumerate(landmarks_detector.get_landmarks(input_path), start=1): image_align(input_path, output_path, face_landmarks)
id_list = [x.split(' ')[1].rstrip("\n") for x in id_lines] # top n-most frequent id ctr = collections.Counter(id_list) top_id_list = [x[0] for x in ctr.most_common(3300)] # data list data_list = [(fn, ld, id) for fn, ld, id in zip(fn_list, ld_list, id_list) if id in top_id_list] for i, (fn, ld, id) in enumerate(data_list): # 4-point landmarks (left eye, right eye, left mouth, right mouth) face_landmarks = [[ld[0], ld[1]], [ld[2], ld[3]], [ld[6], ld[7]], [ld[8], ld[9]]] # FFHQ align image = cv2.imread(os.path.join(img_dir, fn)) aligned_image = image_align(image, face_landmarks, output_size=img_size, transform_size=img_size * 4, enable_padding=True) # save save_dir = os.path.join(save_base_dir, 'sort_by_id_aligned', id) if not os.path.exists(save_dir): os.makedirs(save_dir) cv2.imwrite(os.path.join(save_dir, '{}.png'.format(i)), aligned_image) print('[{}/{}] save {} for id: {}'.format(i + 1, len(data_list), fn, id)) print('done')