Aadhithya
commited on
Commit
·
e0cff75
1
Parent(s):
79546b5
Upload core.py
Browse files- roop/core.py +215 -0
roop/core.py
ADDED
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
# single thread doubles cuda performance - needs to be set before torch import
|
6 |
+
if any(arg.startswith('--execution-provider') for arg in sys.argv):
|
7 |
+
os.environ['OMP_NUM_THREADS'] = '1'
|
8 |
+
# reduce tensorflow log level
|
9 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
10 |
+
import warnings
|
11 |
+
from typing import List
|
12 |
+
import platform
|
13 |
+
import signal
|
14 |
+
import shutil
|
15 |
+
import argparse
|
16 |
+
import torch
|
17 |
+
import onnxruntime
|
18 |
+
import tensorflow
|
19 |
+
|
20 |
+
import roop.globals
|
21 |
+
import roop.metadata
|
22 |
+
import roop.ui as ui
|
23 |
+
from roop.predicter import predict_image, predict_video
|
24 |
+
from roop.processors.frame.core import get_frame_processors_modules
|
25 |
+
from roop.utilities import has_image_extension, is_image, is_video, detect_fps, create_video, extract_frames, get_temp_frame_paths, restore_audio, create_temp, move_temp, clean_temp, normalize_output_path
|
26 |
+
|
27 |
+
if 'ROCMExecutionProvider' in roop.globals.execution_providers:
|
28 |
+
del torch
|
29 |
+
|
30 |
+
warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
|
31 |
+
warnings.filterwarnings('ignore', category=UserWarning, module='torchvision')
|
32 |
+
|
33 |
+
|
34 |
+
def parse_args() -> None:
|
35 |
+
signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
|
36 |
+
program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
|
37 |
+
program.add_argument('-s', '--source', help='select an source image', dest='source_path')
|
38 |
+
program.add_argument('-t', '--target', help='select an target image or video', dest='target_path')
|
39 |
+
program.add_argument('-o', '--output', help='select output file or directory', dest='output_path')
|
40 |
+
program.add_argument('--frame-processor', help='frame processors (choices: face_swapper, face_enhancer, ...)', dest='frame_processor', default=['face_swapper'], nargs='+')
|
41 |
+
program.add_argument('--keep-fps', help='keep original fps', dest='keep_fps', action='store_true', default=False)
|
42 |
+
program.add_argument('--keep-audio', help='keep original audio', dest='keep_audio', action='store_true', default=True)
|
43 |
+
program.add_argument('--keep-frames', help='keep temporary frames', dest='keep_frames', action='store_true', default=False)
|
44 |
+
program.add_argument('--many-faces', help='process every face', dest='many_faces', action='store_true', default=False)
|
45 |
+
program.add_argument('--video-encoder', help='adjust output video encoder', dest='video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9'])
|
46 |
+
program.add_argument('--video-quality', help='adjust output video quality', dest='video_quality', type=int, default=18, choices=range(52), metavar='[0-51]')
|
47 |
+
program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int, default=suggest_max_memory())
|
48 |
+
program.add_argument('--execution-provider', help='available execution provider (choices: cpu, ...)', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
|
49 |
+
program.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
|
50 |
+
program.add_argument('-v', '--version', action='version', version=f'{roop.metadata.name} {roop.metadata.version}')
|
51 |
+
|
52 |
+
args = program.parse_args()
|
53 |
+
|
54 |
+
roop.globals.source_path = args.source_path
|
55 |
+
roop.globals.target_path = args.target_path
|
56 |
+
roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path)
|
57 |
+
roop.globals.frame_processors = args.frame_processor
|
58 |
+
roop.globals.headless = args.source_path or args.target_path or args.output_path
|
59 |
+
roop.globals.keep_fps = args.keep_fps
|
60 |
+
roop.globals.keep_audio = args.keep_audio
|
61 |
+
roop.globals.keep_frames = args.keep_frames
|
62 |
+
roop.globals.many_faces = args.many_faces
|
63 |
+
roop.globals.video_encoder = args.video_encoder
|
64 |
+
roop.globals.video_quality = args.video_quality
|
65 |
+
roop.globals.max_memory = args.max_memory
|
66 |
+
roop.globals.execution_providers = decode_execution_providers(args.execution_provider)
|
67 |
+
roop.globals.execution_threads = args.execution_threads
|
68 |
+
|
69 |
+
|
70 |
+
def encode_execution_providers(execution_providers: List[str]) -> List[str]:
|
71 |
+
return [execution_provider.replace('ExecutionProvider', '').lower() for execution_provider in execution_providers]
|
72 |
+
|
73 |
+
|
74 |
+
def decode_execution_providers(execution_providers: List[str]) -> List[str]:
|
75 |
+
return [provider for provider, encoded_execution_provider in zip(onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers()))
|
76 |
+
if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)]
|
77 |
+
|
78 |
+
|
79 |
+
def suggest_max_memory() -> int:
|
80 |
+
if platform.system().lower() == 'darwin':
|
81 |
+
return 4
|
82 |
+
return 16
|
83 |
+
|
84 |
+
|
85 |
+
def suggest_execution_providers() -> List[str]:
|
86 |
+
return encode_execution_providers(onnxruntime.get_available_providers())
|
87 |
+
|
88 |
+
|
89 |
+
def suggest_execution_threads() -> int:
|
90 |
+
if 'DmlExecutionProvider' in roop.globals.execution_providers:
|
91 |
+
return 1
|
92 |
+
if 'ROCMExecutionProvider' in roop.globals.execution_providers:
|
93 |
+
return 1
|
94 |
+
return 8
|
95 |
+
|
96 |
+
|
97 |
+
def limit_resources() -> None:
|
98 |
+
# prevent tensorflow memory leak
|
99 |
+
gpus = tensorflow.config.experimental.list_physical_devices('GPU')
|
100 |
+
for gpu in gpus:
|
101 |
+
tensorflow.config.experimental.set_virtual_device_configuration(gpu, [
|
102 |
+
tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)
|
103 |
+
])
|
104 |
+
# limit memory usage
|
105 |
+
if roop.globals.max_memory:
|
106 |
+
memory = roop.globals.max_memory * 1024 ** 3
|
107 |
+
if platform.system().lower() == 'darwin':
|
108 |
+
memory = roop.globals.max_memory * 1024 ** 6
|
109 |
+
if platform.system().lower() == 'windows':
|
110 |
+
import ctypes
|
111 |
+
kernel32 = ctypes.windll.kernel32
|
112 |
+
kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
|
113 |
+
else:
|
114 |
+
import resource
|
115 |
+
resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
|
116 |
+
|
117 |
+
|
118 |
+
def release_resources() -> None:
|
119 |
+
if 'CUDAExecutionProvider' in roop.globals.execution_providers:
|
120 |
+
torch.cuda.empty_cache()
|
121 |
+
|
122 |
+
|
123 |
+
def pre_check() -> bool:
|
124 |
+
if sys.version_info < (3, 9):
|
125 |
+
update_status('Python version is not supported - please upgrade to 3.9 or higher.')
|
126 |
+
return False
|
127 |
+
if not shutil.which('ffmpeg'):
|
128 |
+
update_status('ffmpeg is not installed.')
|
129 |
+
return False
|
130 |
+
return True
|
131 |
+
|
132 |
+
|
133 |
+
def update_status(message: str, scope: str = 'ROOP.CORE') -> None:
|
134 |
+
print(f'[{scope}] {message}')
|
135 |
+
if not roop.globals.headless:
|
136 |
+
ui.update_status(message)
|
137 |
+
|
138 |
+
|
139 |
+
def start() -> None:
|
140 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
141 |
+
if not frame_processor.pre_start():
|
142 |
+
return
|
143 |
+
# process image to image
|
144 |
+
if has_image_extension(roop.globals.target_path):
|
145 |
+
if predict_image(roop.globals.target_path):
|
146 |
+
destroy()
|
147 |
+
shutil.copy2(roop.globals.target_path, roop.globals.output_path)
|
148 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
149 |
+
update_status('Progressing...', frame_processor.NAME)
|
150 |
+
frame_processor.process_image(roop.globals.source_path, roop.globals.output_path, roop.globals.output_path)
|
151 |
+
frame_processor.post_process()
|
152 |
+
release_resources()
|
153 |
+
if is_image(roop.globals.target_path):
|
154 |
+
update_status('Processing to image succeed!')
|
155 |
+
else:
|
156 |
+
update_status('Processing to image failed!')
|
157 |
+
return
|
158 |
+
# process image to videos
|
159 |
+
if predict_video(roop.globals.target_path):
|
160 |
+
destroy()
|
161 |
+
update_status('Creating temp resources...')
|
162 |
+
create_temp(roop.globals.target_path)
|
163 |
+
update_status('Extracting frames...')
|
164 |
+
extract_frames(roop.globals.target_path)
|
165 |
+
temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
|
166 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
167 |
+
update_status('Progressing...', frame_processor.NAME)
|
168 |
+
frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
|
169 |
+
frame_processor.post_process()
|
170 |
+
release_resources()
|
171 |
+
# handles fps
|
172 |
+
if roop.globals.keep_fps:
|
173 |
+
update_status('Detecting fps...')
|
174 |
+
fps = detect_fps(roop.globals.target_path)
|
175 |
+
update_status(f'Creating video with {fps} fps...')
|
176 |
+
create_video(roop.globals.target_path, fps)
|
177 |
+
else:
|
178 |
+
update_status('Creating video with 30.0 fps...')
|
179 |
+
create_video(roop.globals.target_path)
|
180 |
+
# handle audio
|
181 |
+
if roop.globals.keep_audio:
|
182 |
+
if roop.globals.keep_fps:
|
183 |
+
update_status('Restoring audio...')
|
184 |
+
else:
|
185 |
+
update_status('Restoring audio might cause issues as fps are not kept...')
|
186 |
+
restore_audio(roop.globals.target_path, roop.globals.output_path)
|
187 |
+
else:
|
188 |
+
move_temp(roop.globals.target_path, roop.globals.output_path)
|
189 |
+
# clean and validate
|
190 |
+
clean_temp(roop.globals.target_path)
|
191 |
+
if is_video(roop.globals.target_path):
|
192 |
+
update_status('Processing to video succeed!')
|
193 |
+
else:
|
194 |
+
update_status('Processing to video failed!')
|
195 |
+
|
196 |
+
|
197 |
+
def destroy() -> None:
|
198 |
+
if roop.globals.target_path:
|
199 |
+
clean_temp(roop.globals.target_path)
|
200 |
+
quit()
|
201 |
+
|
202 |
+
|
203 |
+
def run() -> None:
|
204 |
+
parse_args()
|
205 |
+
if not pre_check():
|
206 |
+
return
|
207 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
208 |
+
if not frame_processor.pre_check():
|
209 |
+
return
|
210 |
+
limit_resources()
|
211 |
+
if roop.globals.headless:
|
212 |
+
start()
|
213 |
+
else:
|
214 |
+
window = ui.init(start, destroy)
|
215 |
+
window.mainloop()
|