Firebase AI Logic SDKs ব্যবহার করে একটি ছবি থেকে একটি বস্তু সরাতে Imagen ব্যবহার করে কিভাবে ইনপেইন্টিং ব্যবহার করতে হয় এই পৃষ্ঠাটি বর্ণনা করে।
ইনপেইন্টিং হল এক ধরনের মুখোশ-ভিত্তিক সম্পাদনা । একটি মুখোশ হল একটি ডিজিটাল ওভারলে যা আপনি যে নির্দিষ্ট এলাকা সম্পাদনা করতে চান তা সংজ্ঞায়িত করে।
এটি কীভাবে কাজ করে : আপনি একটি আসল চিত্র এবং একটি সংশ্লিষ্ট মুখোশযুক্ত চিত্র প্রদান করেন — হয় স্বয়ংক্রিয়ভাবে তৈরি বা আপনার দ্বারা সরবরাহ করা হয় — যা আপনি যে বস্তু বা বিষয়কে সরাতে চান তার উপর একটি মুখোশ সংজ্ঞায়িত করে৷ আপনি কি অপসারণ করতে চান তা বর্ণনা করে আপনি ঐচ্ছিকভাবে একটি পাঠ্য প্রম্পট প্রদান করতে পারেন, অথবা মডেলটি বুদ্ধিমত্তার সাথে সনাক্ত করতে পারে কোন বস্তুটি সরাতে হবে। মডেলটি তারপর বস্তুটি সরিয়ে দেয় এবং নতুন, প্রাসঙ্গিকভাবে উপযুক্ত সামগ্রী দিয়ে এলাকাটি পূরণ করে।
উদাহরণস্বরূপ, আপনি একটি বল মাস্ক করতে পারেন এবং এটি একটি ফাঁকা প্রাচীর বা ঘাসযুক্ত ক্ষেত্র দিয়ে প্রতিস্থাপন করতে পারেন।
স্বয়ংক্রিয়ভাবে তৈরি মাস্কের জন্য কোডে যান মাস্ক প্রদানের জন্য কোডে যান
আপনি শুরু করার আগে
আপনার API প্রদানকারী হিসাবে Vertex AI Gemini API ব্যবহার করার সময় শুধুমাত্র উপলব্ধ। |
যদি আপনি ইতিমধ্যেই না করে থাকেন, শুরু করার নির্দেশিকাটি সম্পূর্ণ করুন, যা বর্ণনা করে যে কীভাবে আপনার Firebase প্রকল্প সেট আপ করবেন, আপনার অ্যাপকে Firebase-এর সাথে সংযুক্ত করবেন, SDK যোগ করবেন, আপনার নির্বাচিত API প্রদানকারীর জন্য ব্যাকএন্ড পরিষেবা শুরু করবেন এবং একটি ImagenModel
উদাহরণ তৈরি করবেন।
মডেল যে এই ক্ষমতা সমর্থন করে
ইমেজেন তার capability
মডেলের মাধ্যমে চিত্র সম্পাদনা অফার করে:
-
imagen-3.0-capability-001
মনে রাখবেন যে Imagen মডেলের জন্য, global
অবস্থান সমর্থিত নয় ।
একটি স্বয়ংক্রিয় তৈরি মাস্ক ব্যবহার করে বস্তু সরান
এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন। |
নিচের নমুনাটি দেখায় কিভাবে একটি ছবি থেকে বিষয়বস্তু অপসারণ করতে ইনপেইন্টিং ব্যবহার করতে হয় — স্বয়ংক্রিয় মাস্ক জেনারেশন ব্যবহার করে। আপনি আসল চিত্র এবং একটি পাঠ্য প্রম্পট প্রদান করেন এবং ইমেজেন স্বয়ংক্রিয়ভাবে সনাক্ত করে এবং মূল চিত্রটি পরিবর্তন করার জন্য একটি মাস্ক এলাকা তৈরি করে।
সুইফট
ইমেজেন মডেলের সাথে ছবি সম্পাদনা সুইফটের জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
Kotlin
একটি স্বয়ংক্রিয়-উত্পাদিত মুখোশ দিয়ে বস্তুগুলি সরাতে, ImagenBackgroundMask
নির্দিষ্ট করুন। editImage()
ব্যবহার করুন এবং ImagenEditMode.INPAINT_REMOVAL
ব্যবহার করতে সম্পাদনা কনফিগারেশন সেট করুন।
// 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 removed.
val prompt = "ball"
// Use the editImage API to remove the unwanted content.
// Pass the original image, the prompt, and an editing configuration.
val editedImage = model.editImage(
sources = listOf(
ImagenRawImage(originalImage),
ImagenBackgroundMask(), // Use ImagenBackgroundMask() to auto-generate the mask.
),
prompt = prompt,
// Define the editing configuration for inpainting and insertion.
config = ImagenEditingConfig(ImagenEditMode.INPAINT_REMOVAL)
)
// Process the resulting 'editedImage' Bitmap, for example, by displaying it in an ImageView.
}
Java
একটি স্বয়ংক্রিয়-উত্পাদিত মুখোশ দিয়ে বস্তুগুলি সরাতে, ImagenBackgroundMask
নির্দিষ্ট করুন। editImage()
ব্যবহার করুন এবং ImagenEditMode.INPAINT_REMOVAL
ব্যবহার করতে সম্পাদনা কনফিগারেশন সেট করুন।
// 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 removed.
String prompt = "ball";
// Define the list of sources for the editImage call.
// This includes the original image and the auto-generated mask.
ImagenRawImage rawOriginalImage = new ImagenRawImage(originalImage);
ImagenBackgroundMask rawMaskedImage = new ImagenBackgroundMask(); // Use ImagenBackgroundMask() to auto-generate the mask.
// Define the editing configuration for inpainting and removal.
ImagenEditingConfig config = new ImagenEditingConfig.Builder()
.setEditMode(ImagenEditMode.INPAINT_REMOVAL)
.build();
// Use the editImage API to remove the unwanted 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 = 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.inpaintRemoval
ব্যবহার করতে সম্পাদনা কনফিগার সেট করুন।
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');
TODO - FLUTTER// 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 removed.
final prompt = 'ball';
try {
// Use the editImage API to remove the unwanted 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,
// Define the editing configuration for inpainting and removal.
config: const ImagenEditingConfig(
editMode: ImagenEditMode.inpaintRemoval,
),
);
// 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');
}
ঐক্য
ইমেজেন মডেলের সাথে ইমেজ এডিটিং ইউনিটির জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
একটি প্রদত্ত মাস্ক ব্যবহার করে বস্তুগুলি সরান
এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন। |
নীচের নমুনাটি দেখায় যে কীভাবে একটি চিত্র থেকে বিষয়বস্তু সরাতে ইনপেইন্টিং ব্যবহার করতে হয় — আপনার দেওয়া ছবিতে সংজ্ঞায়িত একটি মাস্ক ব্যবহার করে৷ আপনি মূল চিত্র, একটি পাঠ্য প্রম্পট এবং মুখোশযুক্ত চিত্র প্রদান করেন।
আপনি যদি একটি মুখোশযুক্ত চিত্র প্রদান করেন তবে একটি পাঠ্য প্রম্পট প্রদান করা ঐচ্ছিক। ইমেজেন মুখোশযুক্ত এলাকা থেকে সরানোর জন্য একটি বস্তুকে বুদ্ধিমত্তার সাথে সনাক্ত করতে পারে। যাইহোক, যদি আপনি যে বস্তুটি সরাতে চান তা সুস্পষ্ট না হয় বা আপনি শুধুমাত্র মুখোশযুক্ত এলাকায় নির্দিষ্ট বস্তুগুলি সরাতে চান, তাহলে মডেলটিকে সঠিক বস্তুটি সরাতে সাহায্য করার জন্য একটি পাঠ্য প্রম্পট প্রদান করুন।
সুইফট
ইমেজেন মডেলের সাথে ছবি সম্পাদনা সুইফটের জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
Kotlin
বস্তুগুলি সরাতে এবং আপনার নিজের মুখোশযুক্ত চিত্র সরবরাহ করতে, মুখোশযুক্ত চিত্রের সাথে ImagenRawMask
নির্দিষ্ট করুন। editImage()
ব্যবহার করুন এবং ImagenEditMode.INPAINT_REMOVAL
ব্যবহার করতে সম্পাদনা কনফিগারেশন সেট করুন।
// 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 removed.
val prompt = "ball"
// Use the editImage API to remove the unwanted 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 removal.
config = ImagenEditingConfig(ImagenEditMode.INPAINT_REMOVAL)
)
// Process the resulting 'editedImage' Bitmap, for example, by displaying it in an ImageView.
}
Java
বস্তুগুলি সরাতে এবং আপনার নিজের মুখোশযুক্ত চিত্র সরবরাহ করতে, মুখোশযুক্ত চিত্রের সাথে ImagenRawMask
নির্দিষ্ট করুন। editImage()
ব্যবহার করুন এবং ImagenEditMode.INPAINT_REMOVAL
ব্যবহার করতে সম্পাদনা কনফিগারেশন সেট করুন।
// 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 removed.
String prompt = "ball";
// Define the list of source images for the editImage call.
ImagenRawImage rawOriginalImage = new ImagenRawImage(originalImage);
ImagenBackgroundMask rawMaskedImage = new ImagenRawMask(maskImage); // Use ImagenRawMask() to provide your own masked image.
// Define the editing configuration for inpainting and removal.
ImagenEditingConfig config = new ImagenEditingConfig.Builder()
.setEditMode(ImagenEditMode.INPAINT_REMOVAL)
.build();
// Use the editImage API to remove the unwanted 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 = 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.inpaintRemoval
ব্যবহার করতে সম্পাদনা কনফিগার সেট করুন।
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 removed.
final prompt = 'ball';
try {
// Use the editImage API to remove the unwanted 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 removal.
config: const ImagenEditingConfig(
editMode: ImagenEditMode.inpaintRemoval,
),
);
// 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 লজিকের সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন