From f64b2495d987e1b86f68d14c33025579dbf9e5d2 Mon Sep 17 00:00:00 2001 From: iperov Date: Tue, 12 Oct 2021 17:23:57 +0400 Subject: [PATCH] dev_misc: code to extract microsoft/FaceSynthetics dataset --- mainscripts/dev_misc.py | 96 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/mainscripts/dev_misc.py b/mainscripts/dev_misc.py index a71bcdf..93a4359 100644 --- a/mainscripts/dev_misc.py +++ b/mainscripts/dev_misc.py @@ -13,7 +13,6 @@ from core.joblib import Subprocessor from core.leras import nn from DFLIMG import * from facelib import FaceType, LandmarksProcessor - from . import Extractor, Sorter from .Extractor import ExtractSubprocessor @@ -359,7 +358,7 @@ def extract_umd_csv(input_file_csv, -def dev_test(input_dir): +def dev_test1(input_dir): # LaPa dataset image_size = 1024 @@ -500,3 +499,96 @@ def dev_segmented_trash(input_dir): except: io.log_info ('fail to trashing %s' % (src.name) ) + + +def dev_test(input_dir): + """ + extract FaceSynthetics dataset https://github.com/microsoft/FaceSynthetics + + BACKGROUND = 0 + SKIN = 1 + NOSE = 2 + RIGHT_EYE = 3 + LEFT_EYE = 4 + RIGHT_BROW = 5 + LEFT_BROW = 6 + RIGHT_EAR = 7 + LEFT_EAR = 8 + MOUTH_INTERIOR = 9 + TOP_LIP = 10 + BOTTOM_LIP = 11 + NECK = 12 + HAIR = 13 + BEARD = 14 + CLOTHING = 15 + GLASSES = 16 + HEADWEAR = 17 + FACEWEAR = 18 + IGNORE = 255 + """ + + + image_size = 1024 + face_type = FaceType.WHOLE_FACE + + input_path = Path(input_dir) + + + + output_path = input_path.parent / f'{input_path.name}_out' + if output_path.exists(): + output_images_paths = pathex.get_image_paths(output_path) + if len(output_images_paths) != 0: + io.input(f"\n WARNING !!! \n {output_path} contains files! \n They will be deleted. \n Press enter to continue.\n") + for filename in output_images_paths: + Path(filename).unlink() + output_path.mkdir(parents=True, exist_ok=True) + + data = [] + + for filepath in io.progress_bar_generator(pathex.get_paths(input_path), "Processing"): + if filepath.suffix == '.txt': + + image_filepath = filepath.parent / f'{filepath.name.split("_")[0]}.png' + if not image_filepath.exists(): + print(f'{image_filepath} does not exist, skipping') + + lmrks = [] + for lmrk_line in filepath.read_text().split('\n'): + if len(lmrk_line) == 0: + continue + + x, y = lmrk_line.split(' ') + x, y = float(x), float(y) + + lmrks.append( (x,y) ) + + lmrks = np.array(lmrks[:68], np.float32) + rect = LandmarksProcessor.get_rect_from_landmarks(lmrks) + data += [ ExtractSubprocessor.Data(filepath=image_filepath, rects=[rect], landmarks=[ lmrks ] ) ] + + if len(data) > 0: + io.log_info ("Performing 3rd pass...") + data = ExtractSubprocessor (data, 'final', image_size, 95, face_type, final_output_path=output_path, device_config=nn.DeviceConfig.CPU()).run() + + for filename in io.progress_bar_generator(pathex.get_image_paths (output_path), "Processing"): + filepath = Path(filename) + + dflimg = DFLJPG.load(filepath) + + src_filename = dflimg.get_source_filename() + image_to_face_mat = dflimg.get_image_to_face_mat() + + seg_filepath = input_path / ( Path(src_filename).stem + '_seg.png') + if not seg_filepath.exists(): + raise ValueError(f'{seg_filepath} does not exist') + + seg = cv2_imread(seg_filepath) + seg_inds = np.isin(seg, [1,2,3,4,5,6,9,10,11]) + seg[~seg_inds] = 0 + seg[seg_inds] = 1 + seg = seg.astype(np.float32) + seg = cv2.warpAffine(seg, image_to_face_mat, (image_size, image_size), cv2.INTER_LANCZOS4) + dflimg.set_xseg_mask(seg) + dflimg.save() + \ No newline at end of file