def dev_test_68(input_dir ): # process 68 landmarks dataset with .pts files input_path = Path(input_dir) if not input_path.exists(): raise ValueError('input_dir not found. Please ensure it exists.') output_path = input_path.parent / (input_path.name+'_aligned') io.log_info(f'Output dir is % {output_path}') if output_path.exists(): output_images_paths = pathex.get_image_paths(output_path) if len(output_images_paths) > 0: io.input_bool("WARNING !!! \n %s contains files! \n They will be deleted. \n Press enter to continue." % (str(output_path)), False ) for filename in output_images_paths: Path(filename).unlink() else: output_path.mkdir(parents=True, exist_ok=True) images_paths = pathex.get_image_paths(input_path) for filepath in io.progress_bar_generator(images_paths, "Processing"): filepath = Path(filepath) pts_filepath = filepath.parent / (filepath.stem+'.pts') if pts_filepath.exists(): pts = pts_filepath.read_text() pts_lines = pts.split('\n') lmrk_lines = None for pts_line in pts_lines: if pts_line == '{': lmrk_lines = [] elif pts_line == '}': break else: if lmrk_lines is not None: lmrk_lines.append (pts_line) if lmrk_lines is not None and len(lmrk_lines) == 68: try: lmrks = [ np.array ( lmrk_line.strip().split(' ') ).astype(np.float32).tolist() for lmrk_line in lmrk_lines] except Exception as e: print(e) print(filepath) continue rect = LandmarksProcessor.get_rect_from_landmarks(lmrks) output_filepath = output_path / (filepath.stem+'.jpg') img = cv2_imread(filepath) img = imagelib.normalize_channels(img, 3) cv2_imwrite(output_filepath, img, [int(cv2.IMWRITE_JPEG_QUALITY), 95] ) DFLJPG.embed_data(output_filepath, face_type=FaceType.toString(FaceType.MARK_ONLY), landmarks=lmrks, source_filename=filepath.name, source_rect=rect, source_landmarks=lmrks ) io.log_info("Done.")
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()