图片换脸 Pro

namifusion/faceswap-image-pro

Image FaceSwap Pro 支持单人自动检测与多人关键点精确映射。

示例

图片换脸 Pro example 1

参数

名称类型默认约束说明
sourceImage *源图textarea支持 URL 字符串(单人模式)或 ImageWithKeypoints 数组(多人模式)。
targetImage *目标图textarea支持 URL 字符串(单人模式)或 ImageWithKeypoints 数组(多人模式)。
face_enhance面部优化booleanfalse仅支持布尔值。开启后对换脸结果进行额外面部优化。

输出字段

字段类型说明
image_urlstringImage FaceSwap Pro result image URL

API

通过统一 REST API 调用本模型;在 API Keys 页获取密钥。

cURL
# 1) Submit — returns { "task_uuid": "..." }
curl -X POST "https://www.namifusion.com/api/v1/marketplace/run/namifusion/faceswap-image-pro" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "input": {
    "sourceImage": "https://kaito-1328216764.cos.ap-tokyo.myqcloud.com/marketplace/thumbnails/2026-01-27/d0328839a75f.png",
    "targetImage": "https://kaito-1328216764.cos.ap-tokyo.myqcloud.com/marketplace/thumbnails/2026-02-24/94a3eefdb397.png",
    "face_enhance": false
  }
}'

# 2) Poll until status is "completed", then read the output URLs
curl "https://www.namifusion.com/api/v1/marketplace/run/tasks/TASK_UUID" \
  -H "Authorization: Bearer YOUR_API_KEY"
Python
import time, requests

API_KEY = "YOUR_API_KEY"
HEADERS = {"Authorization": f"Bearer {API_KEY}"}

# 1) Submit
resp = requests.post(
    "https://www.namifusion.com/api/v1/marketplace/run/namifusion/faceswap-image-pro",
    headers=HEADERS,
    json={
        "input": {
            "sourceImage": "https://kaito-1328216764.cos.ap-tokyo.myqcloud.com/marketplace/thumbnails/2026-01-27/d0328839a75f.png",
            "targetImage": "https://kaito-1328216764.cos.ap-tokyo.myqcloud.com/marketplace/thumbnails/2026-02-24/94a3eefdb397.png",
            "face_enhance": False
        }
    },
)
resp.raise_for_status()  # 401/402/429/5xx stop here instead of polling a bad task
task = resp.json()

# 2) Poll until a terminal state (completed / failed / cancelled).
#    This model is allowed up to 300s server-side.
deadline = time.time() + 360
while task.get("status") not in ("completed", "failed", "cancelled"):
    if time.time() > deadline:
        raise TimeoutError(f"still {task.get('status')} — keep the task_uuid and poll later")
    time.sleep(3)
    poll = requests.get(f"https://www.namifusion.com/api/v1/marketplace/run/tasks/{task['task_uuid']}", headers=HEADERS)
    poll.raise_for_status()
    task = poll.json()

print(task["status"], task.get("output"))
JavaScript
const API_KEY = "YOUR_API_KEY";
const HEADERS = { Authorization: `Bearer ${API_KEY}` };

// 1) Submit
const resp = await fetch("https://www.namifusion.com/api/v1/marketplace/run/namifusion/faceswap-image-pro", {
  method: "POST",
  headers: { ...HEADERS, "Content-Type": "application/json" },
  body: JSON.stringify({
    "input": {
      "sourceImage": "https://kaito-1328216764.cos.ap-tokyo.myqcloud.com/marketplace/thumbnails/2026-01-27/d0328839a75f.png",
      "targetImage": "https://kaito-1328216764.cos.ap-tokyo.myqcloud.com/marketplace/thumbnails/2026-02-24/94a3eefdb397.png",
      "face_enhance": false
    }
  }),
});
if (!resp.ok) throw new Error(`submit failed: ${resp.status} ${await resp.text()}`);
let task = await resp.json();

// 2) Poll until a terminal state (completed / failed / cancelled).
//    This model is allowed up to 300s server-side.
const deadline = Date.now() + 360 * 1000;
while (!["completed", "failed", "cancelled"].includes(task.status)) {
  if (Date.now() > deadline) throw new Error(`still ${task.status} — keep the task_uuid and poll later`);
  await new Promise((r) => setTimeout(r, 3000));
  const poll = await fetch(`https://www.namifusion.com/api/v1/marketplace/run/tasks/${task.task_uuid}`, { headers: HEADERS });
  if (!poll.ok) throw new Error(`poll failed: ${poll.status}`);
  task = await poll.json();
}

console.log(task.status, task.output);

文档

NamiFusion Image FaceSwap Pro

AI 图片换脸 Pro:支持单人自动换脸与多人关键点精确映射,适用于高可控换脸场景。

NamiFusion Image FaceSwap Pro 是一个面向高精度场景的图片换脸接口。它支持两类输入方式:单人自动模式(字符串 URL)与多人精确模式(对象数组 + 关键点),可在不同业务场景下平衡易用性与可控性。


