def extract_faces(input_path: Path, output_path: Path, config_path: Path, process_images: bool, extract_mask: bool, step_mod: int): assert input_path.exists(), f"No such path: {input_path}" assert config_path.exists(), f"No such config file: {config_path}" if not output_path.exists(): logging.info(f"Creating output dir: {output_path}") output_path.mkdir() with open(str(config_path), 'r') as ymlfile: cfg = yaml.load(ymlfile, yaml.SafeLoader) face_detector = FaceDetector(cfg) frame_count = 0 if process_images: # collected all image paths img_paths = image_processing.get_imgs_paths(input_path, as_str=False) logging.info("Running Face Extraction over images") # iterate over all collected image paths for img_path in tqdm(img_paths): frame_count += 1 img = cv2.imread(str(img_path)) if extract_mask: face_mask_fun(img, frame_count, face_detector, output_path / f"{img_path.stem}.jpg") else: frame_extract_fun(img, frame_count, face_detector, output_path, step_mod) # process video else: # get a valid file from given directory if input_path.is_dir(): video_files = image_processing.get_imgs_paths(input_path, img_types=('*.gif', '*.webm', '*.mp4'), as_str=True) if not video_files: logging.error(f"No valid video files in: {input_path}") sys.exit(1) # for now just pick first one input_path = Path(video_files[0]) logging.info("Running Face Extraction over video") video_utils.process_video( str(input_path), lambda frame, frame_count: frame_extract_fun( frame, frame_count, face_detector, output_path, step_mod))
def main(_=None): logging.getLogger().setLevel(logging.INFO) parser = argparse.ArgumentParser(description='Face-swap. Extract Faces') parser.add_argument('-i', metavar='input_path', dest='input_path', required=True) parser.add_argument('-o', metavar='output_path', dest='output_path', required=True) parser.add_argument('-c', metavar='config_path', dest='config_path', default=CONFIG_PATH) parser.add_argument('-v', dest='verbose', action='store_true') parser.set_defaults(verbose=False) parser.add_argument('--process_images', dest='process_images', action='store_true') parser.set_defaults(process_images=False) parser.add_argument( '-s', metavar='step_mod', dest='step_mod', default=1, help="Save only face for frame where frame_num%step_mod == 0") args = parser.parse_args() input_path = Path(args.input_path) output_path = Path(args.output_path) process_images = args.process_images config_path = Path(args.config_path) step_mod = int(args.step_mod) if args.verbose: logging.getLogger().setLevel(logging.DEBUG) if not input_path.exists(): logging.info("No such path: {}".format(input_path)) sys.exit(1) if not output_path.exists(): logging.info("Creating output dir: {}".format(output_path)) output_path.mkdir() if not config_path.exists(): logging.info("No such config file: {}".format(config_path)) sys.exit(1) with open(str(config_path), 'r') as ymlfile: cfg = yaml.load(ymlfile) face_detector = FaceDetector(cfg) frame_count = 0 if process_images: # collected all image paths img_paths = image_processing.get_imgs_paths(input_path, as_str=False) logging.info("Running Face Extraction over images") # iterate over all collected image paths for img_path in tqdm(img_paths): frame_count += 1 img = cv2.imread(str(img_path)) frame_extract_fun(img, frame_count, face_detector, output_path, step_mod) # process video else: # get a valid file from given directory if input_path.is_dir(): video_files = image_processing.get_imgs_paths(input_path, img_types=('*.gif', '*.webm', '*.mp4'), as_str=True) if not video_files: logging.error("No valid video files in: {}".format(input_path)) sys.exit(1) # for now just pick first one input_path = Path(video_files[0]) logging.info("Running Face Extraction over video") video_utils.process_video( str(input_path), lambda frame, frame_count: frame_extract_fun( frame, frame_count, face_detector, output_path, step_mod))