From 9c39bd41f7b2f46a41a5ac091be2c8b5d1bfe658 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 19:45:24 +0200 Subject: [PATCH 1/9] Add FFMpeg hardware acceleration --- core/utils.py | 20 ++++++++++++-------- run.py | 11 ++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/core/utils.py b/core/utils.py index 50aca35..3cf7407 100644 --- a/core/utils.py +++ b/core/utils.py @@ -29,26 +29,30 @@ def detect_fps(input_path): return 30, 30 -def set_fps(input_path, output_path, fps): +def set_fps(input_path, output_path, fps, use_gpu): input_path, output_path = path(input_path), path(output_path) - os.system(f'ffmpeg -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') -def create_video(video_name, fps, output_dir): +def create_video(video_name, fps, output_dir, use_gpu): output_dir = path(output_dir) - os.system(f'ffmpeg -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') -def extract_frames(input_path, output_dir): +def extract_frames(input_path, output_dir, use_gpu): input_path, output_dir = path(input_path), path(output_dir) - os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png"') -def add_audio(output_dir, target_path, video, keep_frames, output_file): +def add_audio(output_dir, target_path, video, keep_frames, output_file, use_gpu): video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - os.system(f'ffmpeg -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"') + hwaccel_option = '-hwaccel cuda' if use_gpu else '' + os.system(f'ffmpeg {hwaccel_option} -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"') if not os.path.isfile(save_to): shutil.move(output_dir + "/output.mp4", save_to) if not keep_frames: diff --git a/run.py b/run.py index 2099fdc..10435b1 100755 --- a/run.py +++ b/run.py @@ -71,6 +71,7 @@ def pre_check(): if not os.path.isfile(model_path): quit('File "inswapper_128.onnx" does not exist!') if '--gpu' in sys.argv: + core.globals.use_gpu = True NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] if len(list(set(core.globals.providers) - set(NVIDIA_PROVIDERS))) == 1: CUDA_VERSION = torch.version.cuda @@ -90,7 +91,7 @@ def pre_check(): def start_processing(): - if args['gpu']: + if core.globals.use_gpu: process_video(args['source_img'], args["frame_paths"]) return frame_paths = args["frame_paths"] @@ -207,12 +208,12 @@ def start(): fps, exact_fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: this_path = output_dir + "/" + video_name + ".mp4" - set_fps(target_path, this_path, 30) + set_fps(target_path, this_path, 30, core.globals.use_gpu) target_path, exact_fps = this_path, 30 else: shutil.copy(target_path, output_dir) status("extracting frames...") - extract_frames(target_path, output_dir) + extract_frames(target_path, output_dir, core.globals.use_gpu) args['frame_paths'] = tuple(sorted( glob.glob(output_dir + "/*.png"), key=lambda x: int(x.split(sep)[-1].replace(".png", "")) @@ -220,9 +221,9 @@ def start(): status("swapping in progress...") start_processing() status("creating video...") - create_video(video_name, exact_fps, output_dir) + create_video(video_name, exact_fps, output_dir, core.globals.use_gpu) status("adding audio...") - add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file']) + add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], core.globals.use_gpu) save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n") status("swap successful!") From b7ce758c991ac8b77a63075f899ae7894fa776d5 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 20:29:37 +0200 Subject: [PATCH 2/9] Fix namespace from core to roop --- roop/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/roop/core.py b/roop/core.py index 3f6ff38..8b53fef 100644 --- a/roop/core.py +++ b/roop/core.py @@ -75,7 +75,7 @@ def pre_check(): if not os.path.isfile(model_path): quit('File "inswapper_128.onnx" does not exist!') if '--gpu' in sys.argv: - core.globals.use_gpu = True + roop.globals.use_gpu = True NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] if len(list(set(roop.globals.providers) - set(NVIDIA_PROVIDERS))) == 1: CUDA_VERSION = torch.version.cuda @@ -100,7 +100,7 @@ def start_processing(): frame_paths = args["frame_paths"] n = len(frame_paths) // (args['cores_count']) # single thread - if core.globals.use_gpu or n < 2: + if roop.globals.use_gpu or n < 2: process_video(args['source_img'], args["frame_paths"]) return # multithread if total frames to cpu cores ratio is greater than 2 @@ -217,12 +217,12 @@ def start(): fps, exact_fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: this_path = output_dir + "/" + video_name + ".mp4" - set_fps(target_path, this_path, 30, core.globals.use_gpu) + set_fps(target_path, this_path, 30, roop.globals.use_gpu) target_path, exact_fps = this_path, 30 else: shutil.copy(target_path, output_dir) status("extracting frames...") - extract_frames(target_path, output_dir, core.globals.use_gpu) + extract_frames(target_path, output_dir, roop.globals.use_gpu) args['frame_paths'] = tuple(sorted( glob.glob(output_dir + "/*.png"), key=lambda x: int(x.split(sep)[-1].replace(".png", "")) @@ -230,9 +230,9 @@ def start(): status("swapping in progress...") start_processing() status("creating video...") - create_video(video_name, exact_fps, output_dir, core.globals.use_gpu) + create_video(video_name, exact_fps, output_dir, roop.globals.use_gpu) status("adding audio...") - add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], core.globals.use_gpu) + add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], roop.globals.use_gpu) save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n") status("swap successful!") From 37a71f661ce654da340665e1791956dd58f737e6 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 20:40:02 +0200 Subject: [PATCH 3/9] Use flags as global --- roop/core.py | 8 ++++---- roop/utils.py | 17 +++++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/roop/core.py b/roop/core.py index 8b53fef..2629c37 100644 --- a/roop/core.py +++ b/roop/core.py @@ -217,12 +217,12 @@ def start(): fps, exact_fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: this_path = output_dir + "/" + video_name + ".mp4" - set_fps(target_path, this_path, 30, roop.globals.use_gpu) + set_fps(target_path, this_path, 30) target_path, exact_fps = this_path, 30 else: shutil.copy(target_path, output_dir) status("extracting frames...") - extract_frames(target_path, output_dir, roop.globals.use_gpu) + extract_frames(target_path, output_dir) args['frame_paths'] = tuple(sorted( glob.glob(output_dir + "/*.png"), key=lambda x: int(x.split(sep)[-1].replace(".png", "")) @@ -230,9 +230,9 @@ def start(): status("swapping in progress...") start_processing() status("creating video...") - create_video(video_name, exact_fps, output_dir, roop.globals.use_gpu) + create_video(video_name, exact_fps, output_dir) status("adding audio...") - add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file'], roop.globals.use_gpu) + add_audio(output_dir, target_path, video_name_full, args['keep_frames'], args['output_file']) save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4" print("\n\nVideo saved as:", save_path, "\n\n") status("swap successful!") diff --git a/roop/utils.py b/roop/utils.py index 69c0aad..479ddb8 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -1,5 +1,6 @@ import os import shutil +import roop.globals sep = "/" if os.name == "nt": @@ -29,29 +30,29 @@ def detect_fps(input_path): return 30, 30 -def set_fps(input_path, output_path, fps, use_gpu): +def set_fps(input_path, output_path, fps): input_path, output_path = path(input_path), path(output_path) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}" -loglevel error') -def create_video(video_name, fps, output_dir, use_gpu): +def create_video(video_name, fps, output_dir): output_dir = path(output_dir) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4" -loglevel error') -def extract_frames(input_path, output_dir, use_gpu): +def extract_frames(input_path, output_dir): input_path, output_dir = path(input_path), path(output_dir) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png" -loglevel error') -def add_audio(output_dir, target_path, video, keep_frames, output_file, use_gpu): +def add_audio(output_dir, target_path, video, keep_frames, output_file): video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - hwaccel_option = '-hwaccel cuda' if use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' os.system(f'ffmpeg {hwaccel_option} -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}" -loglevel error') if not os.path.isfile(save_to): shutil.move(output_dir + "/output.mp4", save_to) From 74783142e6fdb484d9d021e51e07fc0978e21fb5 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 20:53:01 +0200 Subject: [PATCH 4/9] Little ffmpeg refactor --- roop/globals.py | 1 + roop/utils.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/roop/globals.py b/roop/globals.py index b237e8a..9972193 100644 --- a/roop/globals.py +++ b/roop/globals.py @@ -2,6 +2,7 @@ import onnxruntime use_gpu = False all_faces = False +debug = False providers = onnxruntime.get_available_providers() if 'TensorrtExecutionProvider' in providers: diff --git a/roop/utils.py b/roop/utils.py index 479ddb8..0aed392 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -29,31 +29,36 @@ def detect_fps(input_path): pass return 30, 30 +def get_ffmpeg_cmd(): + hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' + log_level = '-loglevel error' if roop.globals.debug else '-loglevel quiet' + + return f'ffmpeg {hwaccel_option} {log_level}' def set_fps(input_path, output_path, fps): + basic_command = get_ffmpeg_cmd() input_path, output_path = path(input_path), path(output_path) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}" -loglevel error') + os.system(f'{basic_command} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): + basic_command = get_ffmpeg_cmd() output_dir = path(output_dir) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4" -loglevel error') + os.system(f'{basic_command} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): + basic_command = get_ffmpeg_cmd() input_path, output_dir = path(input_path), path(output_dir) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png" -loglevel error') + os.system(f'{basic_command} -i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file): + basic_command = get_ffmpeg_cmd() video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - os.system(f'ffmpeg {hwaccel_option} -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}" -loglevel error') + os.system(f'{basic_command} -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"') if not os.path.isfile(save_to): shutil.move(output_dir + "/output.mp4", save_to) if not keep_frames: From 29d65d8e794d3403e1af60a32de92f6644d527a0 Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 21:12:25 +0200 Subject: [PATCH 5/9] Centraliza ffmpeg run command --- roop/globals.py | 2 +- roop/utils.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/roop/globals.py b/roop/globals.py index 9972193..d0f621a 100644 --- a/roop/globals.py +++ b/roop/globals.py @@ -2,7 +2,7 @@ import onnxruntime use_gpu = False all_faces = False -debug = False +log_level = 'quiet' providers = onnxruntime.get_available_providers() if 'TensorrtExecutionProvider' in providers: diff --git a/roop/utils.py b/roop/utils.py index 0aed392..a92f7fc 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -29,28 +29,28 @@ def detect_fps(input_path): pass return 30, 30 -def get_ffmpeg_cmd(): +def run_ffmpeg(args): hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - log_level = '-loglevel error' if roop.globals.debug else '-loglevel quiet' + log_level = '-loglevel {roop.globals.log_level}' - return f'ffmpeg {hwaccel_option} {log_level}' + os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') def set_fps(input_path, output_path, fps): basic_command = get_ffmpeg_cmd() input_path, output_path = path(input_path), path(output_path) - os.system(f'{basic_command} -i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') + run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): basic_command = get_ffmpeg_cmd() output_dir = path(output_dir) - os.system(f'{basic_command} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') + run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): basic_command = get_ffmpeg_cmd() input_path, output_dir = path(input_path), path(output_dir) - os.system(f'{basic_command} -i "{input_path}" "{output_dir}{sep}%04d.png"') + run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file): @@ -58,7 +58,7 @@ def add_audio(output_dir, target_path, video, keep_frames, output_file): video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) - os.system(f'{basic_command} -i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"') + run_ffmpeg(f'-i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"') if not os.path.isfile(save_to): shutil.move(output_dir + "/output.mp4", save_to) if not keep_frames: From 338ea8f50b25991b725d3cde97bcdd728cfc023d Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 21:17:22 +0200 Subject: [PATCH 6/9] Removed unused code --- roop/utils.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/roop/utils.py b/roop/utils.py index a92f7fc..3325864 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -36,25 +36,21 @@ def run_ffmpeg(args): os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') def set_fps(input_path, output_path, fps): - basic_command = get_ffmpeg_cmd() input_path, output_path = path(input_path), path(output_path) run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') def create_video(video_name, fps, output_dir): - basic_command = get_ffmpeg_cmd() output_dir = path(output_dir) run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): - basic_command = get_ffmpeg_cmd() input_path, output_dir = path(input_path), path(output_dir) run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file): - basic_command = get_ffmpeg_cmd() video_name = os.path.splitext(video)[0] save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to_ff, output_dir_ff = path(save_to), path(output_dir) From 107259474fab77cef10b821832ba820ee1b71d4c Mon Sep 17 00:00:00 2001 From: Jose Manuel Date: Fri, 2 Jun 2023 21:19:28 +0200 Subject: [PATCH 7/9] Fix log level --- roop/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roop/utils.py b/roop/utils.py index 3325864..670385a 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -31,7 +31,7 @@ def detect_fps(input_path): def run_ffmpeg(args): hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' - log_level = '-loglevel {roop.globals.log_level}' + log_level = f'-loglevel {roop.globals.log_level}' os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') From 49d78c138f21f2d3ec74f06dd08d7b4cd85aa517 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Fri, 2 Jun 2023 22:03:45 +0200 Subject: [PATCH 8/9] Introduce --cpu=amd|nvidia --- roop/core.py | 43 ++++++++++++++++++++++--------------------- roop/globals.py | 4 ++-- roop/utils.py | 4 +++- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/roop/core.py b/roop/core.py index 4a7b9b8..371191d 100644 --- a/roop/core.py +++ b/roop/core.py @@ -36,7 +36,7 @@ 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') parser.add_argument('-o', '--output', help='save output to this file', dest='output_file') -parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False) +parser.add_argument('--gpu', help='choice your gpu vendor', dest='gpu', choices=['amd', 'nvidia']) parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', 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('--max-memory', help='maximum amount of RAM in GB to be used', type=int) @@ -46,7 +46,10 @@ parser.add_argument('--all-faces', help='swap all faces in frame', dest='all_fac for name, value in vars(parser.parse_args()).items(): args[name] = value -if '--all-faces' in sys.argv or '-a' in sys.argv: +if 'gpu' in args: + roop.globals.gpu = args['gpu'] + +if 'all-faces' in args: roop.globals.all_faces = True sep = "/" @@ -74,33 +77,31 @@ def pre_check(): model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx') if not os.path.isfile(model_path): quit('File "inswapper_128.onnx" does not exist!') - if '--gpu' in sys.argv: - roop.globals.use_gpu = True - NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] - if len(list(set(roop.globals.providers) - set(NVIDIA_PROVIDERS))) == 1: - CUDA_VERSION = torch.version.cuda - CUDNN_VERSION = torch.backends.cudnn.version() - if not torch.cuda.is_available() or not CUDA_VERSION: - quit("You are using --gpu flag but CUDA isn't available or properly installed on your system.") - if CUDA_VERSION > '11.8': - quit(f"CUDA version {CUDA_VERSION} is not supported - please downgrade to 11.8") - if CUDA_VERSION < '11.4': - quit(f"CUDA version {CUDA_VERSION} is not supported - please upgrade to 11.8") - if CUDNN_VERSION < 8220: - quit(f"CUDNN version {CUDNN_VERSION} is not supported - please upgrade to 8.9.1") - if CUDNN_VERSION > 8910: - quit(f"CUDNN version {CUDNN_VERSION} is not supported - please downgrade to 8.9.1") + if roop.globals.gpu == 'amd': + if 'ROCMExecutionProvider' not in roop.globals.providers: + quit("You are using --gpu=amd flag but ROCM isn't available or properly installed on your system.") + if roop.globals.gpu == 'nvidia': + CUDA_VERSION = torch.version.cuda + CUDNN_VERSION = torch.backends.cudnn.version() + if not torch.cuda.is_available() or not CUDA_VERSION: + quit("You are using --gpu=nvidia flag but CUDA isn't available or properly installed on your system.") + if CUDA_VERSION > '11.8': + quit(f"CUDA version {CUDA_VERSION} is not supported - please downgrade to 11.8") + if CUDA_VERSION < '11.4': + quit(f"CUDA version {CUDA_VERSION} is not supported - please upgrade to 11.8") + if CUDNN_VERSION < 8220: + quit(f"CUDNN version {CUDNN_VERSION} is not supported - please upgrade to 8.9.1") + if CUDNN_VERSION > 8910: + quit(f"CUDNN version {CUDNN_VERSION} is not supported - please downgrade to 8.9.1") else: roop.globals.providers = ['CPUExecutionProvider'] - if '--all-faces' in sys.argv or '-a' in sys.argv: - roop.globals.all_faces = True def start_processing(): frame_paths = args["frame_paths"] n = len(frame_paths) // (args['cores_count']) # single thread - if roop.globals.use_gpu or n < 2: + if roop.globals.gpu == 'amd' or roop.globals.gpu == 'nvidia' or n < 2: process_video(args['source_img'], args["frame_paths"]) return # multithread if total frames to cpu cores ratio is greater than 2 diff --git a/roop/globals.py b/roop/globals.py index d0f621a..34adafd 100644 --- a/roop/globals.py +++ b/roop/globals.py @@ -1,8 +1,8 @@ import onnxruntime -use_gpu = False +gpu = None all_faces = False -log_level = 'quiet' +log_level = 'error' providers = onnxruntime.get_available_providers() if 'TensorrtExecutionProvider' in providers: diff --git a/roop/utils.py b/roop/utils.py index 670385a..34976ba 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -29,12 +29,14 @@ def detect_fps(input_path): pass return 30, 30 + def run_ffmpeg(args): - hwaccel_option = '-hwaccel cuda' if roop.globals.use_gpu else '' + hwaccel_option = '-hwaccel cuda' if roop.globals.gpu == 'nvidia' else '' log_level = f'-loglevel {roop.globals.log_level}' os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') + def set_fps(input_path, output_path, fps): input_path, output_path = path(input_path), path(output_path) run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') From 6bd3724443b51017b38dbca546bd4cf358f437aa Mon Sep 17 00:00:00 2001 From: henryruhs Date: Fri, 2 Jun 2023 22:18:16 +0200 Subject: [PATCH 9/9] Limit "hwaccel_option" to non-audio task as of codec issues --- roop/utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/roop/utils.py b/roop/utils.py index 34976ba..a9b0d05 100644 --- a/roop/utils.py +++ b/roop/utils.py @@ -31,10 +31,10 @@ def detect_fps(input_path): def run_ffmpeg(args): - hwaccel_option = '-hwaccel cuda' if roop.globals.gpu == 'nvidia' else '' + log_level = f'-loglevel {roop.globals.log_level}' - os.system(f'ffmpeg {hwaccel_option} {log_level} {args}') + os.system(f'ffmpeg {log_level} {args}') def set_fps(input_path, output_path, fps): @@ -43,13 +43,15 @@ def set_fps(input_path, output_path, fps): def create_video(video_name, fps, output_dir): + hwaccel_option = '-hwaccel cuda' if roop.globals.gpu == 'nvidia' else '' output_dir = path(output_dir) - run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') + run_ffmpeg(f'{hwaccel_option} -framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): + hwaccel_option = '-hwaccel cuda' if roop.globals.gpu == 'nvidia' else '' input_path, output_dir = path(input_path), path(output_dir) - run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"') + run_ffmpeg(f' {hwaccel_option} -i "{input_path}" "{output_dir}{sep}%04d.png"') def add_audio(output_dir, target_path, video, keep_frames, output_file):