> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/talking-heads/liveportrait.md).

# LivePortrait

在 Clore.ai 上从单张图像创建逼真的动画肖像

从单张图像生成逼真的动画肖像。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

{% hint style="info" %}
本指南中的所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace) 市场。
{% endhint %}

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、VRAM 和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价）
4. 配置你的订单：
   * 选择 Docker 镜像
   * 设置端口（SSH 用 TCP，Web UI 用 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问你的服务器

* 在以下位置查找连接信息 **我的订单**
* Web 界面：使用 HTTP 端口 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 什么是 LivePortrait？

快手的 LivePortrait 支持：

* 使用驱动视频为任意肖像生成动画
* 单张照片转视频动画
* 表情与姿态迁移
* 支持实时推理

## 资源

* **GitHub：** [KwaiVGI/LivePortrait](https://github.com/KwaiVGI/LivePortrait)
* **论文：** [LivePortrait 论文](https://arxiv.org/abs/2407.03168)
* **HuggingFace：** [KwaiVGI/LivePortrait](https://huggingface.co/KwaiVGI/LivePortrait)
* **演示：** [HuggingFace Space](https://huggingface.co/spaces/KwaiVGI/LivePortrait)

## 推荐硬件

| 组件  | 最低           | 推荐            | 最佳            |
| --- | ------------ | ------------- | ------------- |
| GPU | RTX 3070 8GB | RTX 4080 16GB | RTX 4090 24GB |
| 显存  | 8GB          | 16GB          | 24GB          |
| CPU | 4 核          | 8 核           | 16 核          |
| 内存  | 16GB         | 32GB          | 64GB          |
| 存储  | 30GB SSD     | 50GB NVMe     | 100GB NVMe    |
| 网络  | 100 Mbps     | 500 Mbps      | 1 Gbps        |

## 在 CLORE.AI 上快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel
```

**端口：**

```
22/tcp
7860/http
```

**命令：**

```bash
cd /workspace && \
git clone https://github.com/KwaiVGI/LivePortrait.git && \
cd LivePortrait && \
pip install -r requirements.txt && \
python app.py
```

## 访问你的服务

部署后，找到你的 `http_pub` URL 在 **我的订单**:

1. 前往 **我的订单** 页面
2. 点击你的订单
3. 找到 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 替代 `localhost` 在下面的示例中。

## 安装

```bash
git clone https://github.com/KwaiVGI/LivePortrait.git
cd LivePortrait
pip install -r requirements.txt

# 下载预训练模型
huggingface-cli download KwaiVGI/LivePortrait --local-dir pretrained_weights
```

## 你可以创建什么

### 虚拟头像

* AI 网红和虚拟主播
* 客服头像
* 教育演示讲师

### 内容创作

* 社交媒体内容
* 营销材料
* 音乐视频概念

### 娱乐

* 为历史照片生成动画
* 角色动画
* 互动体验

### 专业用途

* 视频会议头像
* 演示助手
* 训练模拟

## 基础用法

### 命令行

```bash
python inference.py \
    --source_image path/to/portrait.jpg \
    --driving_video path/to/driving.mp4 \
    --output_path output.mp4
```

### Python API

```python
from liveportrait import LivePortraitPipeline

# 初始化管道
pipeline = LivePortraitPipeline(
    device="cuda",
    model_path="./pretrained_weights"
)

# 为肖像生成动画
result = pipeline.animate(
    source_image="portrait.jpg",
    driving_video="driving.mp4"
)

result.save("animated_portrait.mp4")
```

## 带表情控制的肖像

```python
from liveportrait import LivePortraitPipeline
import cv2

pipeline = LivePortraitPipeline(device="cuda")

# 控制特定表情
expressions = {
    "smile": 0.8,
    "eyebrow_raise": 0.3,
    "head_pitch": -5,  # 角度
    "head_yaw": 10
}

result = pipeline.animate_with_expression(
    source_image="portrait.jpg",
    expressions=expressions,
    num_frames=60,
    fps=30
)

result.save("expression_controlled.mp4")
```

## 批量处理

```python
import os
from liveportrait import LivePortraitPipeline

pipeline = LivePortraitPipeline(device="cuda")

# 使用相同的驱动视频为多个肖像生成动画
portraits = [
    "portrait1.jpg",
    "portrait2.jpg",
    "portrait3.jpg"
]

driving = "speech_driving.mp4"
output_dir = "./animated"
os.makedirs(output_dir, exist_ok=True)

for i, portrait in enumerate(portraits):
    print(f"处理中 {i+1}/{len(portraits)}：{portrait}")

    result = pipeline.animate(
        source_image=portrait,
        driving_video=driving
    )

    result.save(f"{output_dir}/animated_{i:03d}.mp4")
```

## Gradio 界面

```python
import gradio as gr
from liveportrait import LivePortraitPipeline
import tempfile

pipeline = LivePortraitPipeline(device="cuda")

def animate(source_image, driving_video):
    with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as f:
        result = pipeline.animate(
            source_image=source_image,
            driving_video=driving_video
        )
        result.save(f.name)
        return f.name

demo = gr.Interface(
    fn=animate,
    inputs=[
        gr.Image(type="filepath", label="肖像图像"),
        gr.Video(label="驱动视频")
    ],
    outputs=gr.Video(label="动画肖像"),
    title="LivePortrait - 为任意肖像生成动画",
    description="上传一张肖像和一个驱动视频，即可创建动画视频。运行在 CLORE.AI GPU 服务器上。"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## 实时摄像头动画

```python
import cv2
from liveportrait import LivePortraitPipeline

pipeline = LivePortraitPipeline(device="cuda")

# 加载源肖像
source = cv2.imread("portrait.jpg")
pipeline.set_source(source)

# 打开摄像头
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # 使用当前帧作为驱动生成动画
    animated = pipeline.animate_frame(frame)

    cv2.imshow("LivePortrait", animated)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
```

## 与 TTS 集成

使用文本转语音创建会说话的头像：

```python
from liveportrait import LivePortraitPipeline
from TTS.api import TTS

# 生成语音
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
tts.tts_to_file(
    text="你好！欢迎来到我们的演示。",
    file_path="speech.wav",
    speaker_wav="reference_voice.wav",
    language="en"
)

# 根据音频生成口型同步驱动视频

# （使用单独的口型同步工具或预制驱动视频）

# 为肖像生成动画
pipeline = LivePortraitPipeline(device="cuda")
result = pipeline.animate(
    source_image="presenter.jpg",
    driving_video="lip_sync_driving.mp4"
)
result.save("talking_avatar.mp4")
```

## 性能

| 分辨率     | GPU      | FPS | 延迟   |
| ------- | -------- | --- | ---- |
| 256x256 | RTX 3070 | 30  | 33毫秒 |
| 256x256 | RTX 4090 | 60+ | 16毫秒 |
| 512x512 | RTX 4090 | 30  | 33毫秒 |
| 512x512 | A100     | 45  | 22毫秒 |

## 常见问题与解决方案

### 未检测到人脸

**问题：** "源图像中未检测到人脸"

**解决方案：**

* 确保人脸清晰可见且正面朝向
* 使用光线良好的源图像
* 裁剪图像，使焦点集中在人脸上
* 最小人脸尺寸：128x128 像素

### 动作不匹配

**问题：** 动画未跟随驱动视频

**解决方案：**

* 使用面部动作清晰的驱动视频
* 确保驱动视频中的人脸朝向相似
* 尝试不同的驱动视频

### 质量问题

**问题：** 输出看起来模糊或失真

**解决方案：**

```python

# 使用更高质量的设置
result = pipeline.animate(
    source_image=source,
    driving_video=driving,
    output_size=512,  # 更高分辨率
    enhance_face=True  # 启用人脸增强
)
```

### 实时卡顿

**问题：** 摄像头动画有延迟

**解决方案：**

* 使用更小的输出分辨率（256x256）
* 启用 TensorRT 优化
* 使用 RTX 4090 或更高显卡以实现实时效果

```python
pipeline = LivePortraitPipeline(
    device="cuda",
    use_tensorrt=True  # 启用 TensorRT
)
```

### 音频同步问题

**问题：** 口型与音频不匹配

**解决方案：**

* 使用音频转驱动视频生成
* 在后期处理中调整视频时序
* 使用 Wav2Lip 获得更好的口型同步

## 故障排查

### 未检测到人脸

* 确保源图像中的人脸清晰可见
* 使用正面照片
* 检查图像分辨率（建议 512+）

### 动画看起来不自然

* 源图像和驱动视频应具有相似的人脸角度
* 避免驱动视频中出现极端表情
* 使用更短的驱动片段

### 输出视频已损坏

* 安装 ffmpeg： `apt install ffmpeg`
* 检查输出格式兼容性
* 确保磁盘空间充足

### CUDA 错误

* 安装兼容的 PyTorch 版本
* 检查 CUDA 版本是否满足要求

## 成本估算

CLORE.AI 市场常见费率（截至 2024 年）：

| GPU       | 小时费率    | 日费率     | 4 小时会话  |
| --------- | ------- | ------- | ------- |
| RTX 3060  | \~$0.03 | \~$0.70 | \~$0.12 |
| RTX 3090  | \~$0.06 | \~$1.50 | \~$0.25 |
| RTX 4090  | \~$0.10 | \~$2.30 | \~$0.40 |
| A100 40GB | \~$0.17 | \~$4.00 | \~$0.70 |
| A100 80GB | \~$0.25 | \~$6.00 | \~$1.00 |

*价格因提供商和需求而异。请查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前费率。*

**节省费用：**

* 使用 **竞价** 可中断工作市场——约三分之一的服务器将现货价格定得低于按需价格（中位数约优惠 13%），其余则与按需价格持平
* 使用 **CLORE** 代币支付
* 比较不同提供商的价格

## 下一步

* [SadTalker](/guides/guides_v2-zh/talking-heads/sadtalker.md) - 其他说话头方案
* [Wav2Lip](/guides/guides_v2-zh/talking-heads/wav2lip.md) - 更好的口型同步
* [XTTS](/guides/guides_v2-zh/yin-pin-yu-yu-yin/xtts-coqui.md) - 语音生成


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/talking-heads/liveportrait.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
