使用 Imagen 提高圖片解析度

您可以使用 Vertex AI 的 Imagen 放大圖片,在不失真的情況下增加圖片大小。

下列模型支援圖片放大功能:

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  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 role (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 role (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. 為環境設定驗證機制。

    選取這個頁面上的分頁,瞭解如何使用範例:

    控制台

    使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。

    REST

    如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

      安裝 Google Cloud CLI。

      若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 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: 放大圖片的縮放比例係數。套用放大係數後,圖片的最終輸出解析度不得超過 1700 萬像素。可接受的值如下:
    • "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 產品的文章: