Skip to content

Commit

Permalink
Dynamic loading of UI layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Aug 7, 2023
1 parent 3d02a32 commit e151671
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ python run.py [options]
-s SOURCE_PATH, --source SOURCE_PATH select an source image
-t TARGET_PATH, --target TARGET_PATH select an target image or video
-o OUTPUT_PATH, --output OUTPUT_PATH select output file or directory
--frame-processor FRAME_PROCESSOR [FRAME_PROCESSOR ...] frame processors (choices: face_swapper, face_enhancer, ...)
--frame-processors FRAME_PROCESSORS [FRAME_PROCESSORS ...] list of available frame processors (choices: face_swapper, face_enhancer, frame_enhancer, ...)
--ui-layouts UI_LAYOUTS [UI_LAYOUTS ...] list of available ui layouts (choices: default, ...)
--keep-fps keep target fps
--keep-frames keep temporary frames
--keep-temp keep temporary frames
--skip-audio skip target audio
--many-faces process every face
--reference-face-position REFERENCE_FACE_POSITION position of the reference face
Expand All @@ -46,7 +47,7 @@ python run.py [options]
--output-video-encoder {libx264,libx265,libvpx-vp9,h264_nvenc,hevc_nvenc} encoder used for the output video
--output-video-quality [0-100] quality used for the output video
--max-memory MAX_MEMORY maximum amount of RAM in GB
--execution-provider {cpu} [{cpu} ...] available execution provider (choices: cpu, ...)
--execution-providers {tensorrt,cuda,cpu} [{tensorrt,cuda,cpu} ...] list of available execution providers (choices: cpu, ...)
--execution-threads EXECUTION_THREADS number of execution threads
-v, --version show program's version number and exit
```
Expand Down
12 changes: 6 additions & 6 deletions roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import sys
# single thread doubles cuda performance - needs to be set before torch import
if any(arg.startswith('--execution-provider') for arg in sys.argv):
if any(arg.startswith('--execution-providers') for arg in sys.argv):
os.environ['OMP_NUM_THREADS'] = '1'
# reduce tensorflow log level
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
Expand All @@ -18,7 +18,7 @@
import roop.globals
import roop.metadata
from roop.predictor import predict_image, predict_video
from roop.processors.frame.core import get_frame_processors_modules, list_frame_processors_names
from roop.processors.frame.core import get_frame_processors_modules
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, list_module_names

warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
Expand All @@ -31,8 +31,8 @@ def parse_args() -> None:
program.add_argument('-s', '--source', help='select an source image', dest='source_path')
program.add_argument('-t', '--target', help='select an target image or video', dest='target_path')
program.add_argument('-o', '--output', help='select output file or directory', dest='output_path')
program.add_argument('--frame-processors', help='list of available frame processors', dest='frame_processors', default=['face_swapper'], nargs='+')
program.add_argument('--ui-layouts', help='list of available ui layouts', dest='ui_layouts', default=['default'], nargs='+')
program.add_argument('--frame-processors', help='list of available frame processors (choices: face_swapper, face_enhancer, frame_enhancer, ...)', dest='frame_processors', default=['face_swapper'], nargs='+')
program.add_argument('--ui-layouts', help='list of available ui layouts (choices: default, ...)', dest='ui_layouts', default=['default'], nargs='+')
program.add_argument('--keep-fps', help='keep target fps', dest='keep_fps', action='store_true')
program.add_argument('--keep-temp', help='keep temporary frames', dest='keep_temp', action='store_true')
program.add_argument('--skip-audio', help='skip target audio', dest='skip_audio', action='store_true')
Expand Down Expand Up @@ -118,8 +118,8 @@ def limit_resources() -> None:


def pre_check() -> bool:
if sys.version_info < (3, 9):
update_status('Python version is not supported - please upgrade to 3.9 or higher.')
if sys.version_info < (3, 10):
update_status('Python version is not supported - please upgrade to 3.10 or higher.')
return False
if not shutil.which('ffmpeg'):
update_status('ffmpeg is not installed.')
Expand Down
27 changes: 23 additions & 4 deletions roop/uis/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
from typing import Dict, Optional, List
import sys
from typing import Dict, Optional, List, Any

import cv2
import gradio
Expand All @@ -10,16 +11,34 @@
from roop.utilities import list_module_names

COMPONENTS: Dict[ComponentName, Component] = {}
UI_LAYOUT_METHODS = [
'render',
'listen'
]


def init() -> None:
with gradio.Blocks(theme=get_theme()) as ui:
ui_layout_module = importlib.import_module(f'roop.uis.__layouts__.{roop.globals.ui_layouts[0]}')
ui_layout_module.render()
ui_layout_module.listen()
for ui_layout in roop.globals.ui_layouts:
ui_layout_module = load_ui_layout_module(ui_layout)
ui_layout_module.render()
ui_layout_module.listen()
ui.launch()


def load_ui_layout_module(ui_layout: str) -> Any:
try:
ui_layout_module = importlib.import_module(f'roop.uis.__layouts__.{ui_layout}')
for method_name in UI_LAYOUT_METHODS:
if not hasattr(ui_layout_module, method_name):
raise NotImplementedError
except ModuleNotFoundError:
sys.exit(f'UI layout {ui_layout} could be not loaded.')
except NotImplementedError:
sys.exit(f'UI layout {ui_layout} not implemented correctly.')
return ui_layout_module


def get_theme() -> gradio.Theme:
return gradio.themes.Soft(
primary_hue=gradio.themes.colors.red,
Expand Down

0 comments on commit e151671

Please sign in to comment.