diff --git a/core/config.py b/core/analyser.py similarity index 100% rename from core/config.py rename to core/analyser.py diff --git a/core/processor.py b/core/processor.py deleted file mode 100644 index b8ec133..0000000 --- a/core/processor.py +++ /dev/null @@ -1,42 +0,0 @@ -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") diff --git a/core/swapper.py b/core/swapper.py new file mode 100644 index 0000000..188b71f --- /dev/null +++ b/core/swapper.py @@ -0,0 +1,44 @@ +import os +from tqdm import tqdm +import cv2 +import insightface +import core.globals +from core.analyser 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)) + 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 progress: + 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) + progress.set_postfix(status='.', refresh=True) + else: + progress.set_postfix(status='S', refresh=True) + except Exception: + progress.set_postfix(status='E', refresh=True) + pass + progress.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") diff --git a/requirements.txt b/requirements.txt index 75146f2..2b6968f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ torch==2.0.1 onnxruntime-gpu==1.15.0 opennsfw2==0.10.2 protobuf==3.20.2 +tqdm==4.65.0 diff --git a/run.py b/run.py index 3775973..85592ea 100755 --- a/run.py +++ b/run.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import platform +import signal import sys import time import shutil @@ -20,9 +21,9 @@ import cv2 import threading from PIL import Image, ImageTk import core.globals -from core.processor import process_video, process_img +from core.swapper import process_video, process_img from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames, rreplace -from core.config import get_face +from core.analyser import get_face if 'ROCMExecutionProvider' in core.globals.providers: del torch @@ -30,6 +31,7 @@ if 'ROCMExecutionProvider' in core.globals.providers: pool = None args = {} +signal.signal(signal.SIGINT, lambda signal_number, frame: quit()) parser = argparse.ArgumentParser() parser.add_argument('-f', '--face', help='use this face', dest='source_img') parser.add_argument('-t', '--target', help='replace this face', dest='target_path')