如何实现AI一键脱衣与一键穿衣(TryOn)

1,959 阅读3分钟

Virtual Try-On 是一个结合 AI 与时尚的炫酷工具,它可以让我们通过 AI 模型将指定的服装试穿在照片中的人物身上。以下是对该技术的通俗解释,以及如何动手实现一个 Virtual Try-On 工具的完整步骤。


什么是 Virtual Try-On?

Virtual Try-On 本质上是利用 AI 图像生成技术,将原始人物照片中的服装替换为指定的衣物图像。

image.png

  • 传统方法:基于文本描述进行服装替换,比如输入“粉色连衣裙”。
  • IP-Adapter 的新方法:不仅能输入文本描述,还可以直接提供服装的图片(如某件具体的粉色连衣裙),实现更加精确和真实的效果。

image.png

应用实例
想象你在购物时看到一件心仪的衣服,但不知道是否适合自己。Virtual Try-On 能让你用照片尝试穿这件衣服,帮你更直观地做决定。


实现 Virtual Try-On 的步骤

我们基于 Hugging Face Spaces 提供的工具和 IP-Adapter,来实现 Virtual Try-On 工具。

1. 安装依赖库

首先,需要安装必要的 Python 库:

pip install diffusers accelerate

2. 加载所需模型

加载 VAE(减少内存占用)

from diffusers import AutoPipelineForInpainting, AutoencoderKL
import torch

vae = AutoencoderKL.from_pretrained(
    "madebyollin/sdxl-vae-fp16-fix", 
    torch_dtype=torch.float16
)

加载 Stable Diffusion XL 1.0 的 Inpainting 模型

pipeline = AutoPipelineForInpainting.from_pretrained(
    "diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
    vae=vae,
    torch_dtype=torch.float16,
    variant="fp16",
    use_safetensors=True
).to("cuda")

加载 IP-Adapter

image.png

IP-Adapter 允许我们通过服装图像(而非文字描述)进行替换。

pipeline.load_ip_adapter(
    "h94/IP-Adapter",
    subfolder="sdxl_models",
    weight_name="ip-adapter_sdxl.bin",
    low_cpu_mem_usage=True
)

3. 加载图片

我们需要两张图片:

  1. 人物图片:建议使用全身图,且人物穿着尽量少,以减少服装对结果的干扰。

image.png

  1. 服装图片:图片中只包含服装,不带人物。

image.png

from diffusers.utils import load_image

image = load_image('https://example.com/person.jpg').convert("RGB")
ip_image = load_image('https://example.com/clothing.jpg').convert("RGB")

4. 创建服装遮罩(Mask)

为了实现精确的服装替换,我们需要生成遮罩,标记哪些部分需要被替换。使用现成的分割工具非常方便:

from SegBody import segment_body

seg_image, mask_image = segment_body(image, face=False)

5. 生成图像

设置 IP-Adapter 的强度(决定对服装图像的依赖程度):

pipeline.set_ip_adapter_scale(1.0)

然后通过以下代码生成最终图像:

final_image = pipeline(
    prompt="photorealistic, perfect body, beautiful skin, realistic skin, natural skin",
    negative_prompt="ugly, bad quality, bad anatomy, deformed body, deformed hands, deformed feet",
    image=image,
    mask_image=mask_image,
    ip_adapter_image=ip_image,
    strength=0.99,
    guidance_scale=7.5,
    num_inference_steps=100,
).images[0]

6. 将流程封装为函数

为了更方便使用,可以将整个过程封装成函数:

def virtual_try_on(img, clothing, prompt, negative_prompt, ip_scale=1.0, strength=0.99, guidance_scale=7.5, steps=100):
    _, mask_img = segment_body(img, face=False)
    pipeline.set_ip_adapter_scale(ip_scale)
    images = pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        image=img,
        mask_image=mask_img,
        ip_adapter_image=clothing,
        strength=strength,
        guidance_scale=guidance_scale,
        num_inference_steps=steps,
    ).images
    return images[0]

调用方式:

result = virtual_try_on(
    img=image, 
    clothing=ip_image,
    prompt="photorealistic, perfect body, beautiful skin",
    negative_prompt="ugly, bad quality, deformed body"
)

应用场景扩展

  1. 电商行业:消费者可以直接上传照片,试穿商品,提升购物体验。
  2. 服装设计:设计师可以用不同的人物图像快速试验新设计效果。
  3. 社交娱乐:个人可以用该技术生成有趣的内容,比如换装照片。

通过以上步骤,你只需 12 行核心代码,就能轻松打造自己的 Virtual Try-On 工具!