生成虛擬試穿圖片

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

在 Colab 中試用虛擬試裝功能

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. 為環境設定驗證方法。

    Select the tab for how you plan to use the samples on this page:

    Python

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

      安裝 Google Cloud CLI。

      如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

      If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

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

    REST

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

      安裝 Google Cloud CLI。

      如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

    生成圖像

    Python

    安裝

    pip install --upgrade google-genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 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_VERTEXAI=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-preview-08-04",
        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

    使用任何要求資料之前,請先替換以下項目:

    • REGION:專案所在的區域。如要進一步瞭解支援的區域,請參閱「Vertex AI 的生成式 AI 服務地區」。
    • PROJECT_ID:您的 Google Cloud 專案 ID
    • BASE64_PERSON_IMAGE:採用 Base64 編碼的人像圖片。
    • BASE64_PRODUCT_IMAGE:產品圖片的 Base64 編碼圖片。
    • IMAGE_COUNT:要生成的圖片數量。可接受的值範圍為 14
    • GCS_OUTPUT_PATH:儲存虛擬試穿輸出內容的 Cloud Storage 路徑。

    HTTP 方法和網址:

    POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04: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-preview-08-04: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-preview-08-04:predict" | Select-Object -Expand Content
    要求會傳回圖片物件。在本例中,系統會傳回兩個圖片物件,其中兩個預測物件是經過 Base64 編碼的圖片。
    {
      "predictions": [
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        },
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        }
      ]
    }