LivePortrait 本地部署教程,强大且开源的可控人像AI视频生成

 
 

 

 

1,准备工作,本地下载代码并准备环境,运行命令前需安装git

没有外网环境的朋友可以直接点击【网盘打包下载

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 with pip
# for Linux and Windows users
pip install -r requirements.txt
# for macOS with Apple Silicon users
pip install -r requirements_macOS.txt

注意:确保您的系统已安装FFmpeg,包括ffmpegffprobe!不会安装?看这个FFmpeg 【安装教程

2. 下载预训练权重

下载预训练权重的最简单方法是从 HuggingFace 下载:

# first, ensure git-lfs is installed, see: https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage
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

 

非海外用户,没有外网环境的朋友,你可以从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.推理使用

# For Linux and Windows
python inference.py

# For macOS with Apple Silicon, Intel not supported, this maybe 20x slower than RTX 4090
PYTORCH_ENABLE_MPS_FALLBACK=1 python inference.py

如果脚本成功运行,你会得到一个名为 的输出mp4文件animations/s6--d0_concat.mp4。此文件包含以下结果:驾驶视频,输入图像或视频,以及生成的结果。

图像

或者您可以通过指定-s和参数-d来更改输入

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

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

# more options to see
python inference.py -h

参照视频自动裁剪 📢📢📢

 

要使用您自己的参照视频,我们建议:⬇️

  • 将其裁剪为1:1 的宽高比(例如 512×512 或 256×256 像素),或通过 启用自动裁剪--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 # portrait animation
python inference.py -s assets/examples/source/s13.mp4 -d assets/examples/driving/d5.pkl # portrait video editing

4. Gradio 可视化界面操作

在Gradio的可视化界面下可以获得更好的体验,适合新手使用,只需运行下面安装代码即可:

# For Linux and Windows users (and macOS with Intel??)
python app.py

# For macOS with Apple Silicon users, Intel not supported, this maybe 20x slower than RTX 4090
PYTORCH_ENABLE_MPS_FALLBACK=1 python app.py

您可以指定--server_port、、--share参数--server_name来满足您的需求!

🚀 它们还提供了加速选项--flag_do_torch_compile。首次推理会触发优化过程(约一分钟),使后续推理速度提高 20-30%。性能提升可能因 CUDA 版本的不同而有所差异。

# enable torch.compile for faster inference
python app.py --flag_do_torch_compile

注意:Windows 和 macOS 不支持此方法。或者,在HuggingFace上轻松尝试一下🤗

5. 推理速度评估

 

下方提供了一个脚本来评估每个模块的推理速度:

# For NVIDIA GPU
python speed.py

以下是使用原生 PyTorch 框架在 RTX 4090 GPU 上推断一帧的结果torch.compile

模型 参数(米) 模型大小(MB) 推理(毫秒)
外观特征提取器 0.84 3.3 0.82
运动提取器 28.12 108 0.84
铲形发电机 55.37 212 7.59
变形模块 45.53 174 5.21
拼接和重定向模块 0.23 2.3 0.31

注意:拼接和重定向模块的值代表三个连续 MLP 网络的组合参数数量和总推理时间。

当然如果你没有一张好的显卡,无法本地运行,那么可以在huggingface上免费体验:【点击前往】在线使用

详细的LivePortrait安装教程如下:

THE END
喜欢就支持一下吧
点赞2179 分享
Windows 11 强制联网登录? 附最新解决方法,无需登入微软账户也能安装系统! |零度解说-零度博客

Windows 11 强制联网登录? 附最新解决方法,无需登入微软账户也能安装系统! |零度解说

https://youtu.be/bIWo3FOHYIM ============= Windows 11 系统官方正版 iSO镜像下载:https://www.freedidi.com/11106.html
admin的头像-零度博客admin
1.7W+1682
Windows 10 最新版 太阳谷(21H2) 安装教程!它真的是史上最好用的WIN10系统吗?| 零度解说-零度博客
随着仇恨泛滥,大量大广告商在 Twitter 上大打出手-零度博客

随着仇恨泛滥,大量大广告商在 Twitter 上大打出手

一份新报告称,超过三分之一的 Twitter 最大广告商现在已经从该平台撤下了他们的广告,因为新所有者不稳定和不可预测的性质,加上他对仇恨的含蓄鼓励,使得该网站越来越不欢迎有钱花的品牌。 根...
admin的头像-零度博客admin
1.6W+1091
OpenAI正式推出自己的搜索引擎 基于AI技术的ChatGPT Search-零度博客

OpenAI正式推出自己的搜索引擎 基于AI技术的ChatGPT Search

此前已经有传闻称 OpenAI 正在开发自己的搜索引擎,这种基于人工智能的搜索引擎要比传统搜索引擎例如谷歌提供更好的体验,后来 OpenAI 也证实了这种传言,因为推出 SearchGPT waitlist。现在 Ch...
admin的头像-零度博客admin
86803506
2023 年最值得推荐的 7 款免费应用程序! | 零度解说-零度博客

2023 年最值得推荐的 7 款免费应用程序! | 零度解说

https://youtu.be/t2TqeNaYiXo =========== 7款免费应用下载:https://www.freedidi.com/11159.html
admin的头像-零度博客admin
1.7W+1682
Windows 提供的最好用的数据恢复工具!含图形界面-零度博客

Windows 提供的最好用的数据恢复工具!含图形界面

WinFR 界面版:【点击下载】 微软官方版:【点击下载】 彻底删除工具 Permadelete:【点击下载】
admin的头像-零度博客admin
1.7W+2252
鲜为人知!但是很有意思的网站!这5个你都知道吗? | 零度解说-零度博客
2021年最值得推荐的五款免费精品软件-零度博客

2021年最值得推荐的五款免费精品软件

https://youtu.be/95vdNVQDaTk 火绒安全:https://www.huorong.cn/person5.html Recuva:https://www.ccleaner.com/recuva PotPlayer:https://potplayer.daum.net/ BleachBit:https://www.ble...
Google Hangouts 的死亡,Google 与 iMessage  迎来最后一个最佳的竞争机会-零度博客

Google Hangouts 的死亡,Google 与 iMessage 迎来最后一个最佳的竞争机会

谷歌环聊定于今天死亡。自 7 月以来,这款手机应用程序一直在单独引导人们退出服务,但网络应用程序Hangouts 的最后遗迹今天将被关闭。 在短时间内,Hangouts 是 Google 最好、最雄心勃勃、最受...
admin的头像-零度博客admin
1.6W+2250