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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import shutil
|
| 6 |
+
# single thread doubles cuda performance - needs to be set before torch import
|
| 7 |
+
if any(arg.startswith('--execution-provider') for arg in sys.argv):
|
| 8 |
+
os.environ['OMP_NUM_THREADS'] = '1'
|
| 9 |
+
|
| 10 |
+
import warnings
|
| 11 |
+
from typing import List
|
| 12 |
+
import platform
|
| 13 |
+
import signal
|
| 14 |
+
import torch
|
| 15 |
+
import onnxruntime
|
| 16 |
+
import pathlib
|
| 17 |
+
import argparse
|
| 18 |
+
|
| 19 |
+
from time import time
|
| 20 |
+
|
| 21 |
+
import roop.globals
|
| 22 |
+
import roop.metadata
|
| 23 |
+
import roop.utilities as util
|
| 24 |
+
import roop.util_ffmpeg as ffmpeg
|
| 25 |
+
import ui.main as main
|
| 26 |
+
from settings import Settings
|
| 27 |
+
from roop.face_util import extract_face_images
|
| 28 |
+
from roop.ProcessEntry import ProcessEntry
|
| 29 |
+
from roop.ProcessMgr import ProcessMgr
|
| 30 |
+
from roop.ProcessOptions import ProcessOptions
|
| 31 |
+
from roop.capturer import get_video_frame_total, release_video
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
clip_text = None
|
| 35 |
+
|
| 36 |
+
call_display_ui = None
|
| 37 |
+
|
| 38 |
+
process_mgr = None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
if 'ROCMExecutionProvider' in roop.globals.execution_providers:
|
| 42 |
+
del torch
|
| 43 |
+
|
| 44 |
+
warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
|
| 45 |
+
warnings.filterwarnings('ignore', category=UserWarning, module='torchvision')
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def parse_args() -> None:
|
| 49 |
+
signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
|
| 50 |
+
roop.globals.headless = False
|
| 51 |
+
|
| 52 |
+
program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
|
| 53 |
+
program.add_argument('--server_share', help='Public server', dest='server_share', action='store_true', default=False)
|
| 54 |
+
program.add_argument('--cuda_device_id', help='Index of the cuda gpu to use', dest='cuda_device_id', type=int, default=0)
|
| 55 |
+
roop.globals.startup_args = program.parse_args()
|
| 56 |
+
# Always enable all processors when using GUI
|
| 57 |
+
roop.globals.frame_processors = ['face_swapper', 'face_enhancer']
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def encode_execution_providers(execution_providers: List[str]) -> List[str]:
|
| 61 |
+
return [execution_provider.replace('ExecutionProvider', '').lower() for execution_provider in execution_providers]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def decode_execution_providers(execution_providers: List[str]) -> List[str]:
|
| 65 |
+
list_providers = [provider for provider, encoded_execution_provider in zip(onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers()))
|
| 66 |
+
if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)]
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
for i in range(len(list_providers)):
|
| 70 |
+
if list_providers[i] == 'CUDAExecutionProvider':
|
| 71 |
+
list_providers[i] = ('CUDAExecutionProvider', {'device_id': roop.globals.cuda_device_id})
|
| 72 |
+
torch.cuda.set_device(roop.globals.cuda_device_id)
|
| 73 |
+
break
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
|
| 77 |
+
return list_providers
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def suggest_max_memory() -> int:
|
| 82 |
+
if platform.system().lower() == 'darwin':
|
| 83 |
+
return 4
|
| 84 |
+
return 16
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def suggest_execution_providers() -> List[str]:
|
| 88 |
+
return encode_execution_providers(onnxruntime.get_available_providers())
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def suggest_execution_threads() -> int:
|
| 92 |
+
if 'DmlExecutionProvider' in roop.globals.execution_providers:
|
| 93 |
+
return 1
|
| 94 |
+
if 'ROCMExecutionProvider' in roop.globals.execution_providers:
|
| 95 |
+
return 1
|
| 96 |
+
return 8
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def limit_resources() -> None:
|
| 100 |
+
# limit memory usage
|
| 101 |
+
if roop.globals.max_memory:
|
| 102 |
+
memory = roop.globals.max_memory * 1024 ** 3
|
| 103 |
+
if platform.system().lower() == 'darwin':
|
| 104 |
+
memory = roop.globals.max_memory * 1024 ** 6
|
| 105 |
+
if platform.system().lower() == 'windows':
|
| 106 |
+
import ctypes
|
| 107 |
+
kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
|
| 108 |
+
kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
|
| 109 |
+
else:
|
| 110 |
+
import resource
|
| 111 |
+
resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def release_resources() -> None:
|
| 116 |
+
import gc
|
| 117 |
+
global process_mgr
|
| 118 |
+
|
| 119 |
+
if process_mgr is not None:
|
| 120 |
+
process_mgr.release_resources()
|
| 121 |
+
process_mgr = None
|
| 122 |
+
|
| 123 |
+
gc.collect()
|
| 124 |
+
# if 'CUDAExecutionProvider' in roop.globals.execution_providers and torch.cuda.is_available():
|
| 125 |
+
# with torch.cuda.device('cuda'):
|
| 126 |
+
# torch.cuda.empty_cache()
|
| 127 |
+
# torch.cuda.ipc_collect()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def pre_check() -> bool:
|
| 131 |
+
if sys.version_info < (3, 9):
|
| 132 |
+
update_status('Python version is not supported - please upgrade to 3.9 or higher.')
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
+
download_directory_path = util.resolve_relative_path('../models')
|
| 136 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/inswapper_128.onnx'])
|
| 137 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/GFPGANv1.4.onnx'])
|
| 138 |
+
util.conditional_download(download_directory_path, ['https://github.com/csxmli2016/DMDNet/releases/download/v1/DMDNet.pth'])
|
| 139 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/GPEN-BFR-512.onnx'])
|
| 140 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/restoreformer_plus_plus.onnx'])
|
| 141 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/xseg.onnx'])
|
| 142 |
+
download_directory_path = util.resolve_relative_path('../models/CLIP')
|
| 143 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/rd64-uni-refined.pth'])
|
| 144 |
+
download_directory_path = util.resolve_relative_path('../models/CodeFormer')
|
| 145 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/CodeFormerv0.1.onnx'])
|
| 146 |
+
download_directory_path = util.resolve_relative_path('../models/Frame')
|
| 147 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/deoldify_artistic.onnx'])
|
| 148 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/deoldify_stable.onnx'])
|
| 149 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/isnet-general-use.onnx'])
|
| 150 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/real_esrgan_x4.onnx'])
|
| 151 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/real_esrgan_x2.onnx'])
|
| 152 |
+
util.conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/lsdir_x4.onnx'])
|
| 153 |
+
|
| 154 |
+
if not shutil.which('ffmpeg'):
|
| 155 |
+
update_status('ffmpeg is not installed.')
|
| 156 |
+
return True
|
| 157 |
+
|
| 158 |
+
def set_display_ui(function):
|
| 159 |
+
global call_display_ui
|
| 160 |
+
|
| 161 |
+
call_display_ui = function
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def update_status(message: str) -> None:
|
| 165 |
+
global call_display_ui
|
| 166 |
+
|
| 167 |
+
print(message)
|
| 168 |
+
if call_display_ui is not None:
|
| 169 |
+
call_display_ui(message)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def start() -> None:
|
| 175 |
+
if roop.globals.headless:
|
| 176 |
+
print('Headless mode currently unsupported - starting UI!')
|
| 177 |
+
# faces = extract_face_images(roop.globals.source_path, (False, 0))
|
| 178 |
+
# roop.globals.INPUT_FACES.append(faces[roop.globals.source_face_index])
|
| 179 |
+
# faces = extract_face_images(roop.globals.target_path, (False, util.has_image_extension(roop.globals.target_path)))
|
| 180 |
+
# roop.globals.TARGET_FACES.append(faces[roop.globals.target_face_index])
|
| 181 |
+
# if 'face_enhancer' in roop.globals.frame_processors:
|
| 182 |
+
# roop.globals.selected_enhancer = 'GFPGAN'
|
| 183 |
+
|
| 184 |
+
batch_process_regular(None, False, None)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def get_processing_plugins(masking_engine):
|
| 188 |
+
processors = { "faceswap": {}}
|
| 189 |
+
if masking_engine is not None:
|
| 190 |
+
processors.update({masking_engine: {}})
|
| 191 |
+
|
| 192 |
+
if roop.globals.selected_enhancer == 'GFPGAN':
|
| 193 |
+
processors.update({"gfpgan": {}})
|
| 194 |
+
elif roop.globals.selected_enhancer == 'Codeformer':
|
| 195 |
+
processors.update({"codeformer": {}})
|
| 196 |
+
elif roop.globals.selected_enhancer == 'DMDNet':
|
| 197 |
+
processors.update({"dmdnet": {}})
|
| 198 |
+
elif roop.globals.selected_enhancer == 'GPEN':
|
| 199 |
+
processors.update({"gpen": {}})
|
| 200 |
+
elif roop.globals.selected_enhancer == 'Restoreformer++':
|
| 201 |
+
processors.update({"restoreformer++": {}})
|
| 202 |
+
return processors
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def live_swap(frame, options):
|
| 206 |
+
global process_mgr
|
| 207 |
+
|
| 208 |
+
if frame is None:
|
| 209 |
+
return frame
|
| 210 |
+
|
| 211 |
+
if process_mgr is None:
|
| 212 |
+
process_mgr = ProcessMgr(None)
|
| 213 |
+
|
| 214 |
+
# if len(roop.globals.INPUT_FACESETS) <= selected_index:
|
| 215 |
+
# selected_index = 0
|
| 216 |
+
process_mgr.initialize(roop.globals.INPUT_FACESETS, roop.globals.TARGET_FACES, options)
|
| 217 |
+
newframe = process_mgr.process_frame(frame)
|
| 218 |
+
if newframe is None:
|
| 219 |
+
return frame
|
| 220 |
+
return newframe
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def batch_process_regular(output_method, files:list[ProcessEntry], masking_engine:str, new_clip_text:str, use_new_method, imagemask, restore_original_mouth, num_swap_steps, progress, selected_index = 0) -> None:
|
| 224 |
+
global clip_text, process_mgr
|
| 225 |
+
|
| 226 |
+
release_resources()
|
| 227 |
+
limit_resources()
|
| 228 |
+
if process_mgr is None:
|
| 229 |
+
process_mgr = ProcessMgr(progress)
|
| 230 |
+
mask = imagemask["layers"][0] if imagemask is not None else None
|
| 231 |
+
if len(roop.globals.INPUT_FACESETS) <= selected_index:
|
| 232 |
+
selected_index = 0
|
| 233 |
+
options = ProcessOptions(get_processing_plugins(masking_engine), roop.globals.distance_threshold, roop.globals.blend_ratio,
|
| 234 |
+
roop.globals.face_swap_mode, selected_index, new_clip_text, mask, num_swap_steps,
|
| 235 |
+
roop.globals.subsample_size, False, restore_original_mouth)
|
| 236 |
+
process_mgr.initialize(roop.globals.INPUT_FACESETS, roop.globals.TARGET_FACES, options)
|
| 237 |
+
batch_process(output_method, files, use_new_method)
|
| 238 |
+
return
|
| 239 |
+
|
| 240 |
+
def batch_process_with_options(files:list[ProcessEntry], options, progress):
|
| 241 |
+
global clip_text, process_mgr
|
| 242 |
+
|
| 243 |
+
release_resources()
|
| 244 |
+
limit_resources()
|
| 245 |
+
if process_mgr is None:
|
| 246 |
+
process_mgr = ProcessMgr(progress)
|
| 247 |
+
process_mgr.initialize(roop.globals.INPUT_FACESETS, roop.globals.TARGET_FACES, options)
|
| 248 |
+
roop.globals.keep_frames = False
|
| 249 |
+
roop.globals.wait_after_extraction = False
|
| 250 |
+
roop.globals.skip_audio = False
|
| 251 |
+
batch_process("Files", files, True)
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def batch_process(output_method, files:list[ProcessEntry], use_new_method) -> None:
|
| 256 |
+
global clip_text, process_mgr
|
| 257 |
+
|
| 258 |
+
roop.globals.processing = True
|
| 259 |
+
|
| 260 |
+
# limit threads for some providers
|
| 261 |
+
max_threads = suggest_execution_threads()
|
| 262 |
+
if max_threads == 1:
|
| 263 |
+
roop.globals.execution_threads = 1
|
| 264 |
+
|
| 265 |
+
imagefiles:list[ProcessEntry] = []
|
| 266 |
+
videofiles:list[ProcessEntry] = []
|
| 267 |
+
|
| 268 |
+
update_status('Sorting videos/images')
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
for index, f in enumerate(files):
|
| 272 |
+
fullname = f.filename
|
| 273 |
+
if util.has_image_extension(fullname):
|
| 274 |
+
destination = util.get_destfilename_from_path(fullname, roop.globals.output_path, f'.{roop.globals.CFG.output_image_format}')
|
| 275 |
+
destination = util.replace_template(destination, index=index)
|
| 276 |
+
pathlib.Path(os.path.dirname(destination)).mkdir(parents=True, exist_ok=True)
|
| 277 |
+
f.finalname = destination
|
| 278 |
+
imagefiles.append(f)
|
| 279 |
+
|
| 280 |
+
elif util.is_video(fullname) or util.has_extension(fullname, ['gif']):
|
| 281 |
+
destination = util.get_destfilename_from_path(fullname, roop.globals.output_path, f'__temp.{roop.globals.CFG.output_video_format}')
|
| 282 |
+
f.finalname = destination
|
| 283 |
+
videofiles.append(f)
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
if(len(imagefiles) > 0):
|
| 288 |
+
update_status('Processing image(s)')
|
| 289 |
+
origimages = []
|
| 290 |
+
fakeimages = []
|
| 291 |
+
for f in imagefiles:
|
| 292 |
+
origimages.append(f.filename)
|
| 293 |
+
fakeimages.append(f.finalname)
|
| 294 |
+
|
| 295 |
+
process_mgr.run_batch(origimages, fakeimages, roop.globals.execution_threads)
|
| 296 |
+
origimages.clear()
|
| 297 |
+
fakeimages.clear()
|
| 298 |
+
|
| 299 |
+
if(len(videofiles) > 0):
|
| 300 |
+
for index,v in enumerate(videofiles):
|
| 301 |
+
if not roop.globals.processing:
|
| 302 |
+
end_processing('Processing stopped!')
|
| 303 |
+
return
|
| 304 |
+
fps = v.fps if v.fps > 0 else util.detect_fps(v.filename)
|
| 305 |
+
if v.endframe == 0:
|
| 306 |
+
v.endframe = get_video_frame_total(v.filename)
|
| 307 |
+
|
| 308 |
+
is_streaming_only = output_method == "Virtual Camera"
|
| 309 |
+
if is_streaming_only == False:
|
| 310 |
+
update_status(f'Creating {os.path.basename(v.finalname)} with {fps} FPS...')
|
| 311 |
+
|
| 312 |
+
start_processing = time()
|
| 313 |
+
if is_streaming_only == False and roop.globals.keep_frames or not use_new_method:
|
| 314 |
+
util.create_temp(v.filename)
|
| 315 |
+
update_status('Extracting frames...')
|
| 316 |
+
ffmpeg.extract_frames(v.filename,v.startframe,v.endframe, fps)
|
| 317 |
+
if not roop.globals.processing:
|
| 318 |
+
end_processing('Processing stopped!')
|
| 319 |
+
return
|
| 320 |
+
|
| 321 |
+
temp_frame_paths = util.get_temp_frame_paths(v.filename)
|
| 322 |
+
process_mgr.run_batch(temp_frame_paths, temp_frame_paths, roop.globals.execution_threads)
|
| 323 |
+
if not roop.globals.processing:
|
| 324 |
+
end_processing('Processing stopped!')
|
| 325 |
+
return
|
| 326 |
+
if roop.globals.wait_after_extraction:
|
| 327 |
+
extract_path = os.path.dirname(temp_frame_paths[0])
|
| 328 |
+
util.open_folder(extract_path)
|
| 329 |
+
input("Press any key to continue...")
|
| 330 |
+
print("Resorting frames to create video")
|
| 331 |
+
util.sort_rename_frames(extract_path)
|
| 332 |
+
|
| 333 |
+
ffmpeg.create_video(v.filename, v.finalname, fps)
|
| 334 |
+
if not roop.globals.keep_frames:
|
| 335 |
+
util.delete_temp_frames(temp_frame_paths[0])
|
| 336 |
+
else:
|
| 337 |
+
if util.has_extension(v.filename, ['gif']):
|
| 338 |
+
skip_audio = True
|
| 339 |
+
else:
|
| 340 |
+
skip_audio = roop.globals.skip_audio
|
| 341 |
+
process_mgr.run_batch_inmem(output_method, v.filename, v.finalname, v.startframe, v.endframe, fps,roop.globals.execution_threads)
|
| 342 |
+
|
| 343 |
+
if not roop.globals.processing:
|
| 344 |
+
end_processing('Processing stopped!')
|
| 345 |
+
return
|
| 346 |
+
|
| 347 |
+
video_file_name = v.finalname
|
| 348 |
+
if os.path.isfile(video_file_name):
|
| 349 |
+
destination = ''
|
| 350 |
+
if util.has_extension(v.filename, ['gif']):
|
| 351 |
+
gifname = util.get_destfilename_from_path(v.filename, roop.globals.output_path, '.gif')
|
| 352 |
+
destination = util.replace_template(gifname, index=index)
|
| 353 |
+
pathlib.Path(os.path.dirname(destination)).mkdir(parents=True, exist_ok=True)
|
| 354 |
+
|
| 355 |
+
update_status('Creating final GIF')
|
| 356 |
+
ffmpeg.create_gif_from_video(video_file_name, destination)
|
| 357 |
+
if os.path.isfile(destination):
|
| 358 |
+
os.remove(video_file_name)
|
| 359 |
+
else:
|
| 360 |
+
skip_audio = roop.globals.skip_audio
|
| 361 |
+
destination = util.replace_template(video_file_name, index=index)
|
| 362 |
+
pathlib.Path(os.path.dirname(destination)).mkdir(parents=True, exist_ok=True)
|
| 363 |
+
|
| 364 |
+
if not skip_audio:
|
| 365 |
+
ffmpeg.restore_audio(video_file_name, v.filename, v.startframe, v.endframe, destination)
|
| 366 |
+
if os.path.isfile(destination):
|
| 367 |
+
os.remove(video_file_name)
|
| 368 |
+
else:
|
| 369 |
+
shutil.move(video_file_name, destination)
|
| 370 |
+
|
| 371 |
+
elif is_streaming_only == False:
|
| 372 |
+
update_status(f'Failed processing {os.path.basename(v.finalname)}!')
|
| 373 |
+
elapsed_time = time() - start_processing
|
| 374 |
+
average_fps = (v.endframe - v.startframe) / elapsed_time
|
| 375 |
+
update_status(f'\nProcessing {os.path.basename(destination)} took {elapsed_time:.2f} secs, {average_fps:.2f} frames/s')
|
| 376 |
+
end_processing('Finished')
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
def end_processing(msg:str):
|
| 380 |
+
update_status(msg)
|
| 381 |
+
roop.globals.target_folder_path = None
|
| 382 |
+
release_resources()
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
def destroy() -> None:
|
| 386 |
+
if roop.globals.target_path:
|
| 387 |
+
util.clean_temp(roop.globals.target_path)
|
| 388 |
+
release_resources()
|
| 389 |
+
sys.exit()
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
def run() -> None:
|
| 393 |
+
parse_args()
|
| 394 |
+
if not pre_check():
|
| 395 |
+
return
|
| 396 |
+
roop.globals.CFG = Settings('config.yaml')
|
| 397 |
+
roop.globals.cuda_device_id = roop.globals.startup_args.cuda_device_id
|
| 398 |
+
roop.globals.execution_threads = roop.globals.CFG.max_threads
|
| 399 |
+
roop.globals.video_encoder = roop.globals.CFG.output_video_codec
|
| 400 |
+
roop.globals.video_quality = roop.globals.CFG.video_quality
|
| 401 |
+
roop.globals.max_memory = roop.globals.CFG.memory_limit if roop.globals.CFG.memory_limit > 0 else None
|
| 402 |
+
if roop.globals.startup_args.server_share:
|
| 403 |
+
roop.globals.CFG.server_share = True
|
| 404 |
+
main.run()
|