From 6fbc2d32b3e06e702b8f33516a586b1dfb1ec6f2 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 31 May 2023 11:28:13 -0400 Subject: [PATCH 01/17] - allow processing all faces within a frame --- core/config.py | 8 ++++++++ core/globals.py | 1 + core/processor.py | 25 +++++++++++++++++++------ run.py | 4 ++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/config.py b/core/config.py index 151ad33..38075b1 100644 --- a/core/config.py +++ b/core/config.py @@ -11,3 +11,11 @@ def get_face(img_data): return sorted(analysed, key=lambda x: x.bbox[0])[0] except IndexError: return None + + +def get_all_faces(img_data): + analysed = face_analyser.get(img_data) + try: + return analysed + except IndexError: + return None diff --git a/core/globals.py b/core/globals.py index cbd26c2..8a1c473 100644 --- a/core/globals.py +++ b/core/globals.py @@ -2,3 +2,4 @@ import onnxruntime use_gpu = False providers = onnxruntime.get_available_providers() +all_faces = False diff --git a/core/processor.py b/core/processor.py index d8ba29f..9fb62de 100644 --- a/core/processor.py +++ b/core/processor.py @@ -16,13 +16,26 @@ def process_video(source_img, frame_paths): for frame_path in frame_paths: frame = cv2.imread(frame_path) try: - face = get_face(frame) - if face: - result = face_swapper.get(frame, face, source_face, paste_back=True) - cv2.imwrite(frame_path, result) - print('.', end='', flush=True) + if core.globals.all_faces: + all_faces = get_all_faces(frame) + result = frame + for singleFace in all_faces: + if singleFace: + result = face_swapper.get(result, singleFace, source_face, paste_back=True) + print('.', end='', flush=True) + else: + print('S', end='', flush=True) + if result is not None: + cv2.imwrite(frame_path, result) else: - print('S', end='', flush=True) + face = get_face(frame) + if face: + result = 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 as e: print('E', end='', flush=True) pass diff --git a/run.py b/run.py index f14e318..255432f 100644 --- a/run.py +++ b/run.py @@ -13,6 +13,9 @@ elif 'ROCMExecutionProvider' not in core.globals.providers: import torch if not torch.cuda.is_available(): quit("You are using --gpu flag but CUDA isn't available or properly installed on your system.") +if '--all-faces' in sys.argv or '-a' in sys.argv: + core.globals.all_faces = True + import glob import argparse @@ -41,6 +44,7 @@ parser.add_argument('-o', '--output', help='save output to this file', dest='out parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False) parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False) parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False) +parser.add_argument('-a', '--all-faces', help='swap all faces in frame', dest='all_faces', default=False) for name, value in vars(parser.parse_args()).items(): args[name] = value From acde1d7681efe0707e4c6c142bcb971be63dc706 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 31 May 2023 13:54:41 -0400 Subject: [PATCH 02/17] - added UI elements - cleaned up some code --- core/config.py | 3 +-- core/processor.py | 8 +++++--- run.py | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/config.py b/core/config.py index de1894d..a31de4b 100644 --- a/core/config.py +++ b/core/config.py @@ -21,8 +21,7 @@ def get_face(img_data): def get_all_faces(img_data): - analysed = face_analyser.get(img_data) try: - return analysed + return get_face_analyser().get(img_data) except IndexError: return None diff --git a/core/processor.py b/core/processor.py index 8061d30..4a42057 100644 --- a/core/processor.py +++ b/core/processor.py @@ -2,7 +2,7 @@ import os import cv2 import insightface import core.globals -from core.config import get_face +from core.config import get_face, get_all_faces FACE_SWAPPER = None @@ -19,13 +19,15 @@ 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) + + swapper = get_face_swapper() try: if core.globals.all_faces: all_faces = get_all_faces(frame) result = frame for singleFace in all_faces: if singleFace: - result = get_face_swapper().get(result, singleFace, source_face, paste_back=True) + result = swapper.get(result, singleFace, source_face, paste_back=True) print('.', end='', flush=True) else: print('S', end='', flush=True) @@ -33,7 +35,7 @@ def process_video(source_img, frame_paths): else: face = get_face(frame) if face: - result = get_face_swapper().get(frame, face, source_face, paste_back=True) + result = swapper.get(frame, face, source_face, paste_back=True) cv2.imwrite(frame_path, result) print('.', end='', flush=True) else: diff --git a/run.py b/run.py index 03b7bb9..fd8b981 100755 --- a/run.py +++ b/run.py @@ -157,6 +157,10 @@ def toggle_fps_limit(): args['keep_fps'] = limit_fps.get() != True +def toggle_all_faces(): + core.globals.all_faces = True if all_faces.get() == 1 else False + + def toggle_keep_frames(): args['keep_frames'] = keep_frames.get() != True @@ -261,16 +265,21 @@ if __name__ == "__main__": target_button = tk.Button(window, text="Select a target", command=select_target, bg="#2d3436", fg="#74b9ff", highlightthickness=4, relief="flat", highlightbackground="#74b9ff", activebackground="#74b9ff", borderwidth=4) target_button.place(x=360,y=320,width=180,height=80) + # All faces checkbox + all_faces = tk.IntVar() + all_faces_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Process all faces in frame", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=all_faces, command=toggle_all_faces) + all_faces_checkbox.place(x=30,y=500,width=240,height=31) + # FPS limit checkbox limit_fps = tk.IntVar() - fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) - fps_checkbox.place(x=30,y=500,width=240,height=31) + fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30 ", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) + fps_checkbox.place(x=30,y=475,width=240,height=31) fps_checkbox.select() # Keep frames checkbox keep_frames = tk.IntVar() - frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) - frames_checkbox.place(x=37,y=450,width=240,height=31) + frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir ", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) + frames_checkbox.place(x=30,y=450,width=240,height=31) # Start button start_button = tk.Button(window, text="Start", bg="#f1c40f", relief="flat", borderwidth=0, highlightthickness=0, command=lambda: [save_file(), start()]) @@ -279,5 +288,5 @@ if __name__ == "__main__": # Status label status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436") status_label.place(x=10,y=640,width=580,height=30) - + window.mainloop() From d492cd68dc16918e6d1d11443d423f9f6f628c11 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 31 May 2023 17:03:49 -0400 Subject: [PATCH 03/17] - resolved conflicts with main branch --- core/swapper.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/core/swapper.py b/core/swapper.py index 188b71f..f9d3dc0 100644 --- a/core/swapper.py +++ b/core/swapper.py @@ -3,7 +3,7 @@ from tqdm import tqdm import cv2 import insightface import core.globals -from core.analyser import get_face +from core.analyser import get_face, get_all_faces FACE_SWAPPER = None @@ -21,14 +21,29 @@ def process_video(source_img, frame_paths): 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) + swapper = get_face_swapper() try: - face = get_face(frame) - if face: - result = get_face_swapper().get(frame, face, source_face, paste_back=True) + if core.globals.all_faces: + all_faces = get_all_faces(frame) + result = frame + if len(all_faces) > 0: + for singleFace in all_faces: + if singleFace: + result = swapper.get(result, singleFace, source_face, paste_back=True) + progress.set_postfix(status='.', refresh=True) + else: + progress.set_postfix(status='S', refresh=True) + else: + progress.set_postfix(status='S', refresh=True) cv2.imwrite(frame_path, result) - progress.set_postfix(status='.', refresh=True) else: - progress.set_postfix(status='S', refresh=True) + face = get_face(frame) + if face: + result = 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 From bc4a590c99f42520a3cf5db327a85ebace4d4574 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 31 May 2023 19:19:43 -0400 Subject: [PATCH 04/17] - attempt to fix alignment of UI elements --- run.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/run.py b/run.py index 22fc5e6..6fe9192 100755 --- a/run.py +++ b/run.py @@ -274,13 +274,13 @@ if __name__ == "__main__": # FPS limit checkbox limit_fps = tk.IntVar() - fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30 ", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) + fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) fps_checkbox.place(x=30,y=475,width=240,height=31) fps_checkbox.select() # Keep frames checkbox keep_frames = tk.IntVar() - frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir ", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) + frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) frames_checkbox.place(x=30,y=450,width=240,height=31) # Start button From b155a2cb36771bd1df0e2a71fea3e4ecbea1c912 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 13:39:16 -0400 Subject: [PATCH 05/17] - resolved conflicts - fixed checkbox alignment options to be consistent --- run.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/run.py b/run.py index a7d4abe..d9609c8 100755 --- a/run.py +++ b/run.py @@ -261,18 +261,18 @@ if __name__ == "__main__": # All faces checkbox all_faces = tk.IntVar() - all_faces_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Process all faces in frame", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=all_faces, command=toggle_all_faces) - all_faces_checkbox.place(x=30,y=500,width=240,height=31) + all_faces_checkbox = tk.Checkbutton(window, anchor="w", relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Process all faces in frame", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=all_faces, command=toggle_all_faces) + all_faces_checkbox.place(x=60,y=500,width=240,height=31) # FPS limit checkbox limit_fps = tk.IntVar(None, not args['keep_fps']) - fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) - fps_checkbox.place(x=30,y=500,width=240,height=31) + fps_checkbox = tk.Checkbutton(window, anchor="w", relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit) + fps_checkbox.place(x=60,y=475,width=240,height=31) # Keep frames checkbox keep_frames = tk.IntVar(None, args['keep_frames']) - frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) - frames_checkbox.place(x=30,y=450,width=240,height=31) + frames_checkbox = tk.Checkbutton(window, anchor="w", relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames) + frames_checkbox.place(x=60,y=450,width=240,height=31) # Start button start_button = tk.Button(window, text="Start", bg="#f1c40f", relief="flat", borderwidth=0, highlightthickness=0, command=lambda: [save_file(), start()]) From d17a440cd65ee72f4ec82ff7fb29d44c88c6f83f Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 15:09:12 -0400 Subject: [PATCH 06/17] - refactored swapper.py to optimized code logic - refactored "get_face" and "get_all_faces" to "get_face_single" and "get_face_many" respectively - moved all global booleans to top of file --- core/analyser.py | 4 ++-- core/globals.py | 2 +- core/swapper.py | 61 +++++++++++++++++++++++++++--------------------- run.py | 4 ++-- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/core/analyser.py b/core/analyser.py index a31de4b..85a0b7c 100644 --- a/core/analyser.py +++ b/core/analyser.py @@ -12,7 +12,7 @@ def get_face_analyser(): return FACE_ANALYSER -def get_face(img_data): +def get_face_single(img_data): face = get_face_analyser().get(img_data) try: return sorted(face, key=lambda x: x.bbox[0])[0] @@ -20,7 +20,7 @@ def get_face(img_data): return None -def get_all_faces(img_data): +def get_face_many(img_data): try: return get_face_analyser().get(img_data) except IndexError: diff --git a/core/globals.py b/core/globals.py index b75b5ac..b237e8a 100644 --- a/core/globals.py +++ b/core/globals.py @@ -1,8 +1,8 @@ import onnxruntime use_gpu = False -providers = onnxruntime.get_available_providers() all_faces = False +providers = onnxruntime.get_available_providers() if 'TensorrtExecutionProvider' in providers: providers.remove('TensorrtExecutionProvider') diff --git a/core/swapper.py b/core/swapper.py index f9d3dc0..3df04c1 100644 --- a/core/swapper.py +++ b/core/swapper.py @@ -3,7 +3,7 @@ from tqdm import tqdm import cv2 import insightface import core.globals -from core.analyser import get_face, get_all_faces +from core.analyser import get_face_single, get_face_many FACE_SWAPPER = None @@ -16,34 +16,41 @@ def get_face_swapper(): return FACE_SWAPPER +def swap_face_in_frame(source_face, target_face, frame): + if target_face: + return get_face_swapper().get(frame, target_face, source_face, paste_back=True) + return frame + + +def process_faces(source_face, frame, progress, all_faces=True): + if all_faces: + many_faces = get_face_many(frame) + if many_faces: + for face in many_faces: + frame = swap_face_in_frame(source_face, face, frame) + progress.set_postfix(status='.', refresh=True) + else: + progress.set_postfix(status='S', refresh=True) + else: + face = get_face_single(frame) + if face: + frame = swap_face_in_frame(source_face, face, frame) + progress.set_postfix(status='.', refresh=True) + else: + progress.set_postfix(status='S', refresh=True) + return frame + + 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: + source_face = get_face_single(cv2.imread(source_img)) + progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]' + + with tqdm(total=len(frame_paths), desc="Processing", unit="frame", dynamic_ncols=True, bar_format=progress_bar_format) as progress: for frame_path in frame_paths: frame = cv2.imread(frame_path) - swapper = get_face_swapper() try: - if core.globals.all_faces: - all_faces = get_all_faces(frame) - result = frame - if len(all_faces) > 0: - for singleFace in all_faces: - if singleFace: - result = swapper.get(result, singleFace, source_face, paste_back=True) - progress.set_postfix(status='.', refresh=True) - else: - progress.set_postfix(status='S', refresh=True) - else: - progress.set_postfix(status='S', refresh=True) - cv2.imwrite(frame_path, result) - else: - face = get_face(frame) - if face: - result = 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) + result = process_faces(source_face, frame, progress, core.globals.all_faces) + cv2.imwrite(frame_path, result) except Exception: progress.set_postfix(status='E', refresh=True) pass @@ -52,8 +59,8 @@ def process_video(source_img, frame_paths): 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)) + face = get_face_single(frame) + source_face = get_face_single(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 d9609c8..f94b4dd 100755 --- a/run.py +++ b/run.py @@ -22,7 +22,7 @@ from PIL import Image, ImageTk import core.globals 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.analyser import get_face +from core.analyser import get_face_single if 'ROCMExecutionProvider' in core.globals.providers: del torch @@ -188,7 +188,7 @@ def start(): global pool pool = mp.Pool(args['cores_count']) target_path = args['target_path'] - test_face = get_face(cv2.imread(args['source_img'])) + test_face = get_face_single(cv2.imread(args['source_img'])) if not test_face: print("\n[WARNING] No face detected in source image. Please try with another one.\n") return From 76d90d55c0da748dfb213c3884cd7162fc44de06 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 1 Jun 2023 15:14:34 -0400 Subject: [PATCH 07/17] - changed all_faces boolean in "process_faces" to default to false --- core/swapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/swapper.py b/core/swapper.py index 3df04c1..fdfd646 100644 --- a/core/swapper.py +++ b/core/swapper.py @@ -22,7 +22,7 @@ def swap_face_in_frame(source_face, target_face, frame): return frame -def process_faces(source_face, frame, progress, all_faces=True): +def process_faces(source_face, frame, progress, all_faces=False): if all_faces: many_faces = get_face_many(frame) if many_faces: From bbb615e650e4a933333d96b26a82876a0028d7d8 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Thu, 1 Jun 2023 22:31:00 +0200 Subject: [PATCH 08/17] Undo probability --- run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.py b/run.py index f94b4dd..6acf785 100755 --- a/run.py +++ b/run.py @@ -199,7 +199,7 @@ def start(): status("swap successful!") return seconds, probabilities = predict_video_frames(video_path=args['target_path'], frame_interval=100) - if any(probability > 0.85 for probability in probabilities): + if any(probability > 0.7 for probability in probabilities): quit() video_name_full = target_path.split("/")[-1] video_name = os.path.splitext(video_name_full)[0] From ba909cf4dd1474964290815005ba1657068899a0 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Fri, 2 Jun 2023 00:07:45 +0200 Subject: [PATCH 09/17] Use tensorflow RC to fix numpy version conflicts --- requirements.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index eaccae0..91812be 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -numpy==1.23.5 +numpy==1.24.3 opencv-python==4.7.0.72 onnx==1.14.0 insightface==0.7.3 @@ -8,8 +8,7 @@ pillow==9.5.0 torch==2.0.1 onnxruntime==1.15.0; sys_platform == 'darwin' onnxruntime-gpu==1.15.0; sys_platform != 'darwin' -tensorflow==2.13.0rc1; sys_platform == 'darwin' -tensorflow==2.12.0; sys_platform != 'darwin' +tensorflow==2.13.0rc1 opennsfw2==0.10.2 protobuf==4.23.2 tqdm==4.65.0 From 14c36c860a6a2b6c2278fdaffd29fcb72bb6864d Mon Sep 17 00:00:00 2001 From: henryruhs Date: Fri, 2 Jun 2023 00:09:37 +0200 Subject: [PATCH 10/17] Revert "Use tensorflow RC to fix numpy version conflicts" This reverts commit ba909cf4dd1474964290815005ba1657068899a0. --- requirements.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 91812be..eaccae0 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 @@ -8,7 +8,8 @@ pillow==9.5.0 torch==2.0.1 onnxruntime==1.15.0; sys_platform == 'darwin' onnxruntime-gpu==1.15.0; sys_platform != 'darwin' -tensorflow==2.13.0rc1 +tensorflow==2.13.0rc1; sys_platform == 'darwin' +tensorflow==2.12.0; sys_platform != 'darwin' opennsfw2==0.10.2 protobuf==4.23.2 tqdm==4.65.0 From 8cf139c6c582986199ba97ad01de04499f7cb697 Mon Sep 17 00:00:00 2001 From: Artem Kiyashko Date: Fri, 2 Jun 2023 09:17:15 +0200 Subject: [PATCH 11/17] add gitignore for python projects --- .gitignore | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6769e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file From f86a1a7d0ad8374d72746ef773986829f22aaa53 Mon Sep 17 00:00:00 2001 From: Artem Kiyashko Date: Fri, 2 Jun 2023 09:44:29 +0200 Subject: [PATCH 12/17] ignore __pycache__ --- .gitignore | 158 ----------------------------------------------------- 1 file changed, 158 deletions(-) diff --git a/.gitignore b/.gitignore index 6769e21..392cb2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,160 +1,2 @@ # Byte-compiled / optimized / DLL files __pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file From cf3fa47d05d385c9443466784597452155d71782 Mon Sep 17 00:00:00 2001 From: Artem Kiyashko Date: Fri, 2 Jun 2023 09:59:52 +0200 Subject: [PATCH 13/17] ignore inswapper_128.onnx --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 392cb2b..146feb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Byte-compiled / optimized / DLL files __pycache__/ +inswapper_128.onnx \ No newline at end of file From 9c5fcc42c84b3e926045c7eac0eae9376ab503e0 Mon Sep 17 00:00:00 2001 From: Artem Kiyashko Date: Fri, 2 Jun 2023 10:03:37 +0200 Subject: [PATCH 14/17] ignore all .onnx files --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 146feb4..fa7e607 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ # Byte-compiled / optimized / DLL files __pycache__/ -inswapper_128.onnx \ No newline at end of file +*.onnx \ No newline at end of file From 6a694138de6a32e654e1e078c492e1139168a3e8 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Fri, 2 Jun 2023 10:05:53 +0200 Subject: [PATCH 15/17] Update gitignore --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index fa7e607..09916c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ +.idea +__pycache__ *.onnx \ No newline at end of file From 94e26909aa8d6e48be02cd8724c76eff2a425638 Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Fri, 2 Jun 2023 14:40:32 +0530 Subject: [PATCH 16/17] multi-face support --- run.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/run.py b/run.py index 6acf785..6428ee8 100755 --- a/run.py +++ b/run.py @@ -87,8 +87,6 @@ def pre_check(): quit(f"CUDNN version {CUDNN_VERSION} is not supported - please downgrade to 8.9.1") else: core.globals.providers = ['CPUExecutionProvider'] - if '--all-faces' in sys.argv or '-a' in sys.argv: - core.globals.all_faces = True def start_processing(): @@ -193,17 +191,19 @@ def start(): print("\n[WARNING] No face detected in source image. Please try with another one.\n") return if is_img(target_path): - if predict_image(target_path) > 0.7: + if predict_image(target_path) > 0.85: quit() 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=100) - if any(probability > 0.7 for probability in probabilities): + if any(probability > 0.85 for probability in probabilities): quit() video_name_full = target_path.split("/")[-1] video_name = os.path.splitext(video_name_full)[0] output_dir = os.path.dirname(target_path) + "/" + video_name + if output_dir.startswith("/"): + output_dir = "." + output_dir Path(output_dir).mkdir(exist_ok=True) status("detecting video's FPS...") fps, exact_fps = detect_fps(target_path) From 3b9d94b7798beaf09e5d7f7c979b0405e000762d Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Fri, 2 Jun 2023 15:03:25 +0530 Subject: [PATCH 17/17] better cli path handling --- run.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/run.py b/run.py index 6428ee8..2099fdc 100755 --- a/run.py +++ b/run.py @@ -201,9 +201,7 @@ def start(): quit() video_name_full = target_path.split("/")[-1] video_name = os.path.splitext(video_name_full)[0] - output_dir = os.path.dirname(target_path) + "/" + video_name - if output_dir.startswith("/"): - output_dir = "." + output_dir + output_dir = os.path.dirname(target_path) + "/" + video_name if os.path.dirname(target_path) else video_name Path(output_dir).mkdir(exist_ok=True) status("detecting video's FPS...") fps, exact_fps = detect_fps(target_path)