本頁說明如何更換圖片背景。透過 Vertex AI 的 Imagen,您可以使用自動物件區隔功能保留該內容,同時修改其他圖片內容。使用 Imagen 3 編輯圖片時,你也可以提供自己的遮罩區域,進一步控管編輯作業。
下列模型支援更換圖片背景:
產品圖片編輯範例
以下用例重點在於修改圖片背景,但保留圖片中的產品外觀,藉此提升產品圖片品質。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
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 Vertex AI API, if it is not already enabled.
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.-
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 Vertex AI API, if it is not already enabled.
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.-
為環境設定驗證機制。
選取這個頁面上範例的預計用途分頁:
控制台
使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,不需要設定驗證。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。
使用自動偵測到的背景遮罩進行編輯
請按照下列操作說明,啟用並使用產品圖片編輯功能,自動偵測背景。
控制台
在 Google Cloud 控制台中,依序前往「Vertex AI」 >「Vertex AI Studio」 頁面。
按一下「生成媒體」。
按一下「圖像」。
從「Task」(工作) 清單中選取「Product-background-editing」(產品背景編輯)。
從「模型」清單中,選取要使用的 Imagen 模型。
在「輸入圖片」中,按一下「新增」,然後選取要編輯的圖片以上傳。
在「提示」方塊中輸入提示詞,說明如何編輯圖片。
選取下列其中一個遮罩擷取選項:
- 背景元素:偵測背景元素,並在周圍建立遮罩。
- 前景元素:偵測前景物件,並在周圍建立遮罩。
- background_replace People:偵測人物並在周圍建立遮罩。
按一下「執行」。
REST
詳情請參閱「編輯圖片」API 參考資料。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如:
us-central1、europe-west2或asia-northeast3。如需可用區域清單,請參閱「Vertex AI 的生成式 AI 服務地區」。使用區域 API 端點時,系統會根據端點網址中的區域,決定處理要求的位置。如果資源路徑中的LOCATION與端點網址中的區域衝突,系統會忽略資源路徑中的LOCATION。 - TEXT_PROMPT:引導模型生成圖片的文字提示詞。生成和編輯時都必須填寫這個欄位。
referenceType:ReferenceImage是指提供圖片編輯額外背景資訊的圖片。編輯用途需要使用正常的 RGB 原始參考圖像 (REFERENCE_TYPE_RAW)。每個要求最多只能有一個原始參考圖像。輸出圖片的高度和寬度與原始參考圖像相同。如要使用遮罩編輯,必須提供遮罩參考圖像 (REFERENCE_TYPE_MASK)。referenceId:參考圖像的整數 ID。在這個範例中,兩個參考圖像物件的類型不同,因此具有不同的referenceId值 (1和2)。- B64_BASE_IMAGE:要編輯或放大顯示的基礎圖片。圖片必須指定為 Base64 編碼的位元組字串。大小限制:10 MB。
maskImageConfig.maskMode:遮罩編輯的遮罩模式。MASK_MODE_BACKGROUND可自動遮蓋背景,無須使用者提供遮罩。- MASK_DILATION - 浮點值。這個遮罩要擴張的圖片寬度百分比。建議使用
0.00值,避免延長前景產品。最小值:0,最大值:1。 預設值:0.03。 - EDIT_STEPS - 整數。基礎模型的取樣步驟數。如要編輯產品圖片,請從
75步驟開始。 - EDIT_IMAGE_COUNT - 編輯圖片的數量。可接受的整數值:1-4。 預設值:4。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict
JSON 要求內文:
{
"instances": [
{
"prompt": "TEXT_PROMPT",
"referenceImages": [
{
"referenceType": "REFERENCE_TYPE_RAW",
"referenceId": 1,
"referenceImage": {
"bytesBase64Encoded": "B64_BASE_IMAGE"
}
},
{
"referenceType": "REFERENCE_TYPE_MASK",
"referenceId": 2,
"maskImageConfig": {
"maskMode": "MASK_MODE_BACKGROUND",
"dilation": MASK_DILATION
}
}
]
}
],
"parameters": {
"editConfig": {
"baseSteps": EDIT_STEPS
},
"editMode": "EDIT_MODE_BGSWAP",
"sampleCount": EDIT_IMAGE_COUNT
}
}
如要傳送要求,請選擇以下其中一個選項:
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict" | Select-Object -Expand Content
{
"predictions": [
{
"bytesBase64Encoded": "BASE64_IMG_BYTES",
"mimeType": "image/png"
},
{
"mimeType": "image/png",
"bytesBase64Encoded": "BASE64_IMG_BYTES"
},
{
"bytesBase64Encoded": "BASE64_IMG_BYTES",
"mimeType": "image/png"
},
{
"bytesBase64Encoded": "BASE64_IMG_BYTES",
"mimeType": "image/png"
}
]
}
使用定義的遮罩區域編輯
您可以選擇遮蓋要替換的區域,而非讓 Imagen 自動偵測遮罩。
控制台
在 Google Cloud 控制台中,依序前往「Vertex AI」 >「Vertex AI Studio」 頁面。
按一下「生成媒體」。
按一下「圖像」。
從「Task」(工作) 清單中選取「Product-background-editing」(產品背景編輯)。
從「模型」清單中,選取要使用的 Imagen 模型。
在「輸入圖片」中,按一下「新增」,然後選取要編輯的圖片以上傳。
在「提示」方塊中輸入提示詞,說明如何編輯圖片。
請執行下列任一操作來指定遮罩:
- 上傳自己的遮罩:
- 在電腦上建立遮罩。
- 按一下「上傳」 「匯入遮罩」,然後選取要上傳的遮罩。
- 定義遮罩:在編輯工具列中,使用遮罩工具 (方塊、筆刷、masked_transitions、反轉 工具) 指定要新增內容的區域。
- 上傳自己的遮罩:
按一下「執行」。
REST
如要進一步瞭解 Imagen API,請參閱下列內容:
- 方法:
endpoints.predict VisionGenerativeModelInstanceVisionGenerativeModelParams-
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- LOCATION:專案的區域。例如:
us-central1、europe-west2或asia-northeast3。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。使用區域 API 端點時,系統會根據端點網址中的區域,決定要求處理位置,並忽略資源路徑中衝突的LOCATION。 - TEXT_PROMPT:引導模型生成圖片的文字提示詞。生成和編輯時都必須填寫這個欄位。
referenceId:參考圖像的整數 ID。在這個範例中,兩個參考圖像物件的類型不同,因此具有不同的referenceId值 (1和2)。- B64_BASE_IMAGE:要編輯或放大解析度的基本圖片。圖片必須指定為 Base64 編碼的位元組字串。大小限制:10 MB。
- B64_MASK_IMAGE:要用做遮罩圖層的黑白圖片,用於編輯原始圖片。圖片必須指定為 Base64 編碼的位元組字串。大小限制:10 MB。
- MASK_DILATION - 浮點值。這個遮罩要擴張的圖片寬度百分比。建議使用
0.00值,避免延長前景產品。最小值:0,最大值:1。 預設值:0.03。 - EDIT_STEPS - 整數。基礎模型的取樣步驟數。如要編輯產品圖片,請從
75步驟開始。 - EDIT_IMAGE_COUNT - 編輯圖片的數量。可接受的整數值:1-4。 預設值:4。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict
JSON 要求內文:
{ "instances": [ { "prompt": "TEXT_PROMPT": [ { "referenceType": "REFERENCE_TYPE_RAW", "referenceId": 1, "referenceImage": { "bytesBase64Encoded": "B64_BASE_IMAGE" } }, { "referenceType": "REFERENCE_TYPE_MASK", "referenceId": 2, "referenceImage": { "bytesBase64Encoded": "B64_MASK_IMAGE" }, "maskImageConfig": { "maskMode": "MASK_MODE_USER_PROVIDED", "dilation": MASK_DILATION } } ] } ], "parameters": { "editConfig": { "baseSteps": EDIT_STEPS }, "editMode": "EDIT_MODE_BGSWAP", "sampleCount": EDIT_IMAGE_COUNT } }如要傳送要求,請選擇以下其中一個選項:
以下是產品背景編輯要求的回應範例。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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict" | Select-Object -Expand Content{ "predictions": [ { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" }, { "mimeType": "image/png", "bytesBase64Encoded": "BASE64_IMG_BYTES" }, { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" }, { "bytesBase64Encoded": "BASE64_IMG_BYTES", "mimeType": "image/png" } ] }
限制
由於遮罩有時不完整,因此模型可能會嘗試完成前景物件,即使邊界只缺少極小的部分。在罕見的副作用中,如果前景物件已完成,模型可能會建立輕微的擴充部分。
解決方法是先分割模型輸出內容,再進行混合。以下是 Python 程式碼片段範例,說明如何解決這個問題:
blended = Image.composite(out_images[0].resize(image_expanded.size), image_expanded, mask_expanded)
後續步驟
閱讀有關 Imagen 和其他 Vertex AI 生成式 AI 產品的文章:
- 開發人員指南:開始在 Vertex AI 使用 Imagen 3
- 專為創作者打造的全新生成式媒體模型和工具
- Gemini 新功能:自訂 Gem 和 Imagen 3 圖像生成功能
- Google DeepMind:Imagen 3 - 品質最高的文字轉圖像模型