Imagen ব্যবহার করে ইমেজে বস্তু সন্নিবেশ করান


এই পৃষ্ঠায় বর্ণনা করা হয়েছে কিভাবে Firebase AI Logic SDK ব্যবহার করে ইমেজেন ব্যবহার করে ইনপেইন্টিং ব্যবহার করে ছবিতে কোনও বস্তু সন্নিবেশ করানো যায়

ইনপেইন্টিং হলো এক ধরণের মাস্ক-ভিত্তিক সম্পাদনামাস্ক হলো একটি ডিজিটাল ওভারলে যা আপনি যে নির্দিষ্ট এলাকাটি সম্পাদনা করতে চান তা নির্ধারণ করে।

এটি কীভাবে কাজ করে : আপনি একটি আসল ছবি এবং একটি সংশ্লিষ্ট মাস্ক করা ছবি প্রদান করেন — হয় স্বয়ংক্রিয়ভাবে তৈরি অথবা আপনার দ্বারা সরবরাহ করা — যা এমন একটি এলাকার উপর একটি মাস্ক সংজ্ঞায়িত করে যেখানে আপনি নতুন কন্টেন্ট যোগ করতে চান। আপনি কী যোগ করতে চান তা বর্ণনা করে একটি টেক্সট প্রম্পটও প্রদান করেন। এরপর মডেলটি মাস্ক করা এলাকার মধ্যে নতুন কন্টেন্ট তৈরি করে এবং যোগ করে।

উদাহরণস্বরূপ, আপনি একটি টেবিল মাস্ক করতে পারেন এবং মডেলকে ফুলের একটি ফুলদানি যোগ করতে বলতে পারেন।

স্বয়ংক্রিয়ভাবে তৈরি মাস্কের জন্য কোডে যান মাস্ক সরবরাহের জন্য কোডে যান

শুরু করার আগে

শুধুমাত্র আপনার API প্রদানকারী হিসেবে Vertex AI Gemini API ব্যবহার করলেই এটি উপলব্ধ।

যদি আপনি ইতিমধ্যেই না করে থাকেন, তাহলে শুরু করার নির্দেশিকাটি সম্পূর্ণ করুন, যেখানে আপনার Firebase প্রকল্পটি কীভাবে সেট আপ করবেন, আপনার অ্যাপটিকে Firebase-এর সাথে সংযুক্ত করবেন, SDK যোগ করবেন, আপনার নির্বাচিত API প্রদানকারীর জন্য ব্যাকএন্ড পরিষেবা শুরু করবেন এবং একটি ImagenModel ইনস্ট্যান্স তৈরি করবেন তা বর্ণনা করা হয়েছে।

এই ক্ষমতা সমর্থন করে এমন মডেলগুলি

ইমেজেন তার capability মডেলের মাধ্যমে চিত্র সম্পাদনা প্রদান করে:

  • imagen-3.0-capability-001

মনে রাখবেন যে Imagen মডেলের জন্য, global অবস্থান সমর্থিত নয়

একটি স্বয়ংক্রিয়ভাবে তৈরি মাস্ক ব্যবহার করে বস্তু সন্নিবেশ করান

এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই নির্দেশিকার "শুরু করার আগে" বিভাগটি সম্পূর্ণ করুন।

নিম্নলিখিত নমুনাটি দেখায় কিভাবে একটি ছবিতে কন্টেন্ট সন্নিবেশ করার জন্য ইনপেইন্টিং ব্যবহার করতে হয় — স্বয়ংক্রিয় মাস্ক জেনারেশন ব্যবহার করে। আপনি মূল ছবি এবং একটি টেক্সট প্রম্পট প্রদান করেন, এবং ইমেজেন স্বয়ংক্রিয়ভাবে মূল ছবি পরিবর্তন করার জন্য একটি মাস্ক এরিয়া সনাক্ত করে এবং তৈরি করে।

সুইফট

