এই পৃষ্ঠায় বর্ণনা করা হয়েছে কিভাবে Firebase AI Logic SDK ব্যবহার করে Imagen-এর মাধ্যমে ইনপেইন্টিং ব্যবহার করে কোনো ছবির মধ্যে একটি অবজেক্ট যুক্ত করা যায় ।
ইনপেইন্টিং হলো এক ধরনের মাস্ক-ভিত্তিক সম্পাদনা । মাস্ক হলো একটি ডিজিটাল ওভারলে যা আপনি সম্পাদনা করতে চান এমন নির্দিষ্ট এলাকাকে সংজ্ঞায়িত করে।
এটি যেভাবে কাজ করে : আপনি একটি মূল ছবি এবং তার সাথে সঙ্গতিপূর্ণ একটি মাস্ক করা ছবি প্রদান করেন — যা স্বয়ংক্রিয়ভাবে তৈরি হতে পারে অথবা আপনার দেওয়া হতে পারে — এবং এটি এমন একটি এলাকার উপর মাস্ক তৈরি করে যেখানে আপনি নতুন কন্টেন্ট যোগ করতে চান। আপনি কী যোগ করতে চান তা বর্ণনা করে একটি টেক্সট প্রম্পটও প্রদান করেন। এরপর মডেলটি মাস্ক করা এলাকার মধ্যে নতুন কন্টেন্ট তৈরি করে এবং যোগ করে দেয়।
উদাহরণস্বরূপ, আপনি একটি টেবিল মাস্ক করে মডেলকে একটি ফুলদানি যোগ করতে বলতে পারেন।
স্বয়ংক্রিয়ভাবে তৈরি মাস্কের জন্য কোডে যান মাস্ক প্রদানের জন্য কোডে যান
শুরু করার আগে
| শুধুমাত্র তখনই উপলব্ধ যখন আপনার এপিআই প্রদানকারী হিসেবে ভার্টেক্স এআই জেমিনি এপিআই ব্যবহার করা হয়। |
যদি আপনি ইতিমধ্যে তা না করে থাকেন, তাহলে ‘ গেটিং স্টার্টেড গাইড’টি সম্পূর্ণ করুন, যেখানে আপনার 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 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
Imagen মডেল ব্যবহার করে ছবি সম্পাদনা ওয়েব অ্যাপের জন্য সমর্থিত নয়। এই বছরের শেষের দিকে আবার দেখুন!
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
Imagen মডেল ব্যবহার করে ছবি সম্পাদনা ওয়েব অ্যাপের জন্য সমর্থিত নয়। এই বছরের শেষের দিকে আবার দেখুন!
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');
}
ঐক্য
ইউনিটিতে ইমেজেন মডেল ব্যবহার করে ইমেজ এডিটিং সমর্থিত নয়। এই বছরের শেষের দিকে আবার চেষ্টা করুন!
সর্বোত্তম অনুশীলন এবং সীমাবদ্ধতা
ছবি সম্পাদনা করার সময় আমরা মাস্কটি ডাইলেট করার পরামর্শ দিই। এটি সম্পাদনার সীমানা মসৃণ করতে এবং এটিকে আরও বিশ্বাসযোগ্য করে তুলতে সাহায্য করতে পারে। সাধারণত, ১% বা ২% ( 0.01 বা 0.02 ) ডাইলেশন মান সুপারিশ করা হয়।
Firebase AI Logic ব্যবহারের অভিজ্ঞতা সম্পর্কে মতামত দিন।