התאמה אישית של תמונות על סמך אמצעי בקרה באמצעות Imagen


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

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

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

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



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

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

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

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

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

התרחישים הבאים הם תרחישים לדוגמה לשימוש באמצעי בקרה כדי לבצע התאמה אישית, כפי שהם מיועדים:

  • יצירת תמונה על סמך ההנחיה ותמונות הבקרה של קצוות קאני.

  • יצירת תמונה על סמך ההנחיה ותמונות השירבוטים.

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

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

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

  • ליצור תמונה באמצעות סגנון שמצוין בהנחיה.

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

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

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

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

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