LivePortrait/inference.py

59 lines
1.6 KiB
Python
Raw Normal View History

2024-07-03 20:32:47 +00:00
# coding: utf-8
2024-07-11 14:58:45 +00:00
import os.path as osp
2024-07-03 20:32:47 +00:00
import tyro
2024-07-12 06:34:28 +00:00
import subprocess
2024-07-03 20:32:47 +00:00
from src.config.argument_config import ArgumentConfig
from src.config.inference_config import InferenceConfig
from src.config.crop_config import CropConfig
from src.live_portrait_pipeline import LivePortraitPipeline
def partial_fields(target_class, kwargs):
return target_class(**{k: v for k, v in kwargs.items() if hasattr(target_class, k)})
def fast_check_ffmpeg():
try:
subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True)
return True
except:
return False
2024-07-11 14:58:45 +00:00
def fast_check_args(args: ArgumentConfig):
if not osp.exists(args.source_image):
raise FileNotFoundError(f"source image not found: {args.source_image}")
if not osp.exists(args.driving_info):
raise FileNotFoundError(f"driving info not found: {args.driving_info}")
2024-07-03 20:32:47 +00:00
def main():
# set tyro theme
tyro.extras.set_accent_color("bright_cyan")
args = tyro.cli(ArgumentConfig)
if not fast_check_ffmpeg():
raise ImportError(
"FFmpeg is not installed. Please install FFmpeg before running this script. https://ffmpeg.org/download.html"
)
2024-07-12 06:34:28 +00:00
2024-07-11 14:58:45 +00:00
# fast check the args
fast_check_args(args)
2024-07-03 20:32:47 +00:00
# specify configs for inference
inference_cfg = partial_fields(InferenceConfig, args.__dict__)
crop_cfg = partial_fields(CropConfig, args.__dict__)
2024-07-03 20:32:47 +00:00
live_portrait_pipeline = LivePortraitPipeline(
2024-07-12 06:34:28 +00:00
inference_cfg=inference_cfg,
crop_cfg=crop_cfg
2024-07-03 20:32:47 +00:00
)
# run
live_portrait_pipeline.execute(args)
if __name__ == "__main__":
2024-07-03 20:32:47 +00:00
main()