重构产品图片场景

借助 Imagen Product Recontext on Vertex AI,您可以将产品图片修改为不同的场景或背景。您提供产品图片和可选提示,然后 Imagen Product Recontext 会生成产品图片,这些图片会将产品重新置于新场景或背景中。

如需申请访问 Imagen Product Recontext,请填写 Vertex AI - 适用于市场营销的生成式媒体访问申请表单。

支持的产品类型

Imagen Product Recontext支持以下产品类型:

  • 电器
  • 工商业
  • 服装
  • 电子产品
  • 家具
  • 花园和庭院
  • 硬件
  • 美容养生
  • 珠宝
  • 宠物
  • 鞋子
  • 体育用品
  • 玩具和游戏
  • 车辆

产品重新情境化示例

以下示例展示了产品重新情境化的使用情形:

示例输入 示例输出 使用的提示
一台黑色笔记本电脑,键盘上有多色背光,屏幕上有彩色测试图片,放在白色背景上。 一台黑色笔记本电脑,键盘上有多色背光,屏幕上有彩色测试图片,现在放在工作台上。
             工作台后面是一扇窗户,窗外显示着彩色广告。 在一条杂乱的雨天霓虹灯赛博朋克小巷中,工作台上摆满了各种物品,背景中全息广告闪烁着。
一台附有大镜头的数码单反相机,放置在白色背景上。 一台附有大镜头的数码单反相机,放置在白色背景上。现在,相机放置在悬崖边,背景是宽阔的瀑布。 放置在悬崖边一块长满苔藓的岩石上,俯瞰冰岛壮丽的阴天景色中气势磅礴的巨大瀑布。
一副太阳镜,放置在白色背景上。 一副太阳镜,现在放在桌子上,前景中有一个放在托盘上的咖啡杯,背景中是教堂顶部和水体(有山丘)的风景。 坐在圣托里尼一家白色咖啡馆的桌旁,喝着咖啡,俯瞰爱琴海和标志性的蓝色圆顶建筑。

在 Colab 中试用 Imagen Product Recontext

准备工作

  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.

    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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 为您的环境设置身份验证。

    如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。

      Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:

      gcloud init

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

    如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

  9. 在不同场景中生成产品图片

    REST

    在使用任何请求数据之前,请先进行以下替换:

    • REGION:项目所在的区域。如需详细了解支持的区域,请参阅 Vertex AI 上的生成式 AI 位置
    • PROJECT_ID:您的 Google Cloud 项目 ID。
    • TEXT_PROMPT:可选。用于引导模型生成的图片的文本提示。
    • BASE64_SUBJECT_IMAGE:主体图片的 base64 编码图片。
    • PERSON_SETTING:(可选)一个字符串值,用于控制模型允许生成的人物类型或人脸类型。可以为 personGeneration 使用以下值:
      • "allow_adult":仅允许生成成人(名人除外)。不允许针对任何设置生成名人。这是默认设置。
      • "allow_all":允许生成所有年龄的人物(名人除外)。不允许针对任何设置生成名人。
      • "dont_allow":不允许在生成的输出内容中生成人物或人脸。
    • SAFETY_SETTING:(可选)一个字符串值,用于控制生成的图片的安全过滤器阈值。您可以使用以下值来选择安全设置:
      • "block_low_and_above":最严格的安全阈值。 "block_low_and_above" 会过滤最大数量的生成图片。
      • "block_medium_and_above":中等安全阈值,可平衡针对潜在有害内容和安全内容的过滤。 "block_medium_and_above" 是默认安全设置。
      • "block_only_high":最低安全阈值,可减少因安全过滤器而被屏蔽的请求数量。使用 "block_only_high" 安全阈值可能会增加模型生成的不良图片数量。
    • WATERMARK_SETTING:(可选)一个布尔值。如果此值设置为 true,模型会添加数字水印,您可以用于验证生成的图片。默认值为 true
    • IMAGE_COUNT:要生成的图片数量。可接受的值范围为 1-4
    • PROMPT_SETTING:(可选)一个布尔值。如果此值设置为 true,模型将使用增强型提示。默认值为 true

    HTTP 方法和网址:

    POST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-product-recontext-preview-06-30:predict

    请求 JSON 正文:

    {
      "instances": [
        {
          "prompt": "TEXT_PROMPT",
          "productImages": [
            {
              "image": {
                "bytesBase64Encoded": "BASE64_SUBJECT_IMAGE"
              },
            }
          ]
        }
      ],
      "parameters": {
        "personGeneration": PERSON_SETTING,
        "safetySetting": SAFETY_SETTING,
        "addWatermark": WATERMARK_SETTING,
        "sampleCount": IMAGE_COUNT,
        "enhancePrompt": PROMPT_SETTING
      }
    }
    

    如需发送请求,请选择以下方式之一:

    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/imagen-product-recontext-preview-06-30: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/imagen-product-recontext-preview-06-30:predict" | Select-Object -Expand Content
    请求会返回图片对象。在此示例中,系统会返回两个图片对象,其中包含两个预测对象(采用 base64 编码的图片形式)。
    {
      "predictions": [
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        },
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        }
      ]
    }