作者 / 高級開發者關系工程師 Thomas Ezan 和開發者關系工程師 Mozart Louis
為您的應用添加自定義圖像,能夠顯著改善和個性化用戶體驗,有效提高用戶參與度。本文將探討使用 Firebase AI Logic 生成圖像的兩種新功能: 其一是 Imagen 專屬編輯功能預覽版;其二是 Gemini 2.5 Flash Image (又名 Nano Banana) 正式版,后者的專長就是在情境或對話中生成圖像。
借助 Firebase AI Logic 生成圖像以提高用戶參與度
圖像生成模型可用于創建自定義用戶個人資料頭像,或將個性化視覺素材直接集成到應用關鍵流程中。
例如,Imagen 提供了尚處于開發者預覽版的全新編輯功能。現在,您可以繪制遮罩并利用圖像內繪制 (inpainting) 在遮罩區域內生成像素。此外,還提供了圖像擴展 (outpainting) 功能,可用于生成遮罩區域外的像素。
另一方面,Gemini 2.5 Flash Image (又名 Nano Banana) 可以使用 Gemini 模型淵博的世界知識和推理能力來生成與上下文相關的圖像,這非常適合用于創建與用戶當前應用內體驗契合的動態插圖。
△ 使用 Gemini 2.5 Flash Image 創建與您的應用上下文相關的動態插圖
最后,借助對話和迭代編輯圖像的功能,用戶可以使用自然語言編輯圖像。
在著手將 AI 集成到您的應用中時,了解 AI 安全性非常重要。尤其關鍵的是,您需要評估應用的安全風險、斟酌調整以降低安全風險、執行適合您用例的安全測試,以及征求用戶反饋并監控內容。
Imagen 或 Gemini 任您選擇
Gemini 2.5 Flash Image (Nano Banana) 和 Imagen 之間的區別在于各自獨特的側重點和高級功能。Gemini 2.5 Flash Image 作為廣泛的 Gemini 系列中的圖像模型,擅長對話式圖像編輯,能在多次迭代中保持上下文和主體一致性,并利用 "世界知識和推理" 來創建與上下文相關的視覺內容,或在長文本序列中嵌入準確的視覺內容。
Imagen 是 Google 的專業圖像生成模型,專為更好地發揮創意和掌控作品而設計,擅長于高度真實的輸出、藝術細節、特定風格,并提供明確的控制選項,來指定生成圖像的寬高比或格式。
|
Gemini 2.5 Flash Image (Nano Banana ) |
Imagen |
|
具備世界知識和推理能力,生成與上下文更相關的圖像 在保持與上下文關聯性的同時,通過對話編輯圖像 在長文本序列中嵌入準確的視覺內容 |
指定生成圖像的寬高比或格式 支持基于遮罩的編輯,實現圖像內繪制和圖像擴展 更好地控制生成圖像的細節 (質量、藝術細節和特定風格) |
一起來了解如何在您的應用中使用上述功能。
使用 Imagen 進行圖像內繪制
幾個月前,我們發布了 Imagen 新的編輯功能。雖然 Imagen 的圖像生成功能已可正式用于生產環境,但編輯功能仍處于開發者預覽版階段。
Imagen 編輯功能包括圖像內繪制和圖像擴展,二者均為基于遮罩的圖像編輯功能。這項新功能允許用戶修改圖像的特定區域,而無需重新生成整個圖像。這意味著您可以保留圖像中您最滿意的部分,只更改您想要調整的內容。
做出這些更改時,原始圖像的核心元素和圖像整體完整性不受影響,您可以僅調整遮罩區域。
要使用 Imagen 的圖像內繪制功能,請先初始化 imagen-3.0-capability-001,這是支持編輯功能的特定 Imagen 模型:
// Copyright2025Google LLC. // SPDX-License-Identifier: Apache-2.0 val editingModel = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( "imagen-3.0-capability-001", generationConfig =ImagenGenerationConfig( numberOfImages =1, aspectRatio = ImagenAspectRatio.SQUARE_1x1, imageFormat = ImagenImageFormat.jpeg(compressionQuality =75), ), )
然后,定義圖像內繪制函數:
// Copyright 2025 Google LLC. // SPDX-License-Identifier: Apache-2.0 valprompt ="remove the pancakes and make it an omelet instead" suspendfuninpaintImageWithMask(sourceImage:Bitmap, maskImage:Bitmap, prompt:String, editSteps:Int=50): Bitmap { valimageResponse = editingModel.editImage( referenceImages = listOf( ImagenRawImage(sourceImage.toImagenInlineImage()), ImagenRawMask(maskImage.toImagenInlineImage()), ), prompt = prompt, config = ImagenEditingConfig( editMode = ImagenEditMode.INPAINT_INSERTION, editSteps = editSteps, ), ) returnimageResponse.images.first().asBitmap() }
您需要提供一張原始圖像、一張遮罩圖像、用于編輯的提示詞以及需要執行的編輯步驟數量。
您可以在 Android AI 示例庫的 Imagen Editing Sample 中,查看其實際運行效果!
Imagen 還支持圖像擴展,該功能使您能讓模型在遮罩外的區域生成像素。您還可以使用 Imagen 的圖像自定義功能來更改圖像風格或更新圖像中的一個主體。有關詳細信息,請參閱 Android 開發者文檔。
使用 Gemini 2.5 Flash Image 通過對話生成圖像
要使用 Gemini 2.5 Flash Image 編輯圖像,一種方法是通過該模型的多輪聊天功能進行編輯。
首先,初始化模型:
// Copyright 2025 Google LLC.
// SPDX-License-Identifier: Apache-2.0
valmodel=Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName ="gemini-2.5-flash-image",
// Configure the model to respond with text and images (required)
generationConfig = generationConfig {
responseModalities = listOf(ResponseModality.TEXT,
ResponseModality.IMAGE)
}
)
為了實現與上述基于遮罩的 Imagen 方法類似的結果,我們可以利用chatAPI,啟動與 Gemini 2.5 Flash Image 的對話。
// Copyright 2025 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Initialize the chat
valchat=model.startChat()
// Load a bitmap
valsource=ImageDecoder.createSource(context.contentResolver, uri)
valbitmap=ImageDecoder.decodeBitmap(source)
// Create the initial prompt instructing the model to edit the image
valprompt=content {
image(bitmap)
text("remove the pancakes and add an omelet")
}
// To generate an initial response, send a user message with the image and text prompt
varresponse=chat.sendMessage(prompt)
// Inspect the returned image
vargeneratedImageAsBitmap=response
.candidates.first().content.parts.filterIsInstance().firstOrNull()?.image
// Follow up requests do not need to specify the image again
response = chat.sendMessage("Now, center the omelet in the pan")
generatedImageAsBitmap = response .candidates.first().content.parts.filterIsInstance().firstOrNull()?.image
您可以在 Android AI 示例庫的 Gemini Image Chat Sample 查看該功能的實際運作,也可參閱 Android 技術文檔以了解更多相關信息。
結論
Imagen 和 Gemini 2.5 Flash Image 都提供了強大的功能,允許您根據具體用例選擇理想的圖像生成模型,從而讓您的應用更加個性化,并提高用戶參與度。
-
Gemini
+關注
關注
0文章
74瀏覽量
8146 -
AI
+關注
關注
91文章
39755瀏覽量
301358 -
模型
+關注
關注
1文章
3751瀏覽量
52099
原文標題:一文詳解 | 從了解到集成,輕松為您的應用選擇理想的圖像生成模型
文章出處:【微信號:Google_Developers,微信公眾號:谷歌開發者】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
【Sipeed MaixCAM Pro開發板試用體驗】基于MaixCAM-Pro的AI生成圖像鑒別系統
機器人運動控制的兩種模型
機器人運動控制的兩種模型
iOS10.3beta3推送 修復bug增加兩種新功能
一種基于改進的DCGAN生成SAR圖像的方法
Stability AI開源圖像生成模型Stable Diffusion
Stability AI試圖通過新的圖像生成人工智能模型保持領先地位
谷歌計劃重新推出改進后的Gemini AI模型人像生成功能
快手自研文生圖大模型“可圖”開放,支持AI圖像創作及定制
如何用C++創建簡單的生成式AI模型
借助谷歌Gemini和Imagen模型生成高質量圖像
使用Firebase AI Logic生成圖像模型的兩種新功能
評論