LivePortrait 是由 KwaiVGI 团队开发的一项前沿技术,旨在通过深度学习实现高效、逼真的人像动画生成。使得用户能够通过简单的驱动视频或动作模板,将静态图片“活化”为具有丰富表情和姿态变化的动态人像。
本文将详细介绍 LivePortrait 的功能特点、安装步骤、使用方法以及适用场景,并提供清晰的操作示例,帮助开发者快速上手这一强大工具。
.pkl
)进行控制。硬件要求:
内存(RAM):至少 16GB
显卡(GPU):NVIDIA GPU,支持 CUDA 计算能力 ≥ 6.0(如 RTX 30 系列或更高)
git clone https://github.com/KwaiVGI/LivePortrait
cd LivePortrait
# 创建 conda 环境
conda create -n LivePortrait python=3.10
conda activate LivePortrait
根据你的 CUDA 版本安装 PyTorch:
# 示例(请根据实际版本选择)
pip install torch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 --index-url https://download.pytorch.org/whl/cu118
然后安装依赖项:
pip install -r requirements.txt
Mac 用户注意:搭载 Apple Silicon 的设备需使用
requirements_macOS.txt
文件安装依赖。X-Pose 依赖项不支持 macOS,因此您可以跳过其安装。人类模式照常工作,但不支持动物模式
推荐使用 HuggingFace CLI 下载预训练权重:
huggingface-cli download KwaiVGI/LivePortrait --local-dir pretrained_weights --exclude "*.git*" "README.md" "docs"
若无法访问 HuggingFace,可使用镜像网站 hf-mirror:
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download KwaiVGI/LivePortrait --local-dir pretrained_weights ...
下载完成后确保目录结构如下:
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.pth
运行以下命令即可生成默认示例:
# 对于Linux和Windows用户
python inference.py
# 对于搭载Apple Silicon的macOS用户(Intel未测试)。注意:这可能比RTX 4090慢20倍
PYTORCH_ENABLE_MPS_FALLBACK=1 python inference.py
输出结果保存在 animations/s6--d0_concat.mp4
中,包含原始输入、驱动视频和生成动画的拼接效果。
或者,您可以通过指定-s
和-d
参数来更改输入:
# 输入图像 + 驱动视频
python inference.py -s assets/examples/source/s9.jpg -d assets/examples/driving/d0.mp4
# 输入视频 + 驱动视频(动态编辑)
python inference.py -s assets/examples/source/s13.mp4 -d assets/examples/driving/d0.mp4
查看所有参数选项:
python inference.py -h
动物模型仅支持 Linux 和 Windows 系统,并需要 NVIDIA GPU。
首先构建 X-Pose 所需的自定义算子:
cd src/utils/dependencies/XPose/models/UniPose/ops
python setup.py build install
cd - # 等同于 cd ../../../../../../../
然后运行动物模型推理:
python inference_animals.py -s assets/examples/source/s39.jpg -d assets/examples/driving/wink.pkl --driving_multiplier 1.75 --no_flag_stitching
如果脚本成功运行,您将得到一个名为 animations/s39--wink_concat.mp4 的输出 mp4 文件。
使用您自己的驱动视频时,我们推荐: ⬇️
建议对驱动视频进行裁剪以提升效果:
python inference.py -s assets/examples/source/s9.jpg -d assets/examples/driving/d13.mp4 --flag_crop_driving_video
可选参数调整裁剪比例和偏移:
--scale_crop_driving_video 1.2
--vy_ratio_crop_driving_video 0.5
LivePortrait 提供了基于 Gradio 的图形界面,方便非程序员用户使用:
# 对于Linux和Windows用户(以及搭载Intel的macOS??)
python app.py # 人类模型模式
# 对于搭载Apple Silicon的macOS用户,不支持Intel,这可能比RTX 4090慢20倍
PYTORCH_ENABLE_MPS_FALLBACK=1 python app.py # 人类模型模式
支持参数:
--server_port
:指定端口--share
:生成公网链接分享--flag_do_torch_compile
:启用编译优化(仅限 NVIDIA GPU)# 启用torch.compile以进行更快的推理
python app.py --flag_do_torch_compile
LivePortrait 是一个极具潜力的人像动画生成项目,凭借其高效的推理架构、灵活的控制方式和良好的跨平台支持,非常适合用于虚拟主播、AI 换脸、数字人等领域。
无论是研究人员还是内容创作者,都可以通过简单几步快速部署并生成高质量的人像动画。如果你希望打造个性化的虚拟形象,或者探索 AI 驱动的视觉创作,LivePortrait 绝对值得一试!
📌 GitHub 地址:https://github.com/KwaiVGI/LivePortrait 📌 HuggingFace 页面:https://huggingface.co/KwaiVGI/LivePortrait