核心特性

  • 单人自动换脸: sourceImagetargetImage 直接传字符串 URL,系统自动做人脸关键点检测。
  • 多人精确映射: 使用 ImageWithKeypoints[] 并传入 opts 关键点,按索引一一配对处理。
  • 可选面部优化: face_enhance 仅支持布尔值,开启后会移除脸部瑕疵,提升面部观感。
  • 统一输入结构: 支持字符串与对象数组两种输入形态。

技术规格

参数详情
核心输入字段sourceImagetargetImageface_enhance
图片对象结构ImageWithKeypointspath + opts
单人模式输入字符串 URL / 路径
多人模式输入ImageWithKeypoints[] 数组

快速开始

API 端点

端点方法说明
/api/v1/marketplace/run/namifusion/faceswap-image-proPOST提交 FaceSwap Pro 任务
/api/v1/marketplace/run/tasks/{task_uuid}GET查询任务状态与结果

请求参数总览

请求体中与 FaceSwap Pro 相关的核心字段如下:

{
  "sourceImage": "string | ImageWithKeypoints[]",
  "targetImage": "string | ImageWithKeypoints[]",
  "face_enhance": "boolean"
}

认证

在请求 Header 中携带 API Key(如你的服务网关要求):

X-API-Key: sk-your-api-key

标准调用流程

FaceSwap Pro 采用异步任务模式:

  1. 调用 POST /api/v1/marketplace/run/namifusion/faceswap-image-pro 提交任务。
  2. 从响应中拿到 task_uuid
  3. 调用 GET /api/v1/marketplace/run/tasks/{task_uuid} 轮询任务状态。
  4. status 变为 completed 时,从响应中的 output 读取结果;若为 failed,查看 error_message

第 1 步:提交任务

curl -X POST "https://www.namifusion.com/api/v1/marketplace/run/namifusion/faceswap-image-pro" \
  -H "X-API-Key: sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "input": {
      "sourceImage": "https://example.com/source.jpg",
      "targetImage": "https://example.com/target.jpg",
      "face_enhance": false
    }
  }'

提交成功响应示例:

{
  "task_uuid": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "status": "pending",
  "estimated_time": 150,
  "cost_credits": 10
}

第 2 步:查询结果

curl -X GET "https://www.namifusion.com/api/v1/marketplace/run/tasks/a1b2c3d4-e5f6-7890-abcd-ef1234567890" \
  -H "X-API-Key: sk-your-api-key"

处理中响应示例:

{
  "task_uuid": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "model_id": "namifusion/faceswap-image-pro",
  "status": "processing",
  "output": null,
  "error_message": null,
  "created_at": "2026-04-13T10:00:00Z",
  "completed_at": null
}

完成响应示例:

{
  "task_uuid": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "model_id": "namifusion/faceswap-image-pro",
  "status": "completed",
  "output": {
    "image_url": "https://cdn.namifusion.com/result/faceswap_pro_abc123.jpg"
  },
  "cost_credits": 10,
  "error_message": null,
  "created_at": "2026-04-13T10:00:00Z",
  "completed_at": "2026-04-13T10:00:12Z"
}

失败响应示例:

{
  "task_uuid": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "model_id": "namifusion/faceswap-image-pro",
  "status": "failed",
  "output": null,
  "cost_credits": 10,
  "error_message": "Face swap failed because no valid face was detected in targetImage.",
  "created_at": "2026-04-13T10:00:00Z",
  "completed_at": "2026-04-13T10:00:08Z"
}

前置步骤:调用 Detect Faces 组装 ImageWithKeypoints

在多人换脸场景中,推荐先调用人脸检测接口,拿到每张脸的 landmarks_str,再组装为 FaceSwap Pro 需要的 opts 字段。

第 1 步:调用 Detect Faces

检测接口(异步):

  • POST /api/v1/marketplace/run/namifusion/detect_faces
  • GET /api/v1/marketplace/run/tasks/{task_uuid}

请求示例(检测目标图):

curl -X POST "https://www.namifusion.com/api/v1/marketplace/run/namifusion/detect_faces" \
  -H "X-API-Key: sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "input": {
      "url": "https://example.com/group_target.jpg",
      "return_face_url": true
    }
  }'

检测完成后,可从 output.faces_obj["0"] 读取关键字段:

  • landmarks_str[i]:可直接作为 opts
  • face_urls[i]:可作为单脸裁剪图地址(适合 source 侧)
  • region[i]:人脸框信息(排查和可视化用)

检测结果片段示例:

{
  "output": {
    "faces_obj": {
      "0": {
        "landmarks_str": [
          "402,486:614,489:511,626:506,702",
          "120,85:180,88:150,130:150,165"
        ],
        "face_urls": [
          "https://example.com/faces/source_face_0.jpg",
          "https://example.com/faces/source_face_1.jpg"
        ],
        "region": [
          [278, 219, 442, 640],
          [80, 50, 150, 180]
        ]
      }
    }
  }
}

第 2 步:组装 ImageWithKeypoints

ImageWithKeypoints 结构如下:

