התאמה אישית של תמונות על סמך סגנון שצוין באמצעות Imagen


בדף הזה מוסבר איך להשתמש ביכולת ההתאמה האישית של 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 לא נתמך.

שליחת בקשה להתאמה אישית של סגנון

בדוגמה הבאה מוצגת בקשה להתאמה אישית של סגנון, שבה המודל מתבקש ליצור תמונה חדשה בסגנון של תמונת ההפניה שסופקה (בדוגמה הזו, "ליל כוכבים" של ואן גוך).

בהמשך הדף הזה מפורטות תבניות של הנחיות, שבעזרתן אפשר ללמוד איך לכתוב הנחיות ואיך להשתמש בתמונות להשוואה בתוך ההנחיות.

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 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(
        referenceImages = listOf(styleReference),
        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 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 = ((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 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] בסגנון קריקטורה בתלת-ממד עם רקע מטושטש. דמות חמודה ומקסימה, עם פנים מחייכות, מסתכלת למצלמה, גוון צבעי פסטל ...



שיטות מומלצות ומגבלות

תרחישים לדוגמה

היכולת להתאים אישית מאפשרת להזין הנחיות בסגנון חופשי, ולכן יכול להיווצר הרושם שהמודל יכול לעשות יותר ממה שהוא אומן לעשות. בקטעים הבאים מתוארים תרחישי שימוש מיועדים להתאמה אישית, ודוגמאות לא מיועדות לשימוש.

מומלץ להשתמש ביכולת הזו בתרחישי השימוש המיועדים, כי אימנו את המודל על תרחישי השימוש האלה ואנחנו צופים תוצאות טובות. לעומת זאת, אם תנסו להשתמש במודל למטרות שלא לשמן הוא נועד, התוצאות יהיו גרועות.

תרחישים לדוגמה לשימוש

הנה כמה תרחישי שימוש מיועדים להתאמה אישית על סמך סגנון:

  • יצירת תמונה מטקסט קלט לפי סגנון ספציפי שמופיע בתמונה לדוגמה.

  • לשנות תמונה של אדם.

  • תשנה תמונה של אדם ותשמור על הבעת הפנים שלו.

דוגמאות לתרחישי שימוש לא מכוונים

הרשימה הבאה היא רשימה חלקית של תרחישי שימוש לא מכוונים בהתאמה אישית שמבוססת על סגנון. המודל לא אומן לתרחישי השימוש האלה, ולכן סביר להניח שהוא יפיק תוצאות לא טובות.

  • ליצור תמונה מטקסט ומשימוש בתמונה לדוגמה, במטרה לקבל רמה מסוימת של שליטה על הקומפוזיציה שנוצרת מהתמונה לדוגמה.

  • יצירת תמונה של אדם מתמונה לדוגמה שבה מופיע אדם עם הבעת פנים מסוימת.

  • תמקם שני אנשים בסצנה אחרת, תשמור על הזהויות שלהם ותציין את הסגנון של תמונת הפלט (למשל, ציור שמן) באמצעות תמונה לדוגמה.

  • ליצור סגנון לתמונה של חיית מחמד ולהפוך אותה לציור, תוך שמירה על הקומפוזיציה של התמונה או הגדרת קומפוזיציה חדשה.

  • הנחיה: תמקם מוצר, כמו עוגייה או ספה, בסצנות שונות עם זוויות שונות של המוצר, ותשתמש בסגנון תמונה ספציפי (למשל, פוטוריאליסטי עם צבעים ספציפיים, סגנונות תאורה או אנימציה).