יצירת תמונות של התנסות וירטואלית

התכונה "מדידה וירטואלית" מאפשרת ליצור תמונות של אנשים כדי למדוד באופן וירטואלי מוצרי ביגוד. אתם מספקים תמונה של אדם ותמונה של מוצר ביגוד, ואז התכונה 'התנסות וירטואלית' יוצרת תמונה של האדם כשהוא לובש את המוצר.

המודלים הבאים תומכים ביצירת תמונות של התנסות וירטואלית:

לחצו כדי להרחיב את רשימת המודלים הנתמכים

התנסות וירטואלית ב-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 שבדף הזה, מתקינים ומפעילים את ה-CLI של gcloud, ואז מגדירים את Application Default Credentials באמצעות פרטי הכניסה של המשתמש.

    1. התקינו את ה-CLI של Google Cloud.

    2. הגדירו שה-CLI של gcloud ישתמש בזהות המאוחדת שלכם.

      איך נכנסים ל-CLI של gcloud באמצעות הזהות המאוחדת?

    3. צרו פרטי כניסה לאימות מקומי בחשבון המשתמש:

      gcloud auth application-default login

      אם מופיעה שגיאת אימות, ודאו ש נכנסתם ל-CLI של gcloud באמצעות הזהות המאוחדת שלכם.

    למידע נוסף, ראו הגדרת ADC לסביבת פיתוח מקומית במאמרי העזרה בנושא אימות Cloud de Confiance .

    REST

    כדי להשתמש בסביבת פיתוח מקומית בדוגמאות של API בארכיטקטורת REST שבדף הזה, צריך להשתמש בפרטי הכניסה שאתם נותנים ל-CLI של gcloud.

      התקינו את ה-CLI של Google Cloud ואז היכנסו ל-CLI של gcloud באמצעות הזהות המאוחדת שלכם.

    מידע נוסף מופיע במאמר אימות לשימוש ב-REST במסמכי האימות של Cloud de Confiance .

יצירת תמונות

Python

התקנה

pip install --upgrade google-genai

מידע נוסף מופיע ב מאמרי העזרה בנושא SDK.

מגדירים משתני סביבה כדי להשתמש ב-Google Gen AI SDK עם 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

מידע נוסף על Virtual Try-On API:

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫REGION: האזור שבו נמצא הפרויקט. מידע נוסף על אזורים נתמכים זמין במאמר פריסות ונקודות קצה.
  • ‫PROJECT_ID: מזהה הפרויקט ב- Cloud de Confiance by S3NS .
  • ‫BASE64_PERSON_IMAGE: תמונה של האדם בקידוד Base64.
  • ‫BASE64_PRODUCT_IMAGE: תמונת המוצר בקידוד Base64.
  • ‫IMAGE_COUNT: מספר התמונות שרוצים ליצור. טווח הערכים הקבילים הוא 1 עד 4.
  • ‫GCS_OUTPUT_PATH: הנתיב ב-Cloud Storage שבו יישמר הפלט של ההתנסות הווירטואלית.

ה-method של ה-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
הבקשה מחזירה אובייקטים של תמונות. בדוגמה הזו, מוחזרים שני אובייקטים של תמונות, עם שני אובייקטים של חיזויים כתמונות בקידוד Base64.
{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    },
    {
      "bytesBase64Encoded": "BASE64_IMG_BYTES",
      "mimeType": "image/png"
    }
  ]
}