From cfd29513b4557f52a53fd8ceaf078380ae1751f0 Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:50:58 +0800 Subject: [PATCH 1/7] 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") From 667c7da540f99826d82c8793c8bde7e02cb68231 Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:54:52 +0800 Subject: [PATCH 2/7] Add a processer bar with remain time for CLI --- core/processor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/core/processor.py b/core/processor.py index 4844826..d604d68 100644 --- a/core/processor.py +++ b/core/processor.py @@ -4,7 +4,6 @@ import cv2 import insightface import core.globals from core.config import get_face -from tqdm import tqdm FACE_SWAPPER = None From c16049517d58d867a3eb334a7e2884b7cf2cf29c Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:55:36 +0800 Subject: [PATCH 3/7] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) 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 From f85ae763670ce4eac20f3a364c2d6fc33607a62a Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 21:03:44 +0200 Subject: [PATCH 4/7] Proper exiting via ctrl+c --- core/processor.py | 10 +++++----- run.py | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/processor.py b/core/processor.py index d604d68..f601e89 100644 --- a/core/processor.py +++ b/core/processor.py @@ -18,7 +18,7 @@ def get_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: + 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: @@ -26,13 +26,13 @@ def process_video(source_img, frame_paths): 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) + progress.set_postfix(status='.', refresh=True) else: - pbar.set_postfix(status='S', refresh=True) + progress.set_postfix(status='S', refresh=True) except Exception: - pbar.set_postfix(status='E', refresh=True) + progress.set_postfix(status='E', refresh=True) pass - pbar.update(1) + progress.update(1) def process_img(source_img, target_path, output_file): diff --git a/run.py b/run.py index 3775973..2f24ce6 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 @@ -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') From cec2c214019ed2147d1ece63cf17c9af6a52354d Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 21:17:01 +0200 Subject: [PATCH 5/7] Rename files --- core/{config.py => analyser.py} | 0 core/{processor.py => swapper.py} | 88 +++++++++++++++---------------- run.py | 4 +- 3 files changed, 46 insertions(+), 46 deletions(-) rename core/{config.py => analyser.py} (100%) rename core/{processor.py => swapper.py} (95%) 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/swapper.py similarity index 95% rename from core/processor.py rename to core/swapper.py index f601e89..188b71f 100644 --- a/core/processor.py +++ b/core/swapper.py @@ -1,44 +1,44 @@ -import os -from tqdm import tqdm -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)) - 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") +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/run.py b/run.py index 2f24ce6..85592ea 100755 --- a/run.py +++ b/run.py @@ -21,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 From 946e5f35ffbee5b1661073c7ce2a9b86719c2402 Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Thu, 1 Jun 2023 01:42:43 +0530 Subject: [PATCH 6/7] hardcode tensorflow version --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 2b6968f..10ef903 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ tk==0.1.0 pillow==9.5.0 torch==2.0.1 onnxruntime-gpu==1.15.0 +tensorflow==2.12.0 opennsfw2==0.10.2 protobuf==3.20.2 tqdm==4.65.0 From c208ffa5516b43e66092c6a1b83b37e711c8e41e Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Thu, 1 Jun 2023 02:03:15 +0530 Subject: [PATCH 7/7] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 10ef903..88b40db 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -numpy==1.24.3 +numpy==1.23.5 opencv-python==4.7.0.72 onnx==1.14.0 insightface==0.7.3