模型概述
模型特點
模型能力
使用案例
🚀 LivePortrait:具備拼接與重定向控制的高效人像動畫技術
LivePortrait 是一個用於人像動畫的項目,通過拼接和重定向控制技術,實現高效的人像動畫製作。它可以將靜態人像轉化為動態視頻,支持多種輸入格式和自定義設置,為用戶提供了便捷的人像動畫解決方案。
🚀 快速開始
1. 克隆代碼並準備環境
git clone https://github.com/KwaiVGI/LivePortrait
cd LivePortrait
# 使用 conda 創建環境
conda create -n LivePortrait python==3.9
conda activate LivePortrait
# 使用 pip 安裝依賴
# 適用於 Linux 和 Windows 用戶
pip install -r requirements.txt
# 適用於搭載 Apple Silicon 的 macOS 用戶
pip install -r requirements_macOS.txt
⚠️ 重要提示
確保你的系統已經安裝了 FFmpeg,包括
ffmpeg
和ffprobe
!
2. 下載預訓練權重
從 HuggingFace 下載預訓練權重是最簡單的方法:
# 首先,確保已安裝 git-lfs,詳見:https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage
git lfs install
# 克隆並移動權重文件
git clone https://huggingface.co/KwaiVGI/LivePortrait temp_pretrained_weights
mv temp_pretrained_weights/* pretrained_weights/
rm -rf temp_pretrained_weights
或者,你也可以從 Google Drive 或 百度雲 下載所有預訓練權重。解壓後將它們放在 ./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
3. 推理 🚀
快速上手
# 適用於 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
驅動視頻自動裁剪 📢📢📢
若要使用你自己的驅動視頻,我們 建議:⬇️
- 將其裁剪為 1:1 的寬高比(例如 512x512 或 256x256 像素),或者通過
--flag_crop_driving_video
啟用自動裁剪功能。 - 聚焦於頭部區域,與示例視頻類似。
- 儘量減少肩部動作。
- 確保驅動視頻的第一幀是正面且表情 中性 的人臉。
以下是使用 --flag_crop_driving_video
進行自動裁剪的示例:
python inference.py -s assets/examples/source/s9.jpg -d assets/examples/driving/d13.mp4 --flag_crop_driving_video
如果你發現自動裁剪的效果不理想,可以修改 --scale_crop_driving_video
和 --vy_ratio_crop_driving_video
選項來調整裁剪比例和偏移量,或者手動進行裁剪。
運動模板製作
你還可以使用以 .pkl
結尾的自動生成的運動模板文件來加快推理速度,並 保護隱私,例如:
python inference.py -s assets/examples/source/s9.jpg -d assets/examples/driving/d5.pkl # 人像動畫
python inference.py -s assets/examples/source/s13.mp4 -d assets/examples/driving/d5.pkl # 人像視頻編輯
4. Gradio 界面 🤗
我們還提供了一個 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
和 --server_name
參數來滿足你的需求!
🚀 我們還提供了一個加速選項 --flag_do_torch_compile
。首次推理時會觸發一個優化過程(大約一分鐘),使後續推理速度提高 20 - 30%。性能提升可能因不同的 CUDA 版本而異。
# 啟用 torch.compile 以加快推理速度
python app.py --flag_do_torch_compile
⚠️ 重要提示
此方法在 Windows 和 macOS 上不受支持。
或者,你可以在 HuggingFace 上輕鬆試用 🤗
5. 推理速度評估 🚀🚀🚀
我們還提供了一個腳本來評估每個模塊的推理速度:
# 適用於 NVIDIA GPU
python speed.py
以下是在 RTX 4090 GPU 上使用原生 PyTorch 框架並開啟 torch.compile
對一幀進行推理的結果:
模型 | 參數數量(M) | 模型大小(MB) | 推理時間(ms) |
---|---|---|---|
外觀特徵提取器 | 0.84 | 3.3 | 0.82 |
運動提取器 | 28.12 | 108 | 0.84 |
Spade 生成器 | 55.37 | 212 | 7.59 |
變形模塊 | 45.53 | 174 | 5.21 |
拼接和重定向模塊 | 0.23 | 2.3 | 0.31 |
注意:拼接和重定向模塊的值表示三個連續 MLP 網絡的參數總數和總推理時間。
✨ 主要特性
- 多平臺支持:支持 Linux、Windows 和搭載 Apple Silicon 的 macOS 系統。
- 多種輸入格式:支持圖像和視頻作為源輸入。
- 驅動視頻自動裁剪:方便用戶使用自己的驅動視頻。
- 運動模板製作:加快推理速度並保護隱私。
- Gradio 界面:提供更好的使用體驗。
- 推理速度優化:支持
torch.compile
加速推理。
📚 詳細文檔
本項目是論文 LivePortrait: Efficient Portrait Animation with Stitching and Retargeting Control 的官方 PyTorch 實現。我們正在積極更新和改進這個倉庫。如果你發現任何錯誤或有建議,歡迎提出問題或提交拉取請求(PR)💖。
🤗 社區資源
探索我們社區貢獻的寶貴資源,以提升你的 LivePortrait 使用體驗:
- ComfyUI-LivePortraitKJ 作者:@kijai
- comfyui-liveportrait 作者:@shadowcz007
- LivePortrait In ComfyUI 作者:@Benji
- LivePortrait 實踐教程 作者:@AI Search
- ComfyUI 教程 作者:@Sebastian Kamph
- Replicate 遊樂場 和 cog-comfyui 作者:@fofr
還有更多來自我們社區的精彩貢獻!
💐 致謝
我們要感謝 FOMM、Open Facevid2vid、SPADE 和 InsightFace 倉庫的貢獻者,感謝他們的開放研究和貢獻。
💖 引用
如果你發現 LivePortrait 對你的研究有用,歡迎給這個倉庫點個星 🌟 並使用以下 BibTeX 引用我們的工作:
@article{guo2024liveportrait,
title = {LivePortrait: Efficient Portrait Animation with Stitching and Retargeting Control},
author = {Guo, Jianzhu and Zhang, Dingyun and Liu, Xiaoqiang and Zhong, Zhizhou and Zhang, Yuan and Wan, Pengfei and Zhang, Di},
journal = {arXiv preprint arXiv:2407.03168},
year = {2024}
}
長期發佈於 arXiv。
📧 聯繫信息
Jianzhu Guo (郭建珠); guojianzhu1994@gmail.com
📄 許可證
本項目採用 MIT 許可證。





