בדף הזה מוסבר איך להשתמש ביכולת ההתאמה האישית של Imagen כדי לערוך או ליצור תמונות על סמך אמצעי בקרה שצוין באמצעות ערכות ה-SDK של Firebase AI Logic.
איך זה עובד: אתם מספקים פרומפט טקסטואלי ולפחות תמונה לדוגמה אחת לשליטה (כמו ציור או תמונה עם קצוות קאני). המודל משתמש בקלט הזה כדי ליצור תמונה חדשה שמבוססת על תמונות הבקרה.
לדוגמה, אתם יכולים לספק למודל ציור של חללית וירח, יחד עם פרומפט טקסטואלי ליצירת ציור בצבעי מים שמבוסס על הציור.
סוגים של תמונות לדוגמה לבקרה
תמונת ההפניה להתאמה אישית מבוקרת יכולה להיות שרטוט, תמונה עם קצוות של קאני או רשת פנים.
לפני שמתחילים
| האפשרות הזו זמינה רק כשמשתמשים ב-Agent Platform Gemini API (formerly Vertex AI) כספק ה-API. |
אם עדיין לא עשיתם את זה, כדאי לעיין במדריך לתחילת העבודה. במדריך הזה מוסבר איך להגדיר את פרויקט Firebase, לקשר את האפליקציה ל-Firebase, להוסיף את ה-SDK, לאתחל את שירות ה-Backend של ספק ה-API שבחרתם וליצור מופע של ImagenModel.
מודלים שתומכים ביכולת הזו
Imagen מציע עריכת תמונות באמצעות מודל capability:
imagen-3.0-capability-001
הערה: בImagen מודלים, המיקום global לא נתמך.
שליחת בקשה מבוקרת להתאמה אישית
בדוגמה הבאה מוצגת בקשה מבוקרת להתאמה אישית, שבה המודל מתבקש ליצור תמונה חדשה על סמך תמונת ההפניה שסופקה (בדוגמה הזו, ציור של חלל, כמו טיל וירח). מכיוון שתמונת ההפניה היא סקיצה או מתאר גסים שצויירו ביד, נעשה בה שימוש בסוג הבקרה CONTROL_TYPE_SCRIBBLE.
אם תמונת ההפניה היא תמונה עם קצוות קאני או רשת פנים, אפשר להשתמש בדוגמה הזו גם כן, אבל עם השינויים הבאים:
אם תמונת ההפניה היא תמונת קצוות של קאני, צריך להשתמש בסוג הבקרה
CONTROL_TYPE_CANNY.אם תמונת לדוגמה היא רשת פנים, צריך להשתמש בסוג הבקרה
CONTROL_TYPE_FACE_MESH. אפשר להשתמש באמצעי הבקרה הזה רק עם התאמה אישית של נושאים לאנשים.
בהמשך הדף הזה מפורטות תבניות של הנחיות, שבעזרתן אפשר ללמוד איך לכתוב הנחיות ואיך להשתמש בתמונות להשוואה בתוך ההנחיות.
Swift
אין תמיכה בעריכת תמונות באמצעות מודלים של Imagen ב-Swift. כדאי לחזור לכאן בהמשך השנה.
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun customizeImage() {
// Initialize the Agent Platform Gemini API backend service (formerly branded Vertex AI).
// 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 subject reference using the reference image.
val controlReference = ImagenControlReference(
image = referenceImage,
referenceID = 1,
controlType = CONTROL_TYPE_SCRIBBLE
)
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the subject reference with ID 1.
val prompt = "A cat flying through outer space arranged like the space scribble[1]"
// Use the editImage API to perform the controlled customization.
// Pass the list of references, the prompt, and an editing configuration.
val editedImage = model.editImage(
referenceImages = listOf(controlReference),
prompt = prompt,
config = ImagenEditingConfig(
editSteps = 50 // Number of editing steps, a higher value can improve quality
)
)
// Process the result
}
Java
// Initialize the Agent Platform Gemini API backend service (formerly branded Vertex AI).
// 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 subject reference using the reference image.
ImagenControlReference controlReference = new ImagenControlReference.Builder()
.setImage(referenceImage)
.setReferenceID(1)
.setControlType(CONTROL_TYPE_SCRIBBLE)
.build();
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the subject reference with ID 1.
String prompt = "A cat flying through outer space arranged like the space scribble[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 controlled customization.
// Pass the list of references, the prompt, and an editing configuration.
Futures.addCallback(model.editImage(Collections.singletonList(controlReference), prompt, imagenEditingConfig), new FutureCallback<ImagenGenerationResponse>() {
@Override
public void onSuccess(ImagenGenerationResponse result) {
if (result.getImages().isEmpty()) {
Log.d("TAG", "No images generated");
}
Bitmap bitmap = ((ImagenInlineImage) 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 Agent Platform Gemini API backend service (formerly branded Vertex AI).
// 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 control reference using the reference image.
final controlReference = ImagenControlReference(
image: referenceImage,
referenceId: 1,
controlType: ImagenControlType.scribble,
);
// Provide a prompt that describes the final image.
// The "[1]" links the prompt to the subject reference with ID 1.
final prompt = "A cat flying through outer space arranged like the space scribble[1]";
try {
// Use the editImage API to perform the controlled customization.
// Pass the list of references, the prompt, and an editing configuration.
final response = await model.editImage(
[controlReference],
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
אין תמיכה בעריכת תמונות באמצעות מודלים של Imagen ב-Unity. כדאי לחזור לכאן בהמשך השנה.
תבניות לפרומפטים
בבקשה, צריך לספק תמונות לדוגמה (עד 4 תמונות) על ידי הגדרת ImagenControlReference שבו מציינים מזהה של תמונה לדוגמה.
שימו לב שאפשר להשתמש באותו מזהה הפניה לכמה תמונות (למשל, כמה שרבוטים של אותו רעיון).
אחר כך, כשכותבים את ההנחיה, מציינים את המזהים האלה. לדוגמה, משתמשים ב-[1] בהנחיה כדי להפנות לתמונות עם מזהה ההפניה 1.
בטבלה הבאה מופיעות תבניות של הנחיות שיכולות לשמש כנקודת התחלה לכתיבת הנחיות להתאמה אישית על סמך אמצעי בקרה.
| תרחיש שימוש | תמונות לדוגמה | תבנית לפרומפט | דוגמה |
|---|---|---|---|
| התאמה אישית מבוקרת | מפת שרבוטים (1) | יצירת תמונה שמתאימה לscribble map [1] לפי התיאור: ${STYLE_PROMPT} ${PROMPT}. | צור תמונה שתתאים לscribble map [1] כדי שתהיה זהה לתיאור: התמונה צריכה להיות בסגנון של ציור שמן אימפרסיוניסטי עם משיכות מכחול רפויות. התמונה צריכה להיות עם אווירה מוארת באופן טבעי ומשיכות מכחול בולטות. מבט מהצד על מכונית. המכונית חונה על משטח כביש רטוב ומבריק, ואורות העיר משתקפים בשלוליות. |
| התאמה אישית מבוקרת | תמונה של שליטה ב-Canny (1) | יצירת תמונה שתואמת לedge map [1] כדי שתתאים לתיאור: ${STYLE_PROMPT} ${PROMPT} | צור תמונה שתואמת לedge map [1] בהתאם לתיאור הבא: התמונה צריכה להיות בסגנון של ציור שמן אימפרסיוניסטי, עם משיכות מכחול רפויות. התמונה צריכה להיות מוארת באופן טבעי, עם אווירה נעימה ומשיכות מכחול בולטות. מבט מהצד על מכונית. המכונית חונה על משטח כביש רטוב ומבריק, ואורות העיר משתקפים בשלוליות. |
| עיצוב תמונה של אדם עם קלט FaceMesh |
תמונת הנושא (1-3) תמונת בקרה של FaceMesh (1) |
תצור תמונה של SUBJECT_DESCRIPTION [1] בתנוחה של CONTROL_IMAGE [2] בהתאם לתיאור: תמונה של SUBJECT_DESCRIPTION [1] ${PROMPT} | תיצור תמונה של a woman with short hair [1] בתנוחה של control image [2] בהתאם לתיאור הבא: פורטרט של a woman with short hair [1] בסגנון קריקטורה בתלת-ממד עם רקע מטושטש. דמות חמודה ומקסימה, עם פנים מחייכות, מסתכלת למצלמה, גוון צבעי פסטל ... |
| עיצוב תמונה של אדם עם קלט FaceMesh |
תמונת הנושא (1-3) תמונת בקרה של FaceMesh (1) |
תצור תמונה של ${STYLE_PROMPT} בנושא SUBJECT_DESCRIPTION [1] בתנוחה של CONTROL_IMAGE [2] כדי שתתאים לתיאור: דיוקן של SUBJECT_DESCRIPTION [1] ${PROMPT} | צור תמונה בסגנון סרטים מצוירים בתלת ממד של a woman with short hair [1] בתנוחה של control image [2] שתתאים לתיאור: פורטרט של a woman with short hair [1] בסגנון סרטים מצוירים בתלת ממד עם רקע מטושטש. דמות חמודה ומקסימה עם פרצוף מחייך, שמסתכלת למצלמה, בגווני פסטל ... |
שיטות מומלצות ומגבלות
תרחישים לדוגמה
היכולת להתאים אישית מאפשרת להזין הנחיות בסגנון חופשי, ולכן יכול להיווצר הרושם שהמודל יכול לעשות יותר ממה שהוא אומן לעשות. בקטעים הבאים מתוארים תרחישי שימוש מיועדים להתאמה אישית, ודוגמאות לא מיועדות לשימוש.
מומלץ להשתמש ביכולת הזו בתרחישי השימוש המיועדים, כי אימנו את המודל על תרחישי השימוש האלה ואנחנו צופים תוצאות טובות. לעומת זאת, אם תנסו להשתמש במודל למטרות שלא לשמן הוא נועד, התוצאות יהיו גרועות.
תרחישים לדוגמה לשימוש
התרחישים הבאים הם תרחישים לדוגמה לשימוש באמצעי בקרה לצורך התאמה אישית:
יצירת תמונה לפי הפרומפט ותמונות השליטה של קצוות קאני.
יצירת תמונה שנוצרת בהתאם לפרומפט ולתמונות של השרבוטים.
תעצב תמונה של אדם תוך שמירה על הבעת הפנים.
דוגמאות לתרחישי שימוש לא מכוונים
הנה רשימה חלקית של תרחישי שימוש לא מכוונים בהתאמה אישית שמבוססת על אמצעי בקרה. המודל לא אומן לתרחישי השימוש האלה, ולכן סביר להניח שהוא יפיק תוצאות לא טובות.
ליצור תמונה באמצעות סגנון שמצוין בפרומפט.
ליצור תמונה מטקסט בסגנון ספציפי שמוגדר על ידי תמונה לדוגמה, עם רמה מסוימת של שליטה בקומפוזיציה של התמונה באמצעות תמונה לשליטה.
ליצור תמונה מטקסט בסגנון ספציפי שמוגדר על ידי תמונה לדוגמה, עם רמה מסוימת של שליטה בקומפוזיציה של התמונה באמצעות שרבוט בקרה.
תמונה שנוצרת מטקסט בסגנון ספציפי שמוגדר בתמונה לדוגמה, עם רמת שליטה מסוימת על קומפוזיציית התמונה באמצעות תמונה לבקרה. לאדם בתמונה יש הבעת פנים ספציפית.
תעצב תמונה של שני אנשים או יותר, ותשמור על הבעות הפנים שלהם.
תעצב תמונה של חיית מחמד ותהפוך אותה לציור. שמירה או ציון של קומפוזיציה של התמונה (לדוגמה, צבעי מים).