{
  "path": "https://example.com/face_or_image.jpg",
  "opts": "402,486:614,489:511,626:506,702"
}

组装规则建议:

  • sourceImage: 推荐使用检测返回的 face_urls[i] 作为 pathlandmarks_str[i] 作为 opts
  • targetImage: 可使用同一张目标图 URL 作为每项 path,并用目标侧对应人脸的 landmarks_str[i] 作为 opts
  • 保证 sourceImage[i]targetImage[i] 是同一组替换关系

组装示例:

{
  "sourceImage": [
    {
      "path": "https://example.com/faces/source_face_0.jpg",
      "opts": "402,486:614,489:511,626:506,702"
    }
  ],
  "targetImage": [
    {
      "path": "https://example.com/group_target.jpg",
      "opts": "120,85:180,88:150,130:150,165"
    }
  ],
  "face_enhance": true
}

场景一:单人换脸(自动关键点检测)

适用于源图和目标图均只有一张人脸的场景。此时 sourceImagetargetImage 可直接使用字符串。

请求示例

{
  "sourceImage": "https://example.com/source.jpg",
  "targetImage": "https://example.com/target.jpg",
  "face_enhance": false
}

行为说明

  • sourceImagetargetImage 为字符串时,校验层会自动转换为:
    • [{"path": "<value>", "opts": ""}]
  • opts 为空字符串表示未手动传关键点。
  • 管线将进入自动关键点检测流程(单对人脸处理)。

场景二:多人换脸(手动关键点映射)

适用于需要显式指定人脸对应关系的场景。此时应同时传入 sourceImagetargetImage 的对象数组,并严格按索引建立映射关系:sourceImage[0] 对应 targetImage[0]sourceImage[1] 对应 targetImage[1],依此类推。

补充说明:sourceImage 可以传入多张不同的源图片,用于提供多个人脸来源;但 targetImage 应保持为同一张目标图片。最终生成结果会统一基于这张 targetImage 输出。

请求示例

{
  "sourceImage": [
    {
      "path": "https://example.com/source_face_1.jpg",
      "opts": "145.2,210.8:188.1,209.6:166.7,241.3:165.9,272.4"
    },
    {
      "path": "https://example.com/source_face_2.jpg",
      "opts": "320.4,198.2:360.8,197.0:340.2,228.1:339.7,258.6"
    }
  ],
  "targetImage": [
    {
      "path": "https://example.com/target.jpg",
      "opts": "512.1,301.4:548.9,299.8:530.0,330.2:529.0,360.5"
    },
    {
      "path": "https://example.com/target.jpg",
      "opts": "710.2,288.6:748.7,287.1:729.9,317.7:729.1,349.2"
    }
  ],
  "face_enhance": true
}

行为说明

  • 管线以 zip(sourceImage, targetImage) 方式逐对处理,即两个数组会按相同索引一一配对。
  • 多对处理时,每一对都应在源图和目标图两侧提供有效 opts
  • 若关键点缺失或格式异常,可能触发运行时校验错误。

参数与返回值详解

请求参数

顶层字段

参数类型必填说明
sourceImagestring | ImageWithKeypoints[]源图输入。字符串用于单人自动模式;对象数组用于多人精确模式。
targetImagestring | ImageWithKeypoints[]目标图输入。字符串用于单人自动模式;对象数组用于多人精确模式。
face_enhanceboolean面部优化开关,仅支持布尔值。

ImageWithKeypoints 结构

字段类型必填说明
pathstring图片 URL 或本地路径。
optsstring关键点字符串,格式为 "x1,y1:x2,y2:x3,y3:x4,y4"。空字符串表示不传手动关键点。

校验规则与常见错误

face_enhance

  • 接受:true / false
  • 拒绝:任意整数(包括 0 / 1)及其他非布尔类型

sourceImage / targetImage

  • 支持字符串输入,内部会自动转为单元素对象数组。
  • 数组输入必须符合 ImageWithKeypoints 结构。
  • 多人模式下建议两侧数组长度一致并按索引对齐。

opts

  • 格式必须满足 x,y:x,y:...
  • 点位之间使用 : 分隔,每个点使用 x,y 表示
  • 多人模式下每一对都必须提供非空 opts

参数对输出结果的影响

参数组合对结果的影响
sourceImage/targetImage 为字符串进入单人自动模式,系统自动检测关键点。
sourceImage/targetImage 为对象数组 + 有效 opts进入多人精确模式,按索引一一配对换脸。
face_enhance: true启用面部优化,通常可提升面部观感。
face_enhance: false不启用额外面部优化,走默认换脸流程。

注意事项

  1. 多人模式必须对齐: sourceImage[i] 会与 targetImage[i] 配对处理,请保持数组长度和顺序一致。
  2. 关键点格式严格: opts 不符合 x,y:x,y:... 时会解析失败。
  3. 字符串输入会被自动封装: 若你需要精确控制人脸映射,请显式使用对象数组并传 opts
  4. 建议优先使用公开可访问 URL: 便于服务侧稳定拉取图片资源。