Membuat Gambar Coba Virtual

Coba Virtual memungkinkan Anda membuat gambar orang untuk mencoba produk pakaian secara virtual. Anda memberikan gambar seseorang dan gambar produk pakaian, lalu fitur Coba Virtual akan membuat gambar orang tersebut yang sedang mengenakan produk pakaian tersebut.

Model berikut mendukung pembuatan gambar coba virtual:

Klik untuk meluaskan model yang didukung

Mencoba Coba Virtual di Colab

Sebelum memulai

  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. Siapkan autentikasi untuk lingkungan Anda.

    Pilih tab untuk melihat bagaimana Anda berencana menggunakan contoh di halaman ini:

    Python

    Untuk menggunakan contoh Python di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

    1. Instal Google Cloud CLI.

    2. Konfigurasi gcloud CLI agar menggunakan identitas gabungan Anda.

      Untuk mengetahui informasi selengkapnya, lihat Login ke gcloud CLI dengan identitas gabungan Anda.

    3. Buat kredensial autentikasi lokal untuk akun pengguna Anda:

      gcloud auth application-default login

      Jika error autentikasi ditampilkan, pastikan Anda telah login ke gcloud CLI dengan identitas gabungan Anda.

    Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal dalam dokumentasi autentikasi Cloud de Confiance .

    REST

    Untuk menggunakan contoh REST API di halaman ini dalam lingkungan pengembangan lokal, gunakan kredensial yang Anda berikan ke gcloud CLI.

      Instal Google Cloud CLI, lalu login ke gcloud CLI dengan identitas gabungan Anda.

    Untuk mengetahui informasi selengkapnya, lihat Melakukan autentikasi untuk menggunakan REST dalam dokumentasi autentikasi Cloud de Confiance .

Buat gambar

Python

Instal

pip install --upgrade google-genai

Untuk mempelajari lebih lanjut, lihat dokumentasi referensi SDK.

Tetapkan variabel lingkungan untuk menggunakan Google Gen AI SDK dengan Vertex AI:

# 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

Untuk mengetahui informasi selengkapnya tentang Virtual Try-On API, lihat referensi berikut:

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • REGION: Region tempat project Anda berada. Untuk mengetahui informasi selengkapnya tentang wilayah yang didukung, lihat Deployment dan endpoint.
  • PROJECT_ID: Project ID Cloud de Confiance by S3NS Anda.
  • BASE64_PERSON_IMAGE: Gambar orang yang dienkode Base64.
  • BASE64_PRODUCT_IMAGE: Gambar produk yang dienkode Base64.
  • IMAGE_COUNT: Jumlah gambar yang akan dibuat. Rentang nilai yang diterima adalah 1 hingga 4.
  • GCS_OUTPUT_PATH: Jalur Cloud Storage untuk menyimpan output coba virtual.

Metode HTTP dan URL:

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

Meminta isi JSON:

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

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

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

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

$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
Permintaan akan menampilkan objek gambar. Dalam contoh ini, dua objek gambar ditampilkan, dengan dua objek prediksi sebagai gambar berenkode base64.
{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    },
    {
      "bytesBase64Encoded": "BASE64_IMG_BYTES",
      "mimeType": "image/png"
    }
  ]
}