Swift-এর জন্য Imagen মডেলের মাধ্যমে ছবি সম্পাদনা সমর্থিত নয়। এই বছরের শেষের দিকে আবার দেখুন!

Kotlin

স্বয়ংক্রিয়ভাবে তৈরি মাস্ক ব্যবহার করে বস্তু সন্নিবেশ করতে, ImagenBackgroundMask উল্লেখ করুন। editImage() ব্যবহার করুন এবং সম্পাদনা কনফিগারেশনটি ImagenEditMode.INPAINT_INSERTION ব্যবহার করতে সেট করুন।

// 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 'originalImage' is a pre-loaded Bitmap.
    // In a real app, this might come from the user's device or a URL.
    val originalImage: Bitmap = TODO("Load your original image Bitmap here")

    // Provide the prompt describing the content to be inserted.
    val prompt = "a vase of flowers on the table"

    // Use the editImage API to insert the new content.
    // Pass the original image, the prompt, and an editing configuration.
    val editedImage = model.editImage(
        referenceImages = listOf(
            ImagenRawImage(originalImage.toImagenInlineImage()),
            ImagenBackgroundMask(), // Use ImagenBackgroundMask() to auto-generate the mask.
        ),
        prompt = prompt,
        // Define the editing configuration for inpainting and insertion.
        config = ImagenEditingConfig(ImagenEditMode.INPAINT_INSERTION)
    )

    // Process the resulting 'editedImage' Bitmap, for example, by displaying it in an ImageView.
}

Java

স্বয়ংক্রিয়ভাবে তৈরি মাস্ক ব্যবহার করে বস্তু সন্নিবেশ করতে, ImagenBackgroundMask উল্লেখ করুন। editImage() ব্যবহার করুন এবং সম্পাদনা কনফিগারেশনটি ImagenEditMode.INPAINT_INSERTION ব্যবহার করতে সেট করুন।

// 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 'originalImage' is a pre-loaded Bitmap.
// In a real app, this might come from the user's device or a URL.
Bitmap originalImage = null; // TODO("Load your image Bitmap here");

// Provide the prompt describing the content to be inserted.
String prompt = "a vase of flowers on the table";

// Define the list of sources for the editImage call.
// This includes the original image and the auto-generated mask.
ImagenRawImage rawOriginalImage =
    new ImagenRawImage(ImagenInlineImageKt.toImagenInlineImage(originalImage));
// Use ImagenBackgroundMask() to auto-generate the mask.
ImagenBackgroundMask rawMaskedImage = new ImagenBackgroundMask();

ImagenEditingConfig config = new ImagenEditingConfig();

// Use the editImage API to insert the new content.
// Pass the original image, the auto-generated masked image, the prompt, and an editing configuration.
Futures.addCallback(model.editImage(Arrays.asList(rawOriginalImage, rawMaskedImage), prompt, config),
    new FutureCallback<ImagenGenerationResponse>() {
        @Override
        public void onSuccess(ImagenGenerationResponse result) {
            if (result.getImages().isEmpty()) {
                Log.d("ImageEditor", "No images generated");
            }
            Bitmap editedImage = ((ImagenInlineImage) result.getImages().get(0)).asBitmap();
            // Process and use the bitmap to display the image in your UI
        }

        @Override
        public void onFailure(Throwable t) {
            // ...
        }
    }, Executors.newSingleThreadExecutor());

Web

ওয়েব অ্যাপের জন্য ইমেজেন মডেলের মাধ্যমে ছবি সম্পাদনা করা সমর্থিত নয়। এই বছরের শেষের দিকে আবার দেখুন!

Dart

স্বয়ংক্রিয়ভাবে তৈরি মাস্ক ব্যবহার করে বস্তু সন্নিবেশ করতে, ImagenBackgroundMask উল্লেখ করুন। editImage() ব্যবহার করুন এবং সম্পাদনা কনফিগারেশনটি ImagenEditMode.inpaintInsertion ব্যবহার করতে সেট করুন।

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 'originalImage' is a pre-loaded Uint8List.
// In a real app, this might come from the user's device or a URL.
final Uint8List originalImage = Uint8List(0); // TODO: Load your original image data here.

// Provide the prompt describing the content to be inserted.
final prompt = 'a vase of flowers on the table';

try {
  // Use the editImage API to insert the new content.
  // Pass the original image, the prompt, and an editing configuration.
  final response = await model.editImage(
    sources: [
      ImagenRawImage(originalImage),
      ImagenBackgroundMask(), // Use ImagenBackgroundMask() to auto-generate the mask.
    ],
    prompt: prompt,
    // Define the editing configuration for inpainting and insertion.
    config: const ImagenEditingConfig(
      editMode: ImagenEditMode.inpaintInsertion,
    ),
  );

  // 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');
}

ঐক্য

ইউনিটিতে ইমেজেন মডেলের মাধ্যমে ছবি সম্পাদনা করা যাবে না। এই বছরের শেষের দিকে আবার দেখুন!

প্রদত্ত মাস্ক ব্যবহার করে বস্তু ঢোকান

এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই নির্দেশিকার "শুরু করার আগে" বিভাগটি সম্পূর্ণ করুন।

নিচের নমুনাটি দেখায় কিভাবে ইনপেইন্টিং ব্যবহার করে একটি ছবিতে কন্টেন্ট সন্নিবেশ করাতে হয় — আপনার দেওয়া ছবিতে সংজ্ঞায়িত একটি মাস্ক ব্যবহার করে। আপনি মূল ছবি, একটি টেক্সট প্রম্পট এবং মাস্ক করা ছবি প্রদান করেন।

সুইফট

Swift-এর জন্য Imagen মডেলের মাধ্যমে ছবি সম্পাদনা সমর্থিত নয়। এই বছরের শেষের দিকে আবার দেখুন!

Kotlin

অবজেক্ট সন্নিবেশ করতে এবং আপনার নিজস্ব মাস্ক করা ছবি প্রদান করতে, মাস্ক করা ছবির সাথে ImagenRawMask নির্দিষ্ট করুন। editImage() ব্যবহার করুন এবং সম্পাদনা কনফিগারেশনটি ImagenEditMode.INPAINT_INSERTION ব্যবহার করতে সেট করুন।

// 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 'originalImage' is a pre-loaded Bitmap.
    // In a real app, this might come from the user's device or a URL.
    val originalImage: Bitmap = TODO("Load your original image Bitmap here")

    // This example assumes 'maskImage' is a pre-loaded Bitmap that contains the masked area.
    // In a real app, this might come from the user's device or a URL.
    val maskImage: Bitmap = TODO("Load your masked image Bitmap here")

    // Provide the prompt describing the content to be inserted.
    val prompt = "a vase of flowers on the table"

    // Use the editImage API to insert the new content.
    // Pass the original image, the masked image, the prompt, and an editing configuration.
    val editedImage = model.editImage(
        referenceImages = listOf(
            ImagenRawImage(originalImage.toImagenInlineImage()),
            ImagenRawMask(maskImage.toImagenInlineImage()), // Use ImagenRawMask() to provide your own masked image.
        ),
        prompt = prompt,
        // Define the editing configuration for inpainting and insertion.
        config = ImagenEditingConfig(ImagenEditMode.INPAINT_INSERTION)
    )

    // Process the resulting 'editedImage' Bitmap, for example, by displaying it in an ImageView.
}

Java

অবজেক্ট সন্নিবেশ করতে এবং আপনার নিজস্ব মাস্ক করা ছবি প্রদান করতে, মাস্ক করা ছবির সাথে ImagenRawMask নির্দিষ্ট করুন। editImage() ব্যবহার করুন এবং সম্পাদনা কনফিগারেশনটি ImagenEditMode.INPAINT_INSERTION ব্যবহার করতে সেট করুন।

// 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 'originalImage' is a pre-loaded Bitmap.
// In a real app, this might come from the user's device or a URL.
Bitmap originalImage = null; // TODO("Load your original image Bitmap here");

// This example assumes 'maskImage' is a pre-loaded Bitmap that contains the masked area.
// In a real app, this might come from the user's device or a URL.
Bitmap maskImage = null; // TODO("Load your masked image Bitmap here");

// Provide the prompt describing the content to be inserted.
String prompt = "a vase of flowers on the table";

// Define the list of source images for the editImage call.
ImagenRawImage rawOriginalImage =
    new ImagenRawImage(ImagenInlineImageKt.toImagenInlineImage(originalImage));
// Use ImagenRawMask() to provide your own masked image.
ImagenBackgroundMask rawMaskedImage =
    new ImagenRawMask(ImagenInlineImageKt.toImagenInlineImage(maskImage));

ImagenEditingConfig config = new ImagenEditingConfig();

// Use the editImage API to insert the new content.
// Pass the original image, the masked image, the prompt, and an editing configuration.
Futures.addCallback(model.editImage(Arrays.asList(rawOriginalImage, rawMaskedImage), prompt, config), new FutureCallback<ImagenGenerationResponse>() {
    @Override
    public void onSuccess(ImagenGenerationResponse result) {
        if (result.getImages().isEmpty()) {
            Log.d("ImageEditor", "No images generated");
        }
        Bitmap editedImage = ((ImagenInlineImage) result.getImages().get(0)).asBitmap();
        // Process and use the bitmap to display the image in your UI
    }

    @Override
    public void onFailure(Throwable t) {
        // ...
    }
}, Executors.newSingleThreadExecutor());

Web

ওয়েব অ্যাপের জন্য ইমেজেন মডেলের মাধ্যমে ছবি সম্পাদনা করা সমর্থিত নয়। এই বছরের শেষের দিকে আবার দেখুন!

Dart

অবজেক্ট সন্নিবেশ করতে এবং আপনার নিজস্ব মাস্ক করা ছবি প্রদান করতে, মাস্ক করা ছবির সাথে ImagenRawMask নির্দিষ্ট করুন। editImage() ব্যবহার করুন এবং সম্পাদনা কনফিগারেশনটি ImagenEditMode.inpaintInsertion ব্যবহার করতে সেট করুন।

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 'originalImage' is a pre-loaded Uint8List.
// In a real app, this might come from the user's device or a URL.
final Uint8List originalImage = Uint8List(0); // TODO: Load your original image data here.

// This example assumes 'maskImage' is a pre-loaded Uint8List that contains the masked area.
// In a real app, this might come from the user's device or a URL.
final Uint8List maskImage = Uint8List(0); // TODO: Load your masked image data here.

// Provide the prompt describing the content to be inserted.
final prompt = 'a vase of flowers on the table';

try {
  // Use the editImage API to insert the new content.
  // Pass the original image, the prompt, and an editing configuration.
  final response = await model.editImage(
    sources: [
      ImagenRawImage(originalImage),
      ImagenRawMask(maskImage), // Use ImagenRawMask() to provide your own masked image.
    ],
    prompt: prompt,
    // Define the editing configuration for inpainting and insertion.
    config: const ImagenEditingConfig(
      editMode: ImagenEditMode.inpaintInsertion,
    ),
  );

  // 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');
}

ঐক্য

ইউনিটিতে ইমেজেন মডেলের মাধ্যমে ছবি সম্পাদনা করা যাবে না। এই বছরের শেষের দিকে আবার দেখুন!

সর্বোত্তম অনুশীলন এবং সীমাবদ্ধতা

আমরা ছবি সম্পাদনা করার সময় মাস্কটি প্রসারিত করার পরামর্শ দিই। এটি সম্পাদনার সীমানা মসৃণ করতে এবং এটিকে আরও বিশ্বাসযোগ্য করে তুলতে সাহায্য করতে পারে। সাধারণত, 1% বা 2% ( 0.01 বা 0.02 ) এর প্রসারণ মান সুপারিশ করা হয়।


Firebase AI Logic এর সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন।