Merge branch 'next' of https://github.com/K1llMan/roop into next

This commit is contained in:
K1llM@n 2023-06-03 00:26:39 +03:00
commit c5012e0bea
3 changed files with 39 additions and 26 deletions

View File

@ -37,7 +37,7 @@ parser = argparse.ArgumentParser()
parser.add_argument('-f', '--face', help='use this face', dest='source_img') 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('-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('-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-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('--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) parser.add_argument('--max-memory', help='maximum amount of RAM in GB to be used', type=int)
@ -47,7 +47,10 @@ parser.add_argument('--all-faces', help='swap all faces in frame', dest='all_fac
for name, value in vars(parser.parse_args()).items(): for name, value in vars(parser.parse_args()).items():
args[name] = value 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 roop.globals.all_faces = True
sep = "/" sep = "/"
@ -75,32 +78,31 @@ def pre_check():
model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx') model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx')
if not os.path.isfile(model_path): if not os.path.isfile(model_path):
quit('File "inswapper_128.onnx" does not exist!') quit('File "inswapper_128.onnx" does not exist!')
if '--gpu' in sys.argv: if roop.globals.gpu == 'amd':
NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] if 'ROCMExecutionProvider' not in roop.globals.providers:
if len(list(set(roop.globals.providers) - set(NVIDIA_PROVIDERS))) == 1: quit("You are using --gpu=amd flag but ROCM isn't available or properly installed on your system.")
CUDA_VERSION = torch.version.cuda if roop.globals.gpu == 'nvidia':
CUDNN_VERSION = torch.backends.cudnn.version() CUDA_VERSION = torch.version.cuda
if not torch.cuda.is_available() or not CUDA_VERSION: CUDNN_VERSION = torch.backends.cudnn.version()
quit("You are using --gpu flag but CUDA isn't available or properly installed on your system.") if not torch.cuda.is_available() or not CUDA_VERSION:
if CUDA_VERSION > '11.8': quit("You are using --gpu=nvidia flag but CUDA isn't available or properly installed on your system.")
quit(f"CUDA version {CUDA_VERSION} is not supported - please downgrade to 11.8") if CUDA_VERSION > '11.8':
if CUDA_VERSION < '11.4': quit(f"CUDA version {CUDA_VERSION} is not supported - please downgrade to 11.8")
quit(f"CUDA version {CUDA_VERSION} is not supported - please upgrade to 11.8") if CUDA_VERSION < '11.4':
if CUDNN_VERSION < 8220: quit(f"CUDA version {CUDA_VERSION} is not supported - please upgrade to 11.8")
quit(f"CUDNN version {CUDNN_VERSION} is not supported - please upgrade to 8.9.1") if CUDNN_VERSION < 8220:
if CUDNN_VERSION > 8910: quit(f"CUDNN version {CUDNN_VERSION} is not supported - please upgrade to 8.9.1")
quit(f"CUDNN version {CUDNN_VERSION} is not supported - please downgrade to 8.9.1") if CUDNN_VERSION > 8910:
quit(f"CUDNN version {CUDNN_VERSION} is not supported - please downgrade to 8.9.1")
else: else:
roop.globals.providers = ['CPUExecutionProvider'] roop.globals.providers = ['CPUExecutionProvider']
if '--all-faces' in sys.argv or '-a' in sys.argv:
roop.globals.all_faces = True
def start_processing(): def start_processing():
frame_paths = args["frame_paths"] frame_paths = args["frame_paths"]
n = len(frame_paths) // (args['cores_count']) n = len(frame_paths) // (args['cores_count'])
# single thread # single thread
if args['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"], preview.update) process_video(args['source_img'], args["frame_paths"], preview.update)
return return
# multithread if total frames to cpu cores ratio is greater than 2 # multithread if total frames to cpu cores ratio is greater than 2
@ -345,4 +347,4 @@ def run():
status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436") 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) status_label.place(x=10,y=640,width=580,height=30)
window.mainloop() window.mainloop()

View File

@ -1,7 +1,8 @@
import onnxruntime import onnxruntime
use_gpu = False gpu = None
all_faces = False all_faces = False
log_level = 'error'
providers = onnxruntime.get_available_providers() providers = onnxruntime.get_available_providers()
if 'TensorrtExecutionProvider' in providers: if 'TensorrtExecutionProvider' in providers:

View File

@ -1,5 +1,6 @@
import os import os
import shutil import shutil
import roop.globals
sep = "/" sep = "/"
if os.name == "nt": if os.name == "nt":
@ -29,26 +30,35 @@ def detect_fps(input_path):
return 30, 30 return 30, 30
def run_ffmpeg(args):
log_level = f'-loglevel {roop.globals.log_level}'
os.system(f'ffmpeg {log_level} {args}')
def set_fps(input_path, output_path, fps): def set_fps(input_path, output_path, fps):
input_path, output_path = path(input_path), path(output_path) input_path, output_path = path(input_path), path(output_path)
os.system(f'ffmpeg -i "{input_path}" -filter:v fps=fps={fps} "{output_path}" -loglevel error') run_ffmpeg(f'-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):
hwaccel_option = '-hwaccel cuda' if roop.globals.gpu == 'nvidia' else ''
output_dir = path(output_dir) 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" -loglevel error') 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): 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) input_path, output_dir = path(input_path), path(output_dir)
os.system(f'ffmpeg -i "{input_path}" "{output_dir}{sep}%04d.png" -loglevel error') 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): def add_audio(output_dir, target_path, video, keep_frames, output_file):
video_name = os.path.splitext(video)[0] video_name = os.path.splitext(video)[0]
save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" 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) 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}" -loglevel error') 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): if not os.path.isfile(save_to):
shutil.move(output_dir + "/output.mp4", save_to) shutil.move(output_dir + "/output.mp4", save_to)
if not keep_frames: if not keep_frames: