Imagen ব্যবহার করে একটি ছবির ব্যাকগ্রাউন্ড প্রতিস্থাপন করুন


এই পৃষ্ঠায় বর্ণনা করা হয়েছে কিভাবে Firebase AI Logic SDK ব্যবহার করে ছবির ব্যাকগ্রাউন্ড প্রতিস্থাপন করতে Imagen ব্যবহার করতে হয়।

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

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

উদাহরণস্বরূপ, আপনি কোনও বিষয় বা বস্তুর চারপাশের সেটিং পরিবর্তন করতে পারেন, ফোরগ্রাউন্ডকে প্রভাবিত না করেই (উদাহরণস্বরূপ, কোনও পণ্যের ছবিতে)।

স্বয়ংক্রিয়ভাবে সনাক্ত হওয়া ব্যাকগ্রাউন্ডের জন্য কোডে যান ব্যাকগ্রাউন্ড মাস্ক প্রদানের জন্য কোডে যান

শুরু করার আগে

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

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

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

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

  • imagen-3.0-capability-001

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

স্বয়ংক্রিয় ব্যাকগ্রাউন্ড সনাক্তকরণ ব্যবহার করে ব্যাকগ্রাউন্ড প্রতিস্থাপন করুন

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

নিম্নলিখিত নমুনাটি দেখায় কিভাবে একটি ছবির ব্যাকগ্রাউন্ড প্রতিস্থাপন করতে হয় — স্বয়ংক্রিয় ব্যাকগ্রাউন্ড সনাক্তকরণ ব্যবহার করে। আপনি মূল ছবি এবং একটি টেক্সট প্রম্পট প্রদান করেন, এবং Imagen স্বয়ংক্রিয়ভাবে সনাক্ত করে এবং মূল ছবি পরিবর্তন করার জন্য ব্যাকগ্রাউন্ডের একটি মাস্ক তৈরি করে।

সুইফট

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 new background.
    val prompt = "space background"

    // Use the editImage API to replace the background.
    // 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 background replacement.
        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 new background.
String prompt = "space background";

// 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 replace the background.
// 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 new background.
final prompt = 'space background';

try {
  // Use the editImage API to replace the background.
  // 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 background replacement.
    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 new background.
    val prompt = "space background"

    // Use the editImage API to replace the background.
    // 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 background replacement.
        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 new background.
String prompt = "space background";

// 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 replace the background.
// 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.INPAINT_INSERTION ব্যবহার করে সেট করুন।

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 new background.
final prompt = 'space background';

try {
  // Use the editImage API to replace the background.
  // 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 background replacement.
    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 এর সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন।