Imagen 放大 API

支援的模型版本

Imagen 放大功能支援下列模型:

  • imagen-4.0-upscale-preview

如要進一步瞭解模型支援的功能,請參閱 Imagen 模型

HTTP 要求

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/MODEL_ID:predict \

-d '{
  "instances": [
    {
      "prompt": string,
      "image": {
        // Union field can be only one of the following:
        "bytesBase64Encoded": string,
        "gcsUri": string,
        // End of list of possible types for union field.
      },
    }
  ],
  "parameters": {
    "mode": "upscale",
    "storageUri": string,
    "outputOptions": {
      "mimeType": string,
      "compressionQuality": integer
    },
    "upscaleConfig": {
      "upscaleFactor": string
    }
  }
}'

執行個體

執行個體

聯集欄位 image

要提高解析度的圖片。您可以提供圖片的 bytesBase64Encoded 編碼字串,或是 Cloud Storage 值區位置的 gcsUri 字串 URI。

bytesBase64Encoded

string

圖片或影片檔案的位元組 Base64 編碼字串。

gcsUri

string

Cloud Storage 值區位置的字串 URI。

參數

參數
mode

string

這是必要旗標,如要提出升級要求,請傳遞字串 "upscale"

storageUri

string

(選用步驟) Cloud Storage bucket 位置的字串 URI,用於儲存產生的圖片。如果未提供 Cloud Storage bucket,回應中會傳回 base64 編碼的圖片位元組。

outputOptions

outputOptions

(選用步驟) 在 outputOptions 物件中說明輸出圖片格式。

upscaleConfig.upscaleFactor

string

放大後圖片的縮放比例係數。可接受的值如下:

  • "x2"
  • "x3"
  • "x4"

輸出選項物件

outputOptions 物件會說明圖片輸出內容。

參數
outputOptions.mimeType

選填:string

圖片輸出格式。支援的值如下:

  • "image/png":另存為 PNG 圖片。
  • "image/jpeg":儲存為 JPEG 圖片。

預設值為 "image/png"

outputOptions.compressionQuality

選填:int

如果輸出類型為 "image/jpeg",則為壓縮程度。可接受的值為 0-100。預設值為 75

要求範例

REST

使用任何要求資料之前,請先替換以下項目:

  • 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://REGION-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://REGION-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://REGION-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"
    }
  ]
}