ফায়ারবেস এআই লজিক এসডিকে ব্যবহার করে একটি ছবির ব্যাকগ্রাউন্ড প্রতিস্থাপন করতে ইমেজেন কীভাবে ব্যবহার করবেন তা এই পৃষ্ঠায় বর্ণনা করা হয়েছে।
পটভূমি প্রতিস্থাপন হল এক ধরনের মুখোশ-ভিত্তিক সম্পাদনা (বিশেষত পেইন্টিং )। একটি মুখোশ হল একটি ডিজিটাল ওভারলে যা আপনি যে নির্দিষ্ট এলাকা সম্পাদনা করতে চান তা সংজ্ঞায়িত করে।
এটি কীভাবে কাজ করে : আপনি একটি আসল চিত্র এবং একটি সংশ্লিষ্ট মুখোশযুক্ত চিত্র প্রদান করেন যা পটভূমিতে একটি মুখোশ সংজ্ঞায়িত করে — হয় স্বয়ংক্রিয় পটভূমি সনাক্তকরণ ব্যবহার করে বা নিজেই ব্যাকগ্রাউন্ডের মাস্ক প্রদান করে৷ আপনি কি পরিবর্তন করতে চান তা বর্ণনা করে আপনি একটি পাঠ্য প্রম্পটও প্রদান করেন। মডেলটি তারপর একটি নতুন পটভূমি তৈরি করে এবং প্রয়োগ করে।
উদাহরণস্বরূপ, আপনি ফোরগ্রাউন্ডকে প্রভাবিত না করেই একটি বিষয় বা বস্তুর চারপাশের সেটিং পরিবর্তন করতে পারেন (উদাহরণস্বরূপ, একটি পণ্যের ছবিতে)।
স্বয়ংক্রিয়ভাবে সনাক্ত করা ব্যাকগ্রাউন্ডের জন্য কোডে যান ব্যাকগ্রাউন্ড মাস্ক প্রদানের জন্য কোডে যান
আপনি শুরু করার আগে
আপনার API প্রদানকারী হিসাবে Vertex AI Gemini API ব্যবহার করার সময় শুধুমাত্র উপলব্ধ। |
যদি আপনি ইতিমধ্যেই না করে থাকেন, শুরু করার নির্দেশিকাটি সম্পূর্ণ করুন, যা বর্ণনা করে যে কীভাবে আপনার Firebase প্রকল্প সেট আপ করবেন, আপনার অ্যাপকে Firebase-এর সাথে সংযুক্ত করবেন, SDK যোগ করবেন, আপনার নির্বাচিত API প্রদানকারীর জন্য ব্যাকএন্ড পরিষেবা শুরু করবেন এবং একটি ImagenModel
উদাহরণ তৈরি করবেন।
মডেল যে এই ক্ষমতা সমর্থন করে
ইমেজেন তার capability
মডেলের মাধ্যমে চিত্র সম্পাদনা অফার করে:
-
imagen-3.0-capability-001
মনে রাখবেন যে Imagen মডেলের জন্য, global
অবস্থান সমর্থিত নয় ।
স্বয়ংক্রিয় পটভূমি সনাক্তকরণ ব্যবহার করে পটভূমি প্রতিস্থাপন করুন
এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন। |
স্বয়ংক্রিয় পটভূমি সনাক্তকরণ ব্যবহার করে - নিম্নলিখিত নমুনাটি কীভাবে একটি চিত্রের পটভূমি প্রতিস্থাপন করতে হয় তা দেখায়। আপনি মূল চিত্র এবং একটি পাঠ্য প্রম্পট প্রদান করেন এবং ইমেজেন স্বয়ংক্রিয়ভাবে সনাক্ত করে এবং মূল চিত্রটি পরিবর্তন করতে পটভূমির একটি মুখোশ তৈরি করে।
সুইফট
ইমেজেন মডেলের সাথে ছবি সম্পাদনা সুইফটের জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
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(
sources = listOf(
ImagenRawImage(originalImage),
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(originalImage);
ImagenBackgroundMask rawMaskedImage = new ImagenBackgroundMask(); // Use ImagenBackgroundMask() to auto-generate the mask.
// Define the editing configuration for inpainting and insertion.
ImagenEditingConfig config = new ImagenEditingConfig.Builder()
.setEditMode(ImagenEditMode.INPAINT_INSERTION)
.build();
// 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 = 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');
}
ঐক্য
ইমেজেন মডেলের সাথে ইমেজ এডিটিং ইউনিটির জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
প্রদত্ত মাস্ক ব্যবহার করে পটভূমি প্রতিস্থাপন করুন
এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন। |
নিচের নমুনাটি দেখায় কিভাবে একটি ছবির ব্যাকগ্রাউন্ড প্রতিস্থাপন করতে হয় — আপনার দেওয়া ছবিতে সংজ্ঞায়িত একটি ব্যাকগ্রাউন্ড মাস্ক ব্যবহার করে। আপনি মূল চিত্র, একটি পাঠ্য প্রম্পট এবং মুখোশযুক্ত চিত্র প্রদান করেন।
সুইফট
ইমেজেন মডেলের সাথে ছবি সম্পাদনা সুইফটের জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
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(originalImage);
ImagenBackgroundMask rawMaskedImage = new ImagenRawMask(maskImage); // Use ImagenRawMask() to provide your own masked image.
// Define the editing configuration for inpainting and background replacement.
ImagenEditingConfig config = new ImagenEditingConfig.Builder()
.setEditMode(ImagenEditMode.INPAINT_INSERTION)
.build();
// 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 = 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 লজিকের সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন