From cfd29513b4557f52a53fd8ceaf078380ae1751f0 Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:50:58 +0800 Subject: [PATCH] Add a processer bar with remain time for CLI --- core/processor.py | 87 ++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/core/processor.py b/core/processor.py index b8ec133..4844826 100644 --- a/core/processor.py +++ b/core/processor.py @@ -1,42 +1,45 @@ -import os - -import cv2 -import insightface -import core.globals -from core.config import get_face - -FACE_SWAPPER = None - - -def get_face_swapper(): - global FACE_SWAPPER - if FACE_SWAPPER is None: - model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx') - FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=core.globals.providers) - return FACE_SWAPPER - - -def process_video(source_img, frame_paths): - source_face = get_face(cv2.imread(source_img)) - for frame_path in frame_paths: - frame = cv2.imread(frame_path) - try: - face = get_face(frame) - if face: - result = get_face_swapper().get(frame, face, source_face, paste_back=True) - cv2.imwrite(frame_path, result) - print('.', end='', flush=True) - else: - print('S', end='', flush=True) - except Exception: - print('E', end='', flush=True) - pass - - -def process_img(source_img, target_path, output_file): - frame = cv2.imread(target_path) - face = get_face(frame) - source_face = get_face(cv2.imread(source_img)) - result = get_face_swapper().get(frame, face, source_face, paste_back=True) - cv2.imwrite(output_file, result) - print("\n\nImage saved as:", output_file, "\n\n") +import os +from tqdm import tqdm +import cv2 +import insightface +import core.globals +from core.config import get_face +from tqdm import tqdm + +FACE_SWAPPER = None + + +def get_face_swapper(): + global FACE_SWAPPER + if FACE_SWAPPER is None: + model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx') + FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=core.globals.providers) + return FACE_SWAPPER + + +def process_video(source_img, frame_paths): + source_face = get_face(cv2.imread(source_img)) + with tqdm(total=len(frame_paths), desc="Processing", unit="frame", dynamic_ncols=True, bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]') as pbar: + for frame_path in frame_paths: + frame = cv2.imread(frame_path) + try: + face = get_face(frame) + if face: + result = get_face_swapper().get(frame, face, source_face, paste_back=True) + cv2.imwrite(frame_path, result) + pbar.set_postfix(status='.', refresh=True) + else: + pbar.set_postfix(status='S', refresh=True) + except Exception: + pbar.set_postfix(status='E', refresh=True) + pass + pbar.update(1) + + +def process_img(source_img, target_path, output_file): + frame = cv2.imread(target_path) + face = get_face(frame) + source_face = get_face(cv2.imread(source_img)) + result = get_face_swapper().get(frame, face, source_face, paste_back=True) + cv2.imwrite(output_file, result) + print("\n\nImage saved as:", output_file, "\n\n")