בדף הזה מוסבר איך להשתמש ביכולת ההתאמה האישית של Imagen כדי לערוך או ליצור תמונות על סמך הגדרת בקרה באמצעות ערכות ה-SDK של Firebase AI Logic.
איך זה עובד: אתם מספקים הנחיית טקסט ולפחות תמונה אחת של בקרה (כמו ציור או תמונה עם קצוות של קאני). המודל משתמש בקלט הזה כדי ליצור תמונה חדשה על סמך תמונות הבקרה.
לדוגמה, אתם יכולים לספק למודל ציור של חללית וירח, יחד עם הנחיית טקסט ליצירת ציור בצבעי מים שמבוסס על הציור.
סוגים של תמונות לדוגמה
תמונת ההפניה להתאמה אישית מבוקרת יכולה להיות שרטוט, תמונה עם קצוות קאני או רשת פנים.
לפני שמתחילים
אפשרות זמינה רק כשמשתמשים ב-Vertex AI Gemini API כספק ה-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 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 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(
references = listOf(controlReference),
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 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 = 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 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] בסגנון סרטים מצוירים בתלת ממד עם רקע מטושטש. דמות חמודה ומקסימה, עם פנים מחייכות, שמסתכלת על המצלמה, בגווני פסטל ... |
שיטות מומלצות ומגבלות
תרחישים לדוגמה
היכולת להתאים אישית מאפשרת להזין הנחיות בסגנון חופשי, ולכן יכול להיווצר הרושם שהמודל יכול לעשות יותר ממה שהוא אומן לעשות. בקטעים הבאים מתוארים תרחישי שימוש מיועדים להתאמה אישית, ודוגמאות לא מיועדות לשימוש.
מומלץ להשתמש ביכולת הזו בתרחישי השימוש המיועדים, כי אימנו את המודל על תרחישי השימוש האלה ואנחנו צופים תוצאות טובות. לעומת זאת, אם תנסו להשתמש במודל למטרות שלא לשמן הוא נועד, התוצאות יהיו גרועות.
תרחישים לדוגמה לשימוש
התרחישים הבאים הם תרחישים לדוגמה לשימוש באמצעי בקרה כדי לבצע התאמה אישית, כפי שהם מיועדים:
יצירת תמונה בהתאם להנחיה ולתמונות השליטה של קצוות קאני.
יצירת תמונה לפי ההנחיה ותמונות השירבוטים.
הוספת סגנון לתמונה של אדם תוך שמירה על הבעת הפנים.
דוגמאות לתרחישי שימוש לא מכוונים
בהמשך מופיעה רשימה חלקית של תרחישי שימוש לא מכוונים להתאמה אישית על סמך אמצעי בקרה. המודל לא אומן לתרחישי השימוש האלה, וסביר להניח שהוא יפיק תוצאות לא טובות.
ליצור תמונה באמצעות סגנון שמצוין בהנחיה.
ליצור תמונה מטקסט בסגנון מסוים שמופיע בתמונה לדוגמה, עם שליטה מסוימת בקומפוזיציה של התמונה באמצעות תמונה לשליטה.
ליצור תמונה מטקסט בסגנון ספציפי שמופיע בתמונה לדוגמה, עם שליטה מסוימת בקומפוזיציה של התמונה באמצעות שרבוט בקרה.
ליצור תמונה מטקסט בסגנון ספציפי שמופיע בתמונה לדוגמה, עם רמה מסוימת של שליטה בקומפוזיציה של התמונה באמצעות תמונה לשליטה. לאדם בתמונה יש הבעת פנים ספציפית.
ליצור סגנון לתמונה של שני אנשים או יותר, ולשמור על הבעות הפנים שלהם.
ליצור סגנון לתמונה של חיית מחמד ולהפוך אותה לציור. שמירה או ציון של קומפוזיציית התמונה (לדוגמה, צבעי מים).