借助虚拟试穿功能,您可以生成人物虚拟试穿服装产品的图片。您提供一张人物图片和一件服装产品图片,然后虚拟试穿功能会生成人物穿着该产品的图片。
以下模型支持生成虚拟试穿图片:
点击即可展开支持的模型
准备工作
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
为您的环境设置身份验证。
选择标签页以了解您打算如何使用本页面上的示例:
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并 初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭据。
-
安装 Google Cloud CLI。
-
配置 gcloud CLI 以使用您的联合身份。
如需了解详情,请参阅使用联合身份登录 gcloud CLI。
-
为您的用户账号创建本地身份验证凭证:
gcloud auth application-default login
如果返回了身份验证错误,并且您使用的是外部身份提供方 (IdP),请确认您已 使用联合身份登录 gcloud CLI。
如需了解详情,请参阅 身份验证文档中的 为本地开发环境设置 ADC Google Cloud
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的 凭证。
安装 Google Cloud CLI,然后 使用联合身份登录 gcloud CLI。
如需了解详情,请参阅 身份验证文档 中的 Google Cloud 使用 REST 时进行身份验证。
-
生成图片
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
REST
如需详细了解 Virtual Try-On API,请参阅以下内容:
在使用任何请求数据之前, 请先进行以下替换:
- REGION:项目所在的区域。如需详细了解支持的区域,请参阅部署和端点。
- PROJECT_ID:您的 Google Cloud 项目 ID。
- BASE64_PERSON_IMAGE:人物图片的 Base64 编码图片。
- BASE64_PRODUCT_IMAGE:产品图片的 Base64 编码图片。
-
IMAGE_COUNT:要生成的图片数量。可接受的值范围为
1至4。 - GCS_OUTPUT_PATH:用于存储虚拟试穿输出的 Cloud Storage 路径。
HTTP 方法和网址:
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
{
"predictions": [
{
"mimeType": "image/png",
"bytesBase64Encoded": "BASE64_IMG_BYTES"
},
{
"bytesBase64Encoded": "BASE64_IMG_BYTES",
"mimeType": "image/png"
}
]
}