生成虛擬試穿圖片

虛擬試穿功能可生成人物圖像,虛擬試穿服飾產品。您只要提供人物和服飾產品的圖片,虛擬試穿功能就會生成人物穿著該產品的圖片。

下列模型支援生成虛擬試妝圖片:

按一下即可展開支援的模型

在 Colab 中試用虛擬試穿功能

事前準備

  1. In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Cloud de Confiance project.

  3. Enable the Agent Platform API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  4. 為環境設定驗證機制。

    請根據您打算如何使用本頁範例,選取對應的分頁標籤:

    Python

    如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

    1. 安裝 Google Cloud CLI。

    2. 設定 gcloud CLI,使用您的聯合身分。

      詳情請參閱「 使用聯合身分登入 gcloud CLI」。

    3. 為使用者帳戶建立本機驗證憑證:

      gcloud auth application-default login

      如果傳回驗證錯誤,請確認您已 使用聯合身分登入 gcloud CLI。

    詳情請參閱 Cloud de Confiance 驗證說明文件中的「 為本機開發環境設定 ADC」。

    REST

    如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

      安裝 Google Cloud CLI,然後 使用聯合身分登入 gcloud CLI。

    詳情請參閱 Cloud de Confiance 驗證說明文件中的「使用 REST 進行驗證」一節。

生成圖像

Python

安裝

pip install --upgrade google-genai

詳情請參閱 SDK 參考文件。

設定環境變數,以便透過 Vertex AI 使用 Google Gen AI SDK:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_ENTERPRISE=True

from google import genai
from google.genai.types import RecontextImageSource, ProductImage

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_file = "output-image.png"

image = client.models.recontext_image(
    model="virtual-try-on-001",
    source=RecontextImageSource(
        person_image=Image.from_file(location="test_resources/man.png"),
        product_images=[
            ProductImage(product_image=Image.from_file(location="test_resources/sweater.jpg"))
        ],
    ),
)

image.generated_images[0].image.save(output_file)

print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
# Example response:
# Created output image using 1234567 bytes

REST

如要進一步瞭解 Virtual Try-On API,請參閱下列內容:

使用任何要求資料之前,請先修改下列項目的值:

  • REGION:專案所在的區域。如要進一步瞭解支援的區域,請參閱「部署和端點」。
  • PROJECT_ID:您的 Cloud de Confiance by S3NS 專案 ID。
  • BASE64_PERSON_IMAGE:人物圖片的 Base64 編碼圖片。
  • BASE64_PRODUCT_IMAGE:產品圖片的 Base64 編碼圖片。
  • IMAGE_COUNT:要生成的圖片數量。可接受的值範圍為 1 至 4。
  • GCS_OUTPUT_PATH:儲存虛擬試妝輸出內容的 Cloud Storage 路徑。

HTTP 方法和網址:

POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-001:predict

JSON 要求內文:

{
  "instances": [
    {
      "personImage": {
        "image": {
          "bytesBase64Encoded": "BASE64_PERSON_IMAGE"
        }
      },
      "productImages": [
        {
          "image": {
            "bytesBase64Encoded": "BASE64_PRODUCT_IMAGE"
          }
        }
      ]
    }
  ],
  "parameters": {
    "sampleCount": IMAGE_COUNT,
    "storageUri": "GCS_OUTPUT_PATH"
  }
}

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-001:predict"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-001:predict" | Select-Object -Expand Content
要求會傳回圖片物件。在本例中,系統會傳回兩個圖像物件,並以 Base64 編碼圖像的形式提供兩個預測物件。
{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    },
    {
      "bytesBase64Encoded": "BASE64_IMG_BYTES",
      "mimeType": "image/png"
    }
  ]
}