From f48f0b742e1345e19c67d37426ae6928dda39fb3 Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:50:58 +0800 Subject: [PATCH 01/10] 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 3daf82b3f8db3edd42b769ae8926f73c0523fbdc Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:54:52 +0800 Subject: [PATCH 02/10] 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 7c385cc4f4258549ca3da9d2960aea13a3293d10 Mon Sep 17 00:00:00 2001 From: Pikachu~~~ Date: Thu, 1 Jun 2023 00:55:36 +0800 Subject: [PATCH 03/10] 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 91b7301f32c9c8dd331748c5c558c51c4a115f94 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 21:03:44 +0200 Subject: [PATCH 04/10] 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 5aa91e91574de5b4eead8922f52837a70c315ce5 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 21:17:01 +0200 Subject: [PATCH 05/10] 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 7d42dbc4bc0f49e8cc9a3700cec6383fb0b6ad43 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 21:30:49 +0200 Subject: [PATCH 06/10] Remove processing time as of progress bar --- run.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/run.py b/run.py index 85592ea..f4e79e0 100755 --- a/run.py +++ b/run.py @@ -89,12 +89,8 @@ def pre_check(): def start_processing(): - start_time = time.time() if args['gpu']: process_video(args['source_img'], args["frame_paths"]) - end_time = time.time() - print(flush=True) - print(f"Processing time: {end_time - start_time:.2f} seconds", flush=True) return frame_paths = args["frame_paths"] n = len(frame_paths)//(args['cores_count']) @@ -106,9 +102,6 @@ def start_processing(): p.get() pool.close() pool.join() - end_time = time.time() - print(flush=True) - print(f"Processing time: {end_time - start_time:.2f} seconds", flush=True) def preview_image(image_path): From a66dd5cde1a8050cada0fdba9513c0c97026ee04 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 22:37:24 +0200 Subject: [PATCH 07/10] Update dependencies --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2b6968f..8e6cbf1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,5 @@ pillow==9.5.0 torch==2.0.1 onnxruntime-gpu==1.15.0 opennsfw2==0.10.2 -protobuf==3.20.2 +protobuf==4.23.2 tqdm==4.65.0 From bf100f4974222b3f510b1be6da756ea7fff1a664 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 23:04:37 +0200 Subject: [PATCH 08/10] Improve speed --- run.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/run.py b/run.py index f4e79e0..2227c2c 100755 --- a/run.py +++ b/run.py @@ -169,7 +169,6 @@ def status(string): def start(): - print("DON'T WORRY. IT'S NOT STUCK/CRASHED.\n" * 5) if not args['source_img'] or not os.path.isfile(args['source_img']): print("\n[WARNING] Please select an image containing a face.") return @@ -192,7 +191,7 @@ def start(): process_img(args['source_img'], target_path, args['output_file']) status("swap successful!") return - seconds, probabilities = predict_video_frames(video_path=args['target_path'], frame_interval=50) + seconds, probabilities = predict_video_frames(video_path=args['target_path'], frame_interval=100) if any(probability > 0.7 for probability in probabilities): quit() video_name_full = target_path.split("/")[-1] From 8509160850b2852d20895397652ad340dacfc1f0 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 23:05:58 +0200 Subject: [PATCH 09/10] Remove import --- run.py | 1 - 1 file changed, 1 deletion(-) diff --git a/run.py b/run.py index 2227c2c..6b23ee7 100755 --- a/run.py +++ b/run.py @@ -3,7 +3,6 @@ import platform import signal import sys -import time import shutil import glob import argparse From 442c070229c167b00bd0ac9151289abdca724fa2 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Wed, 31 May 2023 23:27:26 +0200 Subject: [PATCH 10/10] Need for Python 3.9+ --- .github/workflows/ci.yml | 4 ++-- run.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d6de8c..49ca83c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,9 +8,9 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 - - name: Set up Python 3.8 + - name: Set up Python 3.9 uses: actions/setup-python@v2 with: - python-version: 3.8 + python-version: 3.9 - run: pip install flake8 - run: flake8 run.py core diff --git a/run.py b/run.py index 6b23ee7..7eb9fed 100755 --- a/run.py +++ b/run.py @@ -62,8 +62,8 @@ def limit_resources(): def pre_check(): - if sys.version_info < (3, 8): - quit('Python version is not supported - please upgrade to 3.8 or higher') + if sys.version_info < (3, 9): + quit('Python version is not supported - please upgrade to 3.9 or higher') if not shutil.which('ffmpeg'): quit('ffmpeg is not installed!') model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'inswapper_128.onnx')