Bilder für den virtuellen Test generieren

Mit dem virtuellen Anprobetool können Sie Bilder von Personen generieren, die Bekleidungsprodukte virtuell anprobieren. Sie stellen ein Bild einer Person und ein Bild eines Bekleidungsprodukts bereit. Virtual Try-On generiert dann ein Bild der Person, die dieses Produkt trägt.

Virtuelles Anprobieren in einem Colab ausprobieren

Hinweise

  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. Richten Sie die Authentifizierung für Ihre Umgebung ein.

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

    Python

    Wenn Sie die Python -Beispiele auf dieser Seite in einer lokalen Entwicklungsumgebung verwenden möchten, installieren und initialisieren Sie die gcloud CLI und richten Sie dann die Standardanmeldedaten für Anwendungen mit Ihren Nutzeranmeldedaten ein.

    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.

    Weitere Informationen finden Sie in der Dokumentation zur Google Cloud -Authentifizierung unter ADC für eine lokale Entwicklungsumgebung einrichten.

    REST

    Wenn Sie die REST API-Beispiele auf dieser Seite in einer lokalen Entwicklungsumgebung verwenden möchten, nutzen Sie die Anmeldedaten, die Sie der gcloud CLI bereitstellen.

      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.

    Weitere Informationen finden Sie in der Dokumentation zur Google Cloud -Authentifizierung unter Für die Verwendung von REST authentifizieren.

  9. Bilder erstellen

    Python

    Installieren

    pip install --upgrade google-genai

    Weitere Informationen finden Sie in der SDK-Referenzdokumentation.

    Umgebungsvariablen für die Verwendung des Gen AI SDK mit Vertex AI festlegen:

    # 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

    Ersetzen Sie diese Werte in den folgenden Anfragedaten:

    • REGION: Die Region, in der sich Ihr Projekt befindet. Weitere Informationen zu unterstützten Regionen finden Sie unter Generative AI an Vertex AI-Standorten.
    • PROJECT_ID: Ihre Google Cloud Projekt-ID.
    • BASE64_PERSON_IMAGE: Das Base64-codierte Bild der Person.
    • BASE64_PRODUCT_IMAGE: Das Base64-codierte Bild des Produktbilds.
    • IMAGE_COUNT: Die Anzahl der zu generierenden Bilder. Der zulässige Wertebereich liegt zwischen 1 und 4.
    • GCS_OUTPUT_PATH: Der Cloud Storage-Pfad, in dem die Ausgabe des virtuellen Anprobierens gespeichert werden soll.

    HTTP-Methode und URL:

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

    JSON-Text der Anfrage:

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

    Wenn Sie die Anfrage senden möchten, wählen Sie eine der folgenden Optionen aus:

    curl

    Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

    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

    Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:

    $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
    Bei der Anfrage werden Bildobjekte zurückgegeben. In diesem Beispiel werden zwei Bildobjekte mit zwei Vorhersageobjekten als base64-codierte Bilder zurückgegeben.
    {
      "predictions": [
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        },
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        }
      ]
    }