How to Get Started with LivePortrait: Efficient Portrait Animation

Jul 23, 2024 | Educational

Have you ever dreamt of animating portraits with a simple click? Well, buckle up because LivePortrait is here to transform that dream into reality! This tool is all about bringing portraits to life through efficient animation techniques, complete with control over stitching and retargeting. Here’s your user-friendly guide to getting started with LivePortrait and resolving common issues.

Getting Started with LivePortrait

Before diving into the nuances, let’s break down the steps to set up and run LivePortrait smoothly.

Step 1: Clone the Code and Prepare the Environment

First, you’ll want to grab the LivePortrait code from GitHub and set up your working environment. Here’s how you do it:


git clone https://github.com/KwaiVGI/LivePortrait
cd LivePortrait
# create env using conda
conda create -n LivePortrait python==3.9
conda activate LivePortrait
# install dependencies
pip install -r requirements.txt  # For Linux and Windows
pip install -r requirements_macOS.txt  # For macOS with Apple Silicon

Note: Don’t forget to install [FFmpeg](https://ffmpeg.org/download.html) to handle video processing.

Step 2: Download Pretrained Weights

Like a chef needs ingredients, LivePortrait needs pretrained weights to perform its magic. You can easily download them using the following code snippet:


# Ensure git-lfs is installed
git lfs install
# clone and move the weights
git clone https://huggingface.co/KwaiVGI/LivePortrait temp_pretrained_weights
mv temp_pretrained_weights/ pretrained_weights/
rm -rf temp_pretrained_weights

If you prefer, you can also grab the weights from [Google Drive](https://drive.google.com) or [Baidu Yun](https://pan.baidu.com). Just unzip them and place them in the `./pretrained_weights` folder, ensuring the structure is correct (refer to the README for specifics).

Step 3: Running Inference

After setting up the environment, you’ll be ready to see LivePortrait in action.


# For Linux and Windows
python inference.py

# For macOS users (note: performance may vary)
PYTORCH_ENABLE_MPS_FALLBACK=1 python inference.py

If everything goes well, you’ll get a shiny new `animations/s6–d0_concat.mp4` that showcases the animated portrait.

Customizing Your Input

Want to animate a different image or video? Here’s how to specify your source:


# Using an image
python inference.py -s assets/examples/source/s9.jpg -d assets/examples/driving/d0.mp4

# Using a video
python inference.py -s assets/examples/source/s13.mp4 -d assets/examples/driving/d0.mp4

Step 4: Exploring the Gradio Interface

For those who prefer a user-friendly interface, LivePortrait offers a Gradio app:


# For Linux and Windows, and Intel-based macOS
python app.py

# For macOS with Apple Silicon
PYTORCH_ENABLE_MPS_FALLBACK=1 python app.py

Bonus: Speed Optimization

You can also speed things up using the torch compilation feature:


python app.py --flag_do_torch_compile

Troubleshooting Common Issues

Even the best tools might throw a few curveballs your way! Here are a few issues you may encounter during your journey with LivePortrait, along with their solutions:

– Issue: FFmpeg is not installed or configured correctly.
– Solution: Ensure that both `ffmpeg` and `ffprobe` are included in your system’s PATH. Reinstalling FFmpeg may help.

– Issue: Pretrained weights fail to download.
– Solution: Make sure you have Git LFS installed. If you’re still having trouble, consider downloading the weights from Google Drive or Baidu Yun.

– Issue: Inference runs slowly or fails on macOS.
– Solution: Make sure you’re using the right configurations for your hardware. Use `PYTORCH_ENABLE_MPS_FALLBACK=1` for added support.

– Issue: The output video doesn’t look as expected.
– Solution: Double-check the cropping settings of your driving video. Ensure to focus on the head area and minimize shoulder movement for better results.

For more troubleshooting questions/issues, contact our fxis.ai data scientist expert team.

—–

With this guide, you’re all set to bring your portraits to life with LivePortrait! Dive in and enjoy the results—your animated portraits await!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox