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_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(
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_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(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 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 = 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');
}
ঐক্য
ইমেজেন মডেলের সাথে ইমেজ এডিটিং ইউনিটির জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
প্রদত্ত মাস্ক ব্যবহার করে বস্তু সন্নিবেশ করান
এই নমুনাটি চেষ্টা করার আগে, আপনার প্রকল্প এবং অ্যাপ সেট আপ করতে এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন। |
নিচের নমুনাটি দেখায় কিভাবে একটি ছবিতে বিষয়বস্তু সন্নিবেশ করতে ইনপেইন্টিং ব্যবহার করতে হয় — আপনার দেওয়া ছবিতে সংজ্ঞায়িত একটি মাস্ক ব্যবহার করে। আপনি মূল চিত্র, একটি পাঠ্য প্রম্পট এবং মুখোশযুক্ত চিত্র প্রদান করেন।
সুইফট
ইমেজেন মডেলের সাথে ছবি সম্পাদনা সুইফটের জন্য সমর্থিত নয়। এই বছরের পরে আবার চেক করুন!
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(originalImage);
ImagenBackgroundMask rawMaskedImage = new ImagenRawMask(maskImage); // Use ImagenRawMask() to provide your own masked image.
// Define the editing configuration for inpainting and insertion.
ImagenEditingConfig config = new ImagenEditingConfig.Builder()
.setEditMode(ImagenEditMode.INPAINT_INSERTION)
.build();
// 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 = 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 লজিকের সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন