Membuat Gambar Coba Virtual

Dengan Coba Virtual, Anda dapat 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 yang mengenakan produk tersebut.

Mencoba Coba Virtual di Colab

Sebelum memulai

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

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

    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. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. 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.

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

    REST

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

      Install the Google Cloud CLI.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

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

  9. Buat gambar

    Python

    Instal

    pip install --upgrade google-genai

    Untuk mempelajari lebih lanjut, lihat dokumentasi referensi SDK.

    Tetapkan variabel lingkungan untuk menggunakan 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_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

    Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

    • REGION: Region tempat project Anda berada. Untuk mengetahui informasi selengkapnya tentang region yang didukung, lihat Lokasi AI Generatif di Vertex AI.
    • PROJECT_ID: Google Cloud Project ID 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-preview-08-04:predict

    Isi JSON permintaan:

    {
      "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-preview-08-04: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-preview-08-04:predict" | Select-Object -Expand Content
    Permintaan ini 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"
        }
      ]
    }