LivePortrait
repository·main·Indexed 12 days ago
https://github.com/klingairesearch/liveportraitA PyTorch-based framework for efficient portrait animation of humans and animals. It features advanced stitching and retargeting controls, supporting Image-to-Video (I2V) and Video-to-Video (V2V) generation. The framework includes a Gradio interface for manual facial control via sliders and supports motion templates (.pkl files) for privacy-preserving animation.
What's inside LivePortrait
- LivePortrait is an efficient solution for portrait and animal (cats/dogs) animation. It provides stitching and retargeting control for high-quality animation. The repository contains the official PyTorch implementation of the paper LivePortrait: Efficient Portrait Animation with Stitching and Retargeting Control.
Explore community-developed LivePortrait projects
mainThe LivePortrait ecosystem includes several community-driven implementations and integrations for different workflows:
Specialized Implementations
- AVTR-1: Real-time conversational talking-head model.
- ditto-talkinghead: Real-time audio-driven talking head.
- FasterLivePortrait: A high-speed version optimized with TensorRT.
- FacePoke: A real-time head transformation application controlled via mouse.
- FaceFusion: Integrates LivePortrait as
expression_restorerandface_editorprocessors.
UI and Workflow Integrations
- Stable Diffusion WebUI: Available as the
sd-webui-live-portraitextension. - ComfyUI: Multiple nodes are available, including
ComfyUI-LivePortraitKJ,ComfyUI-AdvancedLivePortrait(supports real-time preview), andcomfyui-liveportrait(supports multi-faces and expression interpolation). - AdvancedLivePortrait-WebUI: A dedicated Gradio-based WebUI.
Online Playgrounds and Demos
- HuggingFace Spaces: FacePoke Space, Expression Editor Space.
- Replicate: Expression Editor, LivePortrait Playground.
- FAL.ai: Face Control Realtime Demo.
- Poe: LivePortrait is available on Poe.
Ethical considerations for portrait animation
mainPortrait animation technology carries social risks, particularly regarding the potential misuse for creating deepfakes. Users should follow ethical guidelines and adopt responsible practices. Note that current generated results contain visual artifacts that may assist in deepfake detection. The project authors assume no legal responsibility for the use of generated results.Use motion templates for privacy-preserving animation
mainYou can create motion templates to animate portraits without sharing the original driving video. A motion template is a
.pklfile containing only the motion data, making it theoretically impossible to reconstruct the original face.- Generation: By default, LivePortrait generates a
.pklfile with the same name as your driving video (e.g.,d0.mp4producesd0.pkl) and saves it in the output directory. - Usage: To use an existing motion template instead of a video, specify the path to the
.pklfile using the-dor--drivingoption.
-d path/to/motion_template.pkl- Generation: By default, LivePortrait generates a
Configure audio and FPS for generated videos
mainWhen a driving video contains audio, LivePortrait automatically includes that audio in the generated output. The output video will also automatically match the FPS (frames per second) of the driving video.
Windows Requirement: If you are running LivePortrait on Windows, you must have
ffprobeandffmpeginstalled and available in your system path to handle audio and video concatenation.Control motion type with --flag_relative_motion
mainThe
--flag_relative_motionoption determines how motion is applied from the driving input to the source.- Relative Motion (
--flag_relative_motion): Applies the motion deformation between the driving input and its canonical form. When both inputs are videos, it uses the motion offset of the current driving frame relative to the first driving frame. This is generally more stable. - Absolute Motion (
--no_flag_relative_motion): Uses the absolute motion of the driving input. This can amplify expression driving strength but may cause identity leakage or inter-frame jitter. To use absolute driving via CLI, use the--no_flag_relative_motionflag.
# To retain only the driving video's motion (absolute driving) python inference.py --no_flag_relative_motion- Relative Motion (
Understand the `pretrained_weights` directory structure
mainThe
pretrained_weightsdirectory must follow a specific hierarchy to ensure the model components are correctly loaded. It is divided into three main subdirectories:insightfacefor face detection/analysis,liveportraitfor standard portrait animation, andliveportrait_animalsfor animal-specific animation.Ensure that the
.pthand.onnxfiles are placed in their respective subfolders as shown in the structure below.pretrained_weights ├── insightface │ └── models │ └── buffalo_l │ ├── 2d106det.onnx │ └── det_10g.onnx ├── liveportrait │ ├── base_models │ │ ├── appearance_feature_extractor.pth │ │ ├── motion_extractor.pth │ │ ├── spade_generator.pth │ │ └── warping_module.pth │ ├── landmark.onnx │ └── retargeting_models │ └── stitching_retargeting_module.pth └── liveportrait_animals ├── base_models │ ├── appearance_feature_extractor.pth │ ├── motion_extractor.pth │ ├── spade_generator.pth │ └── warping_module.pth ├── retargeting_models │ └── stitching_retargeting_module.pth └── xpose.pthUse video retargeting in Gradio
mainThe Gradio interface supports a video retargeting feature that allows you to adjust mouth movement in the source video by specifying atarget lip-open ratio. For example, setting this ratio to0will force the mouth in the source video to remain closed.Use Animals mode for portrait animation
mainLivePortrait includes an 'animals mode' fine-tuned on approximately 230K frames of animals (primarily cats and dogs).
Requirements:
- Download the trained weights from the
liveportrait_animalssubdirectory on HuggingFace or Google Drive before running. - Install X-Pose as the keypoints detector (see Install X-Pose).
Important Constraints:
- The stitching and retargeting modules are not trained for the animals model. You must disable stitching by using the
--no_flag_stitchingoption. - Using
paste-backis not recommended for this mode. - License Warning: X-Pose is for Non-commercial Scientific Research Purposes only. Replace it with another detector for commercial use.
python inference_animals.py -s assets/examples/source/s39.jpg -d assets/examples/driving/wink.pkl --no_flag_stitching --driving_multiplier 1.75- Download the trained weights from the
Download pretrained weights
mainYou need to download the pretrained weights into a
./pretrained_weightsdirectory.Option 1: Using HuggingFace CLI (Recommended)
huggingface-cli download KlingTeam/LivePortrait --local-dir pretrained_weights --exclude "*.git*" "README.md" "docs"If you are in a region where HuggingFace is inaccessible, use the mirror:
export HF_ENDPOINT=https://hf-mirror.com huggingface-cli download KlingTeam/LivePortrait --local-dir pretrained_weights --exclude "*.git*" "README.md" "docs"Option 2: Manual Download Download from Google Drive or Baidu Cloud and extract the contents into the
./pretrained_weightsdirectory, ensuring the internal directory structure matches the repository requirements.Install LivePortrait and prepare the environment
mainTo use LivePortrait, you must first clone the repository and set up a Conda environment. Ensure you have
git,conda, andFFmpeginstalled on your system.Linux and Windows Setup
- Clone the repo and create a Conda environment:
git clone https://github.com/KlingTeam/LivePortrait cd LivePortrait conda create -n LivePortrait python=3.10 conda activate LivePortrait- Install a
torchversion compatible with your CUDA version. Check your version withnvcc -V.- CUDA 11.1:
pip install torch==1.10.1+cu111 torchvision==0.11.2 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html - CUDA 11.8:
pip install torch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 --index-url https://download.pytorch.org/whl/cu118 - CUDA 12.1:
pip install torch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 --index-url https://download.pytorch.org/whl/cu121Note: For Windows, CUDA 11.8 is recommended for stability if higher versions cause issues.
- CUDA 11.1:
- Install remaining dependencies:
pip install -r requirements.txtmacOS (Apple Silicon) Setup
Note that
X-Poseis not supported on macOS. Human mode will work, but animal mode will not. Use the specific requirements file:pip install -r requirements_macOS.txtUse Portrait Video Editing (v2v) in LivePortrait
mainLivePortrait supports Portrait Video Editing (v2v), allowing you to use a source video to drive animation.
Requirements:
- Ensure the
pykalmanpackage is installed (it is included inrequirements_base.txt).
Key CLI Options for v2v:
-sor--source: Specify the source video file.--driving_smooth_observation_variance: Adjust the temporal smoothness of the motion.--flag_video_editing_head_rotation: Enable head pose motion transfer.--flag_source_video_eye_retargeting: Ensures the eye-open scalar of each source frame matches the first source frame before animation.
python -m liveportrait.inference --source path/to/source_video.mp4 --driving_smooth_observation_variance 0.1 --flag_video_editing_head_rotation --flag_source_video_eye_retargeting- Ensure the