> 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/ren-lian-yu-shen-fen/facefusion.md).

# FaceFusion

在 Clore.ai GPU 上进行专业的人脸交换与增强

专业的人脸交换与增强工具。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 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>`

## 什么是 FaceFusion？

FaceFusion 提供：

* 高质量人脸交换
* 人脸增强
* 年龄修改
* 表情迁移
* 视频处理

## 资源

* **GitHub：** [facefusion/facefusion](https://github.com/facefusion/facefusion)
* **文档：** [docs.facefusion.io](https://docs.facefusion.io/)
* **Discord：** [FaceFusion 社区](https://discord.gg/facefusion)

## 推荐硬件

| 组件  | 最低            | 推荐            | 最佳            |
| --- | ------------- | ------------- | ------------- |
| GPU | RTX 3060 12GB | RTX 4080 16GB | RTX 4090 24GB |
| 显存  | 8GB           | 16GB          | 24GB          |
| CPU | 8 核           | 16 核          | 32 核          |
| 内存  | 16GB          | 32GB          | 64GB          |
| 存储  | 50GB SSD      | 100GB NVMe    | 200GB NVMe    |
| 互联网 | 100 Mbps      | 500 Mbps      | 1 Gbps        |

## 在 CLORE.AI 上快速部署

**Docker 镜像：**

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

**端口：**

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

**命令：**

```bash
pip install facefusion && \
facefusion run --ui-layouts default
```

## 访问你的服务

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

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

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

## 安装

```bash

# 安装 FaceFusion
pip install facefusion

# 或从源代码安装
git clone https://github.com/facefusion/facefusion.git
cd facefusion
pip install -r requirements.txt
```

## 你可以创建的内容

### 娱乐

* 电影配音预备
* 粉丝制作内容
* 表情包制作

### 专业用途

* 时尚虚拟试穿
* 个性化营销
* 视频隐私保护

### 创意项目

* 艺术装置
* 音乐视频特效
* 短片制作

**重要：** 始终负责任地并在获得同意的情况下使用。

## 基础用法

### 网页界面

```bash

# 启动 Web UI
facefusion run --ui-layouts default

# 访问 http://localhost:7860
```

### 命令行

```bash

# 单张图片人脸交换
facefusion headless-run \
    --source-paths source_face.jpg \
    --target-path target_image.jpg \
    --output-path result.jpg \
    --face-swapper-model inswapper_128

# 视频人脸交换
facefusion headless-run \
    --source-paths source_face.jpg \
    --target-path input_video.mp4 \
    --output-path output_video.mp4 \
    --face-swapper-model inswapper_128 \
    --execution-providers cuda
```

### Python API

```python
from facefusion import core
from facefusion.processors.frame.face_swapper import FaceSwapper

# 初始化
swapper = FaceSwapper(
    model="inswapper_128",
    device="cuda"
)

# 处理图像
result = swapper.process(
    source_image="source_face.jpg",
    target_image="target.jpg"
)

result.save("swapped.jpg")
```

## 人脸增强

```bash

# 增强人脸质量
facefusion headless-run \
    --target-path input.jpg \
    --output-path enhanced.jpg \
    --face-enhancer-model gfpgan_1.4 \
    --face-enhancer-blend 80
```

## 多张人脸

```bash

# 交换指定人脸
facefusion headless-run \
    --source-paths face1.jpg face2.jpg \
    --target-path group_photo.jpg \
    --output-path result.jpg \
    --face-selector-mode reference \
    --reference-face-position 0 1
```

## 视频处理

```bash

# 带增强的完整视频人脸交换
facefusion headless-run \
    --source-paths source_face.jpg \
    --target-path input_video.mp4 \
    --output-path output_video.mp4 \
    --face-swapper-model inswapper_128 \
    --face-enhancer-model gfpgan_1.4 \
    --execution-providers cuda \
    --execution-thread-count 4 \
    --video-encoder libx264 \
    --video-quality 18
```

## 批量处理

```python
import os
from facefusion import core
from facefusion.processors.frame.face_swapper import FaceSwapper

swapper = FaceSwapper(model="inswapper_128", device="cuda")

# 源人脸
source = "my_face.jpg"

# 目标图像
targets = [f for f in os.listdir("./targets") if f.endswith(('.jpg', '.png'))]

os.makedirs("./results", exist_ok=True)

for target in targets:
    print(f"Processing: {target}")

    result = swapper.process(
        source_image=source,
        target_image=f"./targets/{target}"
    )

    result.save(f"./results/swapped_{target}")
```

## Gradio 自定义界面

```python
import gradio as gr
from facefusion.processors.frame.face_swapper import FaceSwapper

swapper = FaceSwapper(model="inswapper_128", device="cuda")

def swap_faces(source_image, target_image, enhance):
    result = swapper.process(
        source_image=source_image,
        target_image=target_image,
        enhance=enhance
    )
    return result

demo = gr.Interface(
    fn=swap_faces,
    inputs=[
        gr.Image(type="filepath", label="源人脸"),
        gr.Image(type="filepath", label="目标图像"),
        gr.Checkbox(label="增强结果", value=True)
    ],
    outputs=gr.Image(label="结果"),
    title="FaceFusion - 人脸交换",
    description="在 CLORE.AI 服务器上进行专业人脸交换"
)

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

## 质量设置

### 交换模型

| 模型                   | 质量 | 速度 | 显存   |
| -------------------- | -- | -- | ---- |
| inswapper\_128       | 好  | 快  | 4GB  |
| inswapper\_128\_fp16 | 好  | 更快 | 2GB  |
| simswap\_256         | 更好 | 中等 | 6GB  |
| simswap\_512         | 最佳 | 慢  | 10GB |

### 增强模型

| 模型             | 质量 | 速度 |
| -------------- | -- | -- |
| gfpgan\_1.4    | 很高 | 中等 |
| codeformer     | 最佳 | 慢  |
| gpen\_bfr\_512 | 好  | 快  |

## 性能

| 任务       | 分辨率       | GPU      | 时间    |
| -------- | --------- | -------- | ----- |
| 单张图片     | 1024x1024 | RTX 3090 | 0.5 秒 |
| 单张图片     | 1024x1024 | RTX 4090 | 0.3 秒 |
| 视频（1 分钟） | 1080p     | RTX 4090 | 5 分钟  |
| 视频（1 分钟） | 1080p     | A100     | 3 分钟  |

## 常见问题与解决方案

### 未检测到人脸

**问题：** “在源图像/目标图像中未找到人脸”

**解决方案：**

* 确保人脸清晰可见
* 人脸至少应为 64x64 像素
* 使用正面或轻微角度的照片
* 需要良好光照

### 交换质量差

**问题：** 结果看起来不自然

**解决方案：**

```bash

# 使用更好的模型和增强
facefusion headless-run \
    --source-paths source.jpg \
    --target-path target.jpg \
    --output-path result.jpg \
    --face-swapper-model simswap_256 \
    --face-enhancer-model gfpgan_1.4 \
    --face-enhancer-blend 80
```

### 视频处理缓慢

**问题：** 处理视频耗时过长

**解决方案：**

* 使用 CUDA 执行提供程序
* 增加线程数
* 使用 fp16 模型以提升速度
* 在 A100 上处理以获得最佳性能

```bash
facefusion headless-run \
    --execution-providers cuda \
    --execution-thread-count 8 \
    --face-swapper-model inswapper_128_fp16
```

### 颜色不匹配

**问题：** 交换后的人脸颜色不同

**解决方案：**

* 启用人脸颜色校正
* 使源图像与目标图像的光照匹配
* 使用色彩分级进行后期处理

### 内存问题

**问题：** 视频内存不足

**解决方案：**

```bash

# 降低内存占用
facefusion headless-run \
    --video-memory-strategy tolerant \
    --execution-thread-count 2
```

## 故障排查

### 人脸交换质量差

* 使用更高分辨率的源图像
* 确保两张图像都有良好光照
* 尝试不同的人脸增强模型

### “未检测到人脸”

* 人脸必须清晰可见
* 避免侧脸
* 检查图像是否过小

### 视频处理卡住

* 大视频需要更多 RAM
* 分成更小的片段处理
* 为临时文件使用 SSD 存储

### 模型未下载

* 检查网络连接
* 使用 `--download-models` 标志
* 从 HuggingFace 手动下载

## 成本估算

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** 代币支付
* 比较不同提供商的价格

## 下一步

* [InstantID](/guides/guides_v2-zh/ren-lian-yu-shen-fen/instantid.md) - 身份保留
* [LivePortrait](/guides/guides_v2-zh/talking-heads/liveportrait.md) - 人像动画
* [GFPGAN](/guides/guides_v2-zh/tu-xiang-chu-li/gfpgan-face-restore.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/ren-lian-yu-shen-fen/facefusion.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.
