バーチャルで試着画像を生成する

「バーチャル試着」では、人物の画像を生成し、その人物に衣料品をバーチャル試着させることができます。人物の画像と衣料品の画像を提供すると、「バーチャルでお試し」機能がその衣料品を着用している人物の画像を生成します。

次のモデルは、バーチャルでお試し画像を生成することをサポートしています。

クリックして、サポートされているモデルを開く

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. フェデレーション ID(連携 ID)を使用するように gcloud CLI を構成します。

      詳細については、 フェデレーション ID を使用して gcloud CLI にログインするをご覧ください。

    3. ユーザーアカウントのローカル認証情報を作成します。

      gcloud auth application-default login

      認証エラーが返された場合は、 連携 ID を使用して gcloud CLI にログインしていることを確認します。

    詳細については、 認証ドキュメントのローカル開発環境の ADC の設定 Cloud de Confiance をご覧ください。

    REST

    このページの REST API サンプルをローカル開発環境で使用するには、 gcloud CLI に指定した認証情報を使用します。

      Google Cloud CLI をインストールし、 フェデレーション ID を使用して gcloud CLI にログインします。

    詳細については、 REST を使用して認証する 認証ドキュメントの Cloud de Confiance をご覧ください。

画像を生成

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 メソッドと URL:

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
リクエストは画像オブジェクトを返します。この例では、2 つの画像オブジェクトが返され、2 つの予測オブジェクトが base64 でエンコードされた画像として返されます。
{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    },
    {
      "bytesBase64Encoded": "BASE64_IMG_BYTES",
      "mimeType": "image/png"
    }
  ]
}