בדף הזה מוסבר איך להשתמש ביכולת ההתאמה האישית של 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
לא נתמך.
שליחת בקשה להתאמה אישית של סגנון
בדוגמה הבאה מוצגת בקשה להתאמה אישית של סגנון, שבה המודל מתבקש ליצור תמונה חדשה בסגנון של תמונה לדוגמה שסופקה (בדוגמה הזו, 'ליל כוכבים' של ואן גוך).
בהמשך הדף הזה מפורטות תבניות של הנחיות. תוכלו לקרוא על כתיבת הנחיות ועל שימוש בתמונות להשוואה בתוך ההנחיות.
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 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
אין תמיכה בעריכת תמונות באמצעות מודלים של Imagen ב-Unity. כדאי לחזור לכאן בהמשך השנה.
תבניות להנחיות
בבקשה, צריך לספק תמונות להשוואה (עד 4 תמונות) על ידי הגדרת תג ImagenStyleReference
שבו מציינים מזהה של תמונה להשוואה (ואפשר גם תיאור של הסגנון). שימו לב שאפשר להשתמש באותו מזהה הפניה לכמה תמונות (למשל, כמה תמונות של אותו דפוס).
אחר כך, כשכותבים את ההנחיה, מציינים את המזהים האלה. לדוגמה, משתמשים ב-[1]
בהנחיה כדי להפנות לתמונות עם מזהה ההפניה 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] שתתאים לתיאור: דיוקן של a woman with short hair[1] בסגנון סרטים מצוירים בתלת ממד עם רקע מטושטש. דמות חמודה ומקסימה, עם פנים מחייכות, מביטה למצלמה, גוון צבעי פסטל ... |
סגנון של תמונת אדם עם קלט של רשת פנים |
תמונת הנושא (1-3) תמונת בקרה של רשת הפנים (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] בסגנון קריקטורה בתלת-ממד עם רקע מטושטש. דמות חמודה ומקסימה עם פנים מחייכות, שמסתכלת למצלמה, בגווני פסטל ... |
שיטות מומלצות ומגבלות
תרחישים לדוגמה
היכולת להתאים אישית מאפשרת להזין הנחיות בסגנון חופשי, ולכן יכול להיווצר הרושם שהמודל יכול לעשות יותר ממה שהוא אומן לעשות. בקטעים הבאים מתוארים תרחישי שימוש מיועדים להתאמה אישית, ודוגמאות לא מיועדות לשימוש.
מומלץ להשתמש ביכולת הזו בתרחישי השימוש המיועדים, כי אימנו את המודל על תרחישי השימוש האלה ואנחנו צופים תוצאות טובות. לעומת זאת, אם תנסו להשתמש במודל למטרות שלא לשמן הוא נועד, התוצאות יהיו גרועות.
תרחישים לדוגמה לשימוש
הנה כמה תרחישי שימוש מיועדים להתאמה אישית על סמך סגנון:
ליצור תמונה מטקסט קלט לפי סגנון ספציפי שמופיע בתמונת עזר.
לשנות תמונה של אדם.
לשנות תמונה של אדם ולשמור על הבעת הפנים שלו.
דוגמאות לתרחישי שימוש לא מכוונים
בהמשך מופיעה רשימה חלקית של תרחישים לדוגמה לשימוש לא מכוון בהתאמה אישית שמבוססת על סגנון. המודל לא אומן לתרחישי השימוש האלה, וסביר להניח שהוא יפיק תוצאות לא טובות.
תמונה שנוצרה מטקסט ומתוך תמונה לדוגמה, במטרה לקבל שליטה מסוימת על הקומפוזיציה שנוצרה מתוך התמונה לדוגמה.
יצירת תמונה של אדם מתמונת הפניה שבה מופיע אדם עם הבעת פנים מסוימת.
תמקם שני אנשים בסצנה אחרת, תשמור על הזהויות שלהם ותציין את הסגנון של תמונת הפלט (למשל, ציור שמן) באמצעות תמונת הפניה.
ליצור סגנון לתמונה של חיית מחמד ולהפוך אותה לציור, תוך שמירה על הקומפוזיציה של התמונה או הגדרת קומפוזיציה חדשה.
הנחיה: צור תמונות של מוצר, למשל עוגייה או ספה, בסצנות שונות עם זוויות שונות של המוצר, בסגנון תמונה ספציפי (למשל פוטוריאליסטי עם צבעים ספציפיים, סגנונות תאורה או אנימציה).