Imagen का इस्तेमाल करके, किसी स्टाइल के आधार पर इमेज को पसंद के मुताबिक बनाना


इस पेज पर, Firebase AI Logic एसडीके टूल का इस्तेमाल करके, Imagen की कस्टमाइज़ेशन की सुविधा का इस्तेमाल करने का तरीका बताया गया है. इस सुविधा की मदद से, स्टाइल के आधार पर इमेज में बदलाव किया जा सकता है या इमेज जनरेट की जा सकती हैं.

यह कैसे काम करता है: आपको एक टेक्स्ट प्रॉम्प्ट और कम से कम एक रेफ़रंस इमेज देनी होती है. इससे किसी खास स्टाइल (जैसे कि पैटर्न, बनावट या डिज़ाइन स्टाइल) का पता चलता है. मॉडल इन इनपुट का इस्तेमाल करके, नई इमेज जनरेट करता है. यह इमेज, रेफ़रंस इमेज में बताई गई स्टाइल के हिसाब से होती है.

उदाहरण के लिए, आपको लोकप्रिय खुदरा कैटलॉग से मिली किसी इमेज के आधार पर, किचन की नई इमेज जनरेट की जा सकती है.

कोड पर जाएं



शुरू करने से पहले

यह सुविधा सिर्फ़ तब उपलब्ध होती है, जब Vertex AI Gemini API को एपीआई उपलब्ध कराने वाली सेवा के तौर पर इस्तेमाल किया जा रहा हो.

अगर आपने अब तक शुरुआती गाइड नहीं पढ़ी है, तो इसे पढ़ें. इसमें बताया गया है कि Firebase प्रोजेक्ट कैसे सेट अप करें, अपने ऐप्लिकेशन को Firebase से कैसे कनेक्ट करें, एसडीके कैसे जोड़ें, चुने गए एपीआई उपलब्ध कराने वाली कंपनी के लिए बैकएंड सेवा को कैसे शुरू करें, और ImagenModel इंस्टेंस कैसे बनाएं.

इस सुविधा के साथ काम करने वाले मॉडल

Imagen, capability मॉडल के ज़रिए इमेज में बदलाव करने की सुविधा देता है:

  • imagen-3.0-capability-001

ध्यान दें कि Imagen मॉडल के लिए, global लोकेशन की जानकारी नहीं दी जा सकती.

स्टाइल को पसंद के मुताबिक बनाने का अनुरोध भेजना

यहां दिए गए सैंपल में, स्टाइल को पसंद के मुताबिक बनाने का अनुरोध दिखाया गया है. इसमें मॉडल से, दी गई रेफ़रंस इमेज की स्टाइल में नई इमेज जनरेट करने के लिए कहा गया है. इस उदाहरण में, रेफ़रंस इमेज के तौर पर वैन गॉग की "स्टारी नाइट" का इस्तेमाल किया गया है.

प्रॉम्प्ट लिखने और उनमें रेफ़रंस इमेज इस्तेमाल करने के बारे में जानने के लिए, इस पेज पर बाद में प्रॉम्प्ट टेंप्लेट देखें.

Swift

Swift के लिए, Imagen मॉडल की मदद से इमेज में बदलाव करने की सुविधा उपलब्ध नहीं है. इस साल के आखिर में फिर से देखें!

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

Unity के लिए, Imagen मॉडल की मदद से इमेज में बदलाव करने की सुविधा काम नहीं करती. इस साल के आखिर में फिर से देखें!

प्रॉम्प्ट टेंप्लेट

अनुरोध में, आपको रेफ़रंस इमेज (ज़्यादा से ज़्यादा चार इमेज) देनी होती हैं. इसके लिए, आपको ImagenStyleReference तय करना होता है. इसमें आपको किसी इमेज के लिए रेफ़रंस आईडी तय करना होता है. साथ ही, स्टाइल के बारे में जानकारी भी दी जा सकती है. ध्यान दें कि एक से ज़्यादा इमेज के लिए, एक ही रेफ़रंस आईडी हो सकता है. उदाहरण के लिए, एक ही पैटर्न की कई फ़ोटो.

इसके बाद, प्रॉम्प्ट लिखते समय इन आईडी का इस्तेमाल करें. उदाहरण के लिए, प्रॉम्प्ट में [1] का इस्तेमाल करके, रेफ़रंस आईडी 1 वाली इमेज का रेफ़रंस दिया जाता है. अगर आपने विषय के बारे में जानकारी दी है, तो उसे प्रॉम्प्ट में भी शामिल किया जा सकता है. इससे, प्रॉम्प्ट को मैन्युअल तरीके से पढ़ना आसान हो जाता है.

यहां दी गई टेबल में, प्रॉम्प्ट टेंप्लेट दिए गए हैं. इनका इस्तेमाल, स्टाइल के हिसाब से प्रॉम्प्ट लिखने के लिए किया जा सकता है.

इस्तेमाल का उदाहरण रेफ़रंस इमेज प्रॉम्प्ट टेंप्लेट उदाहरण
ऑब्जेक्ट स्टाइल सब्जेक्ट की इमेज (1-4) इस कैप्शन IMAGE_DESCRIPTION के आधार पर, STYLE_DESCRIPTION [1] में एक इमेज जनरेट करो. इस कैप्शन a sign saying have a great day के आधार पर, neon sign style [1] में एक इमेज जनरेट करो.
चेहरे के मेश इनपुट के बिना व्यक्ति की इमेज को स्टाइल करना सब्जेक्ट की इमेज (1-4) SUBJECT_DESCRIPTION [1] के बारे में एक इमेज बनाओ, जो इस ब्यौरे से मेल खाती हो: SUBJECT_DESCRIPTION [1] का पोर्ट्रेट ${PROMPT} a woman with short hair[1] के बारे में एक इमेज बनाओ, जो इस ब्यौरे से मेल खाती हो: a woman with short hair[1] का पोर्ट्रेट, 3D कार्टून स्टाइल में और धुंधले बैकग्राउंड के साथ. एक प्यारा और सुंदर किरदार, मुस्कुराते हुए चेहरे के साथ, कैमरे की ओर देख रहा है, हल्के रंग का टोन ...
चेहरे के मेश इनपुट की मदद से, व्यक्ति की इमेज को स्टाइल में बदलना सब्जेक्ट की इमेज (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] का पोर्ट्रेट 3D-कार्टून स्टाइल में, धुंधले बैकग्राउंड के साथ. एक प्यारा और सुंदर किरदार, मुस्कुराते हुए चेहरे के साथ, कैमरे की ओर देख रहा है, हल्के रंग का टोन ...



सबसे सही तरीके और सीमाएं

इस्तेमाल के उदाहरण

कस्टम बनाने की सुविधा में, प्रॉम्प्ट को अपनी पसंद के मुताबिक बनाने का विकल्प मिलता है. इससे ऐसा लग सकता है कि मॉडल, ट्रेनिंग के दौरान सीखी गई बातों के अलावा भी कई काम कर सकता है. यहां दिए गए सेक्शन में, कस्टम बनाने की सुविधा के इस्तेमाल के सही उदाहरण और इस्तेमाल के गलत उदाहरण दिए गए हैं. हालांकि, ये उदाहरण पूरी तरह से नहीं दिए गए हैं.

हमारा सुझाव है कि आप इस सुविधा का इस्तेमाल, उन मामलों के लिए करें जिनके लिए इसे बनाया गया है. ऐसा इसलिए, क्योंकि हमने मॉडल को उन मामलों के लिए ट्रेन किया है और हमें उनसे अच्छे नतीजे मिलने की उम्मीद है. इसके उलट, अगर मॉडल को ऐसे काम करने के लिए कहा जाता है जो उसके इस्तेमाल के मकसद से अलग हैं, तो आपको खराब नतीजे मिल सकते हैं.

इस्तेमाल के उदाहरण

यहां अनुमति वाले कुछ उदाहरण दिए गए हैं, जिनमें स्टाइल के आधार पर बदलाव किया जा सकता है:

  • टेक्स्ट इनपुट से ऐसी इमेज जनरेट करना जो रेफ़रंस इमेज में दी गई स्टाइल के मुताबिक हो.

  • किसी व्यक्ति की फ़ोटो में बदलाव करना.

  • किसी व्यक्ति की फ़ोटो में बदलाव करना और उसके चेहरे के भाव को बनाए रखना.

इस्तेमाल के ऐसे उदाहरण जो मकसद के मुताबिक नहीं हैं

यहां स्टाइल के आधार पर, पसंद के मुताबिक बनाए गए कॉन्टेंट के अनचाहे इस्तेमाल के उदाहरणों की सूची दी गई है. इसमें इनके अलावा, कई और उदाहरण शामिल हो सकते हैं. मॉडल को इस्तेमाल के इन उदाहरणों के लिए ट्रेन नहीं किया गया है. इसलिए, हो सकता है कि यह खराब नतीजे दे.

  • टेक्स्ट और रेफ़रंस इमेज का इस्तेमाल करके इमेज जनरेट करना. ऐसा इसलिए किया जाता है, ताकि रेफ़रंस इमेज से जनरेट की गई कंपोज़िशन को कुछ हद तक कंट्रोल किया जा सके.

  • किसी व्यक्ति की ऐसी इमेज जनरेट करें जिसमें उसके चेहरे के भाव, रेफ़रंस इमेज में मौजूद व्यक्ति के चेहरे के भाव से मिलते-जुलते हों.

  • दो लोगों को किसी दूसरे सीन में रखो. उनकी पहचान को सुरक्षित रखो. साथ ही, किसी रेफ़रंस इमेज का इस्तेमाल करके, आउटपुट इमेज की स्टाइल (जैसे, ऑयल पेंटिंग) तय करो.

  • किसी पालतू जानवर की फ़ोटो को ड्रॉइंग में बदलें. साथ ही, इमेज की कंपोज़िशन को बनाए रखें या उसे तय करें.

  • किसी प्रॉडक्ट, जैसे कि कुकी या सोफ़ा को अलग-अलग सीन में रखें. साथ ही, प्रॉडक्ट को अलग-अलग ऐंगल से दिखाएं. इसके अलावा, इमेज को किसी खास स्टाइल में दिखाएं. जैसे, फ़ोटो जैसा दिखने वाला, खास रंग, लाइटिंग स्टाइल या ऐनिमेशन वाला.