def generate_training_list(): subject_id_list = os.listdir(args.db_dir) subject_id_list.sort() subject_id = {} for index, subject_dir in enumerate(subject_id_list): subject_id[subject_dir] = index final_contents = [] p_bar = tqdm(total=len(subject_id_list)) for sub_dir in subject_id_list: identity = subject_id[sub_dir] cur_dir = os.path.join(args.db_dir, sub_dir) im_files = os.listdir(cur_dir) items = [[identity, os.path.join(sub_dir, im_file)] for im_file in im_files] final_contents.extend(items) p_bar.update() p_bar.close() if args.shuffle: random.shuffle(final_contents) save_csv(os.path.join(args.db_dir, 'WebFace.csv'), final_contents, header=['SUBJECT_ID', 'FILE', ]) with open(os.path.join(args.db_dir, 'WebFace.lst'), 'w') as f: csv_writer = csv.writer(f, delimiter='\t') for i, item in enumerate(final_contents): csv_writer.writerow([i, item[0], item[1]])
def merge_train_test_sets(): sub_dirs = os.listdir(args.db_dir) contents = {} subject_id_list = [] for sub_dir in sub_dirs: l = os.listdir(os.path.join(args.db_dir, sub_dir)) l.sort() contents[sub_dir] = l subject_id_list.extend(l) subject_id_list.sort() subject_id = {} for index, subject_dir in enumerate(subject_id_list): subject_id[subject_dir] = index final_contents = [] p_bar = tqdm(total=len(subject_id_list)) for sub_dir, contents in contents.items(): for ss_dir in contents: identity = subject_id[ss_dir] cur_dir = os.path.join(args.db_dir, sub_dir, ss_dir) im_files = os.listdir(cur_dir) items = [[identity, os.path.join(sub_dir, ss_dir, im_file)] for im_file in im_files] final_contents.extend(items) p_bar.update() p_bar.close() if args.shuffle: random.shuffle(final_contents) save_csv(os.path.join(args.db_dir, 'VGG-FACE2.csv'), final_contents, header=['FILE', 'SUBJECT_ID']) with open(os.path.join(args.db_dir, 'VGG-FACE2.lst'), 'w') as f: csv_writer = csv.writer(f, delimiter='\t') for i, item in enumerate(final_contents): csv_writer.writerow([i, item[0], item[1]]) print('Done')
def generate_training_list(): subject_id_list = os.listdir(args.db_dir) subject_id_list.sort() subject_id = {} for index, subject_dir in enumerate(subject_id_list): if os.path.isdir(os.path.join(args.db_dir, subject_dir)): subject_id[subject_dir] = index if not os.path.exists(os.path.join(args.out_dir, subject_dir)): os.makedirs(os.path.join(args.out_dir, subject_dir)) final_contents = [] p_bar = tqdm(total=len(subject_id_list)) for sub_dir in subject_id_list: identity = subject_id[sub_dir] cur_dir = os.path.join(args.db_dir, sub_dir) im_files = os.listdir(cur_dir) im_files = [im_file for im_file in im_files if 'texture' in im_file] items = [[identity, os.path.join(sub_dir, im_file)] for im_file in im_files] final_contents.extend(items) p_bar.update() p_bar.close() with Pool() as p: final_contents = list(tqdm(p.imap_unordered(crop_image, final_contents), total=len(final_contents))) final_contents = list(filter(None, final_contents)) if args.shuffle: random.shuffle(final_contents) if len(args.out_file) > 0: save_csv(args.out_file, final_contents, header=['SUBJECT_ID', 'FILE', ]) with open(args.out_file.replace(os.path.splitext(args.out_file)[1], '.lst'), 'w') as f: csv_writer = csv.writer(f, delimiter='\t') for i, item in enumerate(final_contents): csv_writer.writerow([i, item[0], item[1]])
def main(): contents = load_csv(args.list, read_header=True) # create out dir for row in contents: if not os.path.exists( os.path.join(args.out_dir, os.path.dirname(row['FILE']))): os.makedirs( os.path.join(args.out_dir, os.path.dirname(row['FILE']))) with Pool() as p: contents_post = list( tqdm.tqdm(p.imap_unordered(align_image, contents), total=len(contents))) contents_post = list(filter(None, contents_post)) if len(args.out_path) > 0: save_csv(args.out_path, contents_post, header=['FILE'])