使用 Imagen 提高图片分辨率

您可以使用 Imagen on Vertex AI 来放大图片,这样可以增加图片的大小,而不会降低图片质量。

以下模型支持放大图片:

准备工作

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  3. Enable the Vertex AI API.

    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. 为您的环境设置身份验证。

    选择标签页以了解您打算如何使用本页面上的示例:

    控制台

    当您使用 Google Cloud 控制台访问 Google Cloud 服务和 API 时,无需设置身份验证。

    REST

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

      安装 Google Cloud CLI,然后 使用联合身份登录 gcloud CLI

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

放大图片

REST

如需详细了解 Imagen API,请参阅以下内容:

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

  • REGION:项目所在的区域。如需详细了解支持的区域,请参阅 Vertex AI 上的生成式 AI 位置
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • BASE64_SUBJECT_IMAGE:主题图片的 base64 编码图片。
  • ADD_WATERMARK:一个可选的布尔值。设置为 true 可启用添加水印的图片,设置为 false 可停用添加水印的图片。默认值为 true
  • GCS_IMAGE_PATH:图片文件的 Cloud Storage 路径。
  • GCS_OUTPUT_PATH:用于存储生成的输出的 Cloud Storage 路径。
  • OUTPUT_MIMETYPE:一个可选字符串,用于定义图片的输出文件类型。接受以下值:"image/png""image/jpeg"。默认值为 "image/png"
  • COMPRESSION_QUALITY:一个可选的整数值,用于指定模型为 JPEG 图片保留的细节级别。接受的值范围为:0 - 100。值越高,表示压缩级别越高。默认值为 75
  • UPSCALE_FACTOR:放大后图片的缩放比例。应用放大系数后,图片的最终输出分辨率不得超过 1,700 万像素。接受以下值:
    • "x2"
    • "x3"
    • "x4"

HTTP 方法和网址:

POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview:predict

请求 JSON 正文:

{
  "instances": [
    {
      "prompt": "Upscale the image",
      "image": {
        // use one of the following to specify the image to upscale
        "bytesBase64Encoded": "BASE64_SUBJECT_IMAGE"
        "gcsUri": "GCS_IMAGE_PATH"
        // end of base image input options
      },
    }
  ],
  "parameters": {
    "mode": "upscale",
    "storageUri": "GCS_OUTPUT_PATH",
    "outputOptions": {
      "mimeType": "OUTPUT_MIMETYPE",
      "compressionQuality": COMPRESSION_QUALITY
    },
    "upscaleConfig": {
      "upscaleFactor": "UPSCALE_FACTOR"
    }
  }
}

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

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://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview: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://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/imagen-4.0-upscale-preview:predict" | Select-Object -Expand Content
请求会返回一个图片对象。在此示例中,以预测对象的形式返回了一个图片对象,其中包含一个采用 base64 编码的图片。
{
  "predictions": [
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    }
  ]
}


后续步骤

阅读有关 Imagen 和其他 Vertex AI 上的生成式 AI 产品的文章: