Introduce bool returns for pre_check() and pre_start(), Scope for terminal output

This commit is contained in:
henryruhs 2023-06-14 20:37:17 +02:00
parent fa93a0b1e5
commit ac867d79b2
7 changed files with 41 additions and 29 deletions

View File

Before

Width:  |  Height:  |  Size: 537 KiB

After

Width:  |  Height:  |  Size: 537 KiB

View File

@ -26,6 +26,6 @@ jobs:
with:
python-version: 3.9
- run: pip install -r requirements.txt gdown
- run: python run.py -s=.github/examples/face.jpg -t=.github/examples/target.mp4 -o=.github/examples/output.mp4
- run: python run.py -s=.github/examples/source.jpg -t=.github/examples/target.mp4 -o=.github/examples/output.mp4
- run: ffmpeg -i .github/examples/snapshot.mp4 -i .github/examples/output.mp4 -filter_complex psnr -f null -

View File

@ -46,7 +46,7 @@ def parse_args() -> None:
parser.add_argument('--execution-provider', help='execution provider', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
parser.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
args = parser.parse_known_args()[0]
args = parser.parse_args()
roop.globals.source_path = args.source_path
roop.globals.target_path = args.target_path
@ -119,32 +119,33 @@ def release_resources() -> None:
torch.cuda.empty_cache()
def pre_check() -> None:
def pre_check() -> bool:
if sys.version_info < (3, 9):
quit('Python version is not supported - please upgrade to 3.9 or higher.')
update_status('Python version is not supported - please upgrade to 3.9 or higher.')
return False
if not shutil.which('ffmpeg'):
quit('ffmpeg is not installed.')
update_status('ffmpeg is not installed.')
return False
return True
def update_status(message: str) -> None:
value = 'Status: ' + message
print(value)
def update_status(message: str, scope: str = 'ROOP.CORE') -> None:
print(f'[{scope}] {message}')
if not roop.globals.headless:
ui.update_status(value)
ui.update_status(message)
def start() -> None:
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
update_status(f'{frame_processor.NAME} is starting...')
frame_processor.pre_start()
release_resources()
if not frame_processor.pre_start():
return
# process image to image
if has_image_extension(roop.globals.target_path):
if predict_image(roop.globals.target_path) > 0.85:
destroy()
# todo: this needs a temp path for images to work with multiple frame processors
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
update_status(f'{frame_processor.NAME} is progressing...')
update_status('Progressing...', frame_processor.NAME)
frame_processor.process_image(roop.globals.source_path, roop.globals.target_path, roop.globals.output_path)
release_resources()
if is_image(roop.globals.target_path):
@ -162,7 +163,7 @@ def start() -> None:
extract_frames(roop.globals.target_path)
temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
update_status(f'{frame_processor.NAME} is progressing...')
update_status('Progressing...', frame_processor.NAME)
frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
release_resources()
# handles fps
@ -199,9 +200,11 @@ def destroy() -> None:
def run() -> None:
parse_args()
pre_check()
if not pre_check():
return
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
frame_processor.pre_check()
if not frame_processor.pre_check():
return
limit_resources()
if roop.globals.headless:
start()

View File

@ -17,21 +17,25 @@ if 'ROCMExecutionProvider' in roop.globals.execution_providers:
CODE_FORMER = None
THREAD_LOCK = threading.Lock()
NAME = 'Face Enhancer'
NAME = 'ROOP.FACE-ENHANCER'
def pre_check() -> None:
def pre_check() -> bool:
download_directory_path = resolve_relative_path('../models')
conditional_download(download_directory_path, ['https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth'])
return True
def pre_start() -> None:
def pre_start() -> bool:
if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
return update_status('Select an image or video for target path.')
update_status('Select an image or video for target path.', NAME)
return False
return True
def get_code_former():
global CODE_FORMER
with THREAD_LOCK:
model_path = resolve_relative_path('../models/codeformer.pth')
if CODE_FORMER is None:

View File

@ -11,21 +11,26 @@ from roop.utilities import conditional_download, resolve_relative_path, is_image
FACE_SWAPPER = None
THREAD_LOCK = threading.Lock()
NAME = 'Face Swapper'
NAME = 'ROOP.FACE-SWAPPER'
def pre_check() -> None:
def pre_check() -> bool:
download_directory_path = resolve_relative_path('../models')
conditional_download(download_directory_path, ['https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx'])
return True
def pre_start() -> None:
def pre_start() -> bool:
if not is_image(roop.globals.source_path):
return update_status('Select an image for source path.')
update_status('Select an image for source path.', NAME)
return False
elif not get_one_face(cv2.imread(roop.globals.source_path)):
return update_status('No face in source path detected.')
update_status('No face in source path detected.', NAME)
return False
if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
return update_status('Select an image or video for target path.')
update_status('Select an image or video for target path.', NAME)
return False
return True
def get_face_swapper() -> None:

View File

@ -78,8 +78,8 @@ def create_root(start: Callable, destroy: Callable) -> ctk.CTk:
preview_button = ctk.CTkButton(root, text='Preview', command=lambda: toggle_preview())
preview_button.place(relx=0.65, rely=0.75, relwidth=0.2, relheight=0.05)
status_label = ctk.CTkLabel(root, text='Status: None', justify='center')
status_label.place(relx=0.1, rely=0.9)
status_label = ctk.CTkLabel(root, text=None, justify='center')
status_label.place(relx=0.1, rely=0.9, relwidth=0.8)
return root

View File

@ -123,7 +123,7 @@ def is_video(video_path: str) -> bool:
return False
def conditional_download(download_directory_path: str, urls: List[str]):
def conditional_download(download_directory_path: str, urls: List[str]) -> None:
if not os.path.exists(download_directory_path):
os.makedirs(download_directory_path)
for url in urls: