本頁面說明如何使用 Imagen 的自訂功能,透過 Firebase AI Logic SDK 根據指定風格編輯或生成圖片。
運作方式:提供文字提示和至少一張參考圖片,圖片中顯示特定風格 (例如圖案、紋理或設計風格)。模型會根據參考圖片中指定的風格,使用這些輸入內容生成新圖片。
舉例來說,您可以根據熱門零售目錄中的圖片,生成廚房的新圖片。
事前準備
只有在使用 Vertex AI Gemini API 做為 API 供應商時,才能使用這項功能。 |
如果尚未完成,請參閱入門指南,瞭解如何設定 Firebase 專案、將應用程式連結至 Firebase、新增 SDK、初始化所選 API 供應商的後端服務,以及建立 ImagenModel
執行個體。
支援這項功能的模型
Imagen 可透過 capability
模型編輯圖片:
imagen-3.0-capability-001
請注意,Imagen 型號global
不支援位置資訊。
傳送樣式自訂要求
以下範例顯示樣式自訂要求,要求模型生成與所提供參考圖片 (在本範例中為梵谷的「星夜」) 相同風格的新圖片。
請參閱本頁稍後的「提示範本」,瞭解如何撰寫提示,以及如何在提示中使用參考圖片。
Swift
Swift 不支援使用 Imagen 模型編輯圖片。請於今年稍晚再回來查看!
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun customizeImage() {
// Initialize the Vertex AI Gemini API backend service
// Optionally specify the location to access the model (for example, `us-central1`)
val ai = Firebase.ai(backend = GenerativeBackend.vertexAI(location = "us-central1"))
// Create an `ImagenModel` instance with an Imagen "capability" model
val model = ai.imagenModel("imagen-3.0-capability-001")
// This example assumes 'referenceImage' is a pre-loaded Bitmap.
// In a real app, this might come from the user's device or a URL.
val referenceImage: Bitmap = TODO("Load your reference image Bitmap here")
// Define the style reference using the reference image.
val styleReference = ImagenStyleReference(
image = referenceImage,
referenceID = 1,
description = "Van Gogh style"
)
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the style reference with ID 1.
val prompt = "A cat flying through outer space, in the Van Gogh style[1]"
// Use the editImage API to perform the style customization.
// Pass the list of references, the prompt, and an editing configuration.
val editedImage = model.editImage(
references = listOf(styleReference),
prompt = prompt,
config = ImagenEditingConfig(
editSteps = 50 // Number of editing steps, a higher value can improve quality
)
)
// Process the result
}
Java
// Initialize the Vertex AI Gemini API backend service
// Optionally specify the location to access the model (for example, `us-central1`)
// Create an `ImagenModel` instance with an Imagen "capability" model
ImagenModel imagenModel = FirebaseAI.getInstance(GenerativeBackend.vertexAI("us-central1"))
.imagenModel(
/* modelName */ "imagen-3.0-capability-001");
ImagenModelFutures model = ImagenModelFutures.from(imagenModel);
// This example assumes 'referenceImage' is a pre-loaded Bitmap.
// In a real app, this might come from the user's device or a URL.
Bitmap referenceImage = null; // TODO("Load your image Bitmap here");
// Define the style reference using the reference image.
ImagenStyleReference subjectReference = new ImagenStyleReference.Builder()
.setImage(referenceImage)
.setReferenceID(1)
.setDescription("Van Gogh style")
.build();
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the style reference with ID 1.
String prompt = "A cat flying through outer space, in the Van Gogh style[1]";
// Define the editing configuration.
ImagenEditingConfig imagenEditingConfig = new ImagenEditingConfig.Builder()
.setEditSteps(50) // Number of editing steps, a higher value can improve quality
.build();
// Use the editImage API to perform the style customization.
// Pass the list of references, the prompt, and the editing configuration.
Futures.addCallback(model.editImage(Collections.singletonList(styleReference), prompt, imagenEditingConfig), new FutureCallback<ImagenGenerationResponse>() {
@Override
public void onSuccess(ImagenGenerationResponse result) {
if (result.getImages().isEmpty()) {
Log.d("TAG", "No images generated");
}
Bitmap bitmap = result.getImages().get(0).asBitmap();
// Use the bitmap to display the image in your UI
}
@Override
public void onFailure(Throwable t) {
// ...
}
}, Executors.newSingleThreadExecutor());
Web
網頁應用程式不支援使用 Imagen 模型編輯圖片。請於今年稍晚再回來查看!
Dart
import 'dart:typed_data';
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI Gemini API backend service
// Optionally specify a location to access the model (for example, `us-central1`)
final ai = FirebaseAI.vertexAI(location: 'us-central1');
// Create an `ImagenModel` instance with an Imagen "capability" model
final model = ai.imagenModel(model: 'imagen-3.0-capability-001');
// This example assumes 'referenceImage' is a pre-loaded Uint8List.
// In a real app, this might come from the user's device or a URL.
final Uint8List referenceImage = Uint8List(0); // TODO: Load your reference image data here
// Define the style reference using the reference image.
final styleReference = ImagenStyleReference(
image: referenceImage,
referenceId: 1,
description: 'Van Gogh style',
);
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the style reference with ID 1.
final prompt = "A cat flying through outer space, in the Van Gogh style[1]";
try {
// Use the editImage API to perform the style customization.
// Pass the list of references, the prompt, and an editing configuration.
final response = await model.editImage(
[styleReference],
prompt,
config: ImagenEditingConfig(
editSteps: 50, // Number of editing steps, a higher value can improve quality
),
);
// Process the result.
if (response.images.isNotEmpty) {
final editedImage = response.images.first.bytes;
// Use the editedImage (a Uint8List) to display the image, save it, etc.
print('Image successfully generated!');
} else {
// Handle the case where no images were generated.
print('Error: No images were generated.');
}
} catch (e) {
// Handle any potential errors during the API call.
print('An error occurred: $e');
}
Unity
Unity 不支援使用 Imagen 模型編輯圖片。請於今年稍晚再回來查看!
提示範本
在要求中,您可以定義 ImagenStyleReference
,指定圖片的參照 ID (以及風格說明,視需要),藉此提供參照圖片 (最多 4 張)。請注意,多張圖片可以有相同的參照 ID (例如同一種圖案的多張相片)。
然後在撰寫提示時參照這些 ID。舉例來說,您可以在提示中使用 [1]
,參照參考 ID 為 1
的圖片。如果提供主旨說明,也可以將其納入提示,方便使用者閱讀提示。
下表提供提示範本,可做為撰寫提示的起點,根據風格進行自訂。
用途 | 參考圖片 | 提示範本 | 範例 |
---|---|---|---|
物件樣式 | 主體圖像 (1 到 4 張) | 根據下列說明文字,在 STYLE_DESCRIPTION [1] 中生成圖片:IMAGE_DESCRIPTION。 | 根據下列說明文字,在 neon sign style [1] 中生成圖片:a sign saying have a great day。 |
不使用臉部網格輸入內容,為人物圖片套用風格 | 主體圖像 (1 到 4 張) | 請根據以下說明,製作與 SUBJECT_DESCRIPTION [1] 相關的圖片:SUBJECT_DESCRIPTION [1] 的肖像照${PROMPT} | 請根據以下說明製作「a woman with short hair[1]」的圖片:以 3D 卡通風格繪製「a woman with short hair[1]」的肖像,背景模糊處理。可愛的卡通人物,面帶微笑,看向鏡頭,粉彩色調 ... |
以臉部網格輸入內容為人物圖片套用風格 |
主體圖片 (1-3) 臉部網格控制圖片 (1) |
以SUBJECT_DESCRIPTION [1]的姿勢建立CONTROL_IMAGE [2]圖片,符合以下說明:SUBJECT_DESCRIPTION [1]的肖像照 ${PROMPT} | 以control image [2]的姿勢繪製「a woman with short hair [1]」的圖片,並符合以下描述:a woman with short hair [1]的肖像照,3D 卡通風格,背景模糊。可愛的角色,面帶微笑,看向鏡頭,粉彩色調 ... |
最佳做法和限制
用途
自訂功能提供自由形式的提示,可能會讓人誤以為模型能執行的工作超出訓練範圍。以下各節說明自訂功能的預期用途,以及非預期用途的範例 (僅列舉部分)。
我們建議您將這項功能用於預期用途,因為我們已針對這些用途訓練模型,預期能獲得良好結果。反之,如果強迫模型執行超出預期用途的工作,結果就會不盡理想。
預定用途
以下是建議根據樣式進行自訂的用途:
根據文字輸入內容生成圖像,並採用參考圖像提供的特定風格。
變更人物相片。
變更人物相片,但保留臉部表情。
不當用途範例
以下列舉幾個非預期的用途,這些用途是根據樣式進行自訂。模型並未針對這些用途進行訓練,因此可能會產生不佳的結果。
根據文字和參考圖像生成圖像,並透過參考圖像控制生成圖像的構圖。
從參考圖片生成人物圖像,參考圖片中的人物帶有特定臉部表情。
將兩個人放在不同場景,保留他們的身份,並使用參考圖片指定輸出圖片的樣式 (例如油畫)。
將寵物的相片轉換成繪圖,同時保留或指定圖片構圖。
將產品 (例如餅乾或沙發) 放置在不同場景,並從不同角度呈現產品,同時遵循特定圖片風格 (例如特定顏色、光線風格或動畫的擬真照片)。