AutoML Vision Edge ব্যবহার করে আপনার নিজস্ব মডেল প্রশিক্ষণের পর, আপনি ছবি লেবেল করার জন্য এটি আপনার অ্যাপে ব্যবহার করতে পারেন।
AutoML Vision Edge থেকে প্রশিক্ষিত মডেলগুলিকে একীভূত করার দুটি উপায় রয়েছে। আপনি আপনার Xcode প্রকল্পে মডেলের ফাইলগুলি অনুলিপি করে মডেলটি বান্ডেল করতে পারেন, অথবা আপনি Firebase থেকে গতিশীলভাবে এটি ডাউনলোড করতে পারেন।
| মডেল বান্ডলিং বিকল্পগুলি | |
|---|---|
| আপনার অ্যাপে বান্ডেল করা |
|
| Firebase এর সাথে হোস্ট করা হয়েছে |
|
শুরু করার আগে
আপনার পডফাইলে ML কিট লাইব্রেরিগুলি অন্তর্ভুক্ত করুন:
আপনার অ্যাপের সাথে একটি মডেল বান্ডেল করার জন্য:
pod 'GoogleMLKit/ImageLabelingCustom'Firebase থেকে একটি মডেল গতিশীলভাবে ডাউনলোড করার জন্য,
LinkFirebaseনির্ভরতা যোগ করুন:pod 'GoogleMLKit/ImageLabelingCustom' pod 'GoogleMLKit/LinkFirebase'আপনার প্রোজেক্টের পড ইনস্টল বা আপডেট করার পর,
.xcworkspaceব্যবহার করে আপনার Xcode প্রোজেক্টটি খুলুন। ML Kit Xcode সংস্করণ 12.2 বা তার উচ্চতর সংস্করণে সমর্থিত।যদি আপনি একটি মডেল ডাউনলোড করতে চান , তাহলে আপনার অ্যান্ড্রয়েড প্রোজেক্টে Firebase যোগ করতে ভুলবেন না, যদি আপনি ইতিমধ্যেই তা না করে থাকেন। মডেলটি বান্ডেল করার সময় এটি প্রয়োজন হয় না।
1. মডেলটি লোড করুন
একটি স্থানীয় মডেল উৎস কনফিগার করুন
আপনার অ্যাপের সাথে মডেলটি বান্ডেল করতে:
Firebase কনসোল থেকে ডাউনলোড করা জিপ আর্কাইভ থেকে মডেল এবং এর মেটাডেটা একটি ফোল্ডারে বের করুন:
your_model_directory |____dict.txt |____manifest.json |____model.tfliteতিনটি ফাইলই একই ফোল্ডারে থাকতে হবে। আমরা আপনাকে ফাইলগুলি ডাউনলোড করার সময় ব্যবহার করার পরামর্শ দিচ্ছি, কোনও পরিবর্তন ছাড়াই (ফাইলের নাম সহ)।
আপনার Xcode প্রজেক্টে ফোল্ডারটি কপি করুন, যখন আপনি এটি করবেন তখন Create folder references নির্বাচন করতে ভুলবেন না। মডেল ফাইল এবং মেটাডেটা অ্যাপ বান্ডেলে অন্তর্ভুক্ত করা হবে এবং ML Kit-এ উপলব্ধ থাকবে।
মডেল ম্যানিফেস্ট ফাইলের পাথ নির্দিষ্ট করে
LocalModelঅবজেক্ট তৈরি করুন:সুইফট
guard let manifestPath = Bundle.main.path( forResource: "manifest", ofType: "json", inDirectory: "your_model_directory" ) else { return true } let localModel = LocalModel(manifestPath: manifestPath)অবজেক্টিভ-সি
NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest" ofType:@"json" inDirectory:@"your_model_directory"]; MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithManifestPath:manifestPath];
একটি Firebase-হোস্টেড মডেল সোর্স কনফিগার করুন
রিমোটলি-হোস্টেড মডেল ব্যবহার করতে, একটি CustomRemoteModel অবজেক্ট তৈরি করুন, যেখানে আপনি মডেলটি প্রকাশ করার সময় যে নামটি দিয়েছিলেন তা উল্লেখ করুন:
সুইফট
// Initialize the model source with the name you assigned in
// the Firebase console.
let remoteModelSource = FirebaseModelSource(name: "your_remote_model")
let remoteModel = CustomRemoteModel(remoteModelSource: remoteModelSource)
অবজেক্টিভ-সি
// Initialize the model source with the name you assigned in
// the Firebase console.
MLKFirebaseModelSource *firebaseModelSource =
[[MLKFirebaseModelSource alloc] initWithName:@"your_remote_model"];
MLKCustomRemoteModel *remoteModel =
[[MLKCustomRemoteModel alloc] initWithRemoteModelSource:firebaseModelSource];
তারপর, মডেল ডাউনলোড টাস্ক শুরু করুন, আপনি কোন শর্তাবলীর অধীনে ডাউনলোডের অনুমতি দিতে চান তা উল্লেখ করুন। যদি মডেলটি ডিভাইসে না থাকে, অথবা মডেলের একটি নতুন সংস্করণ উপলব্ধ থাকে, তাহলে টাস্কটি Firebase থেকে অ্যাসিঙ্ক্রোনাসভাবে মডেলটি ডাউনলোড করবে:
সুইফট
let downloadConditions = ModelDownloadConditions(
allowsCellularAccess: true,
allowsBackgroundDownloading: true
)
let downloadProgress = ModelManager.modelManager().download(
remoteModel,
conditions: downloadConditions
)
অবজেক্টিভ-সি
MLKModelDownloadConditions *downloadConditions =
[[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
allowsBackgroundDownloading:YES];
NSProgress *downloadProgress =
[[MLKModelManager modelManager] downloadRemoteModel:remoteModel
conditions:downloadConditions];
অনেক অ্যাপ তাদের ইনিশিয়ালাইজেশন কোডে ডাউনলোডের কাজ শুরু করে, কিন্তু মডেলটি ব্যবহার করার আগে আপনি যেকোনো সময় তা করতে পারেন।
আপনার মডেল থেকে একটি ইমেজ লেবেলার তৈরি করুন
আপনার মডেল সোর্সগুলি কনফিগার করার পরে, তাদের যেকোনো একটি থেকে একটি ImageLabeler অবজেক্ট তৈরি করুন।
যদি আপনার শুধুমাত্র স্থানীয়ভাবে বান্ডিল করা মডেল থাকে, তাহলে আপনার LocalModel অবজেক্ট থেকে একটি লেবেলার তৈরি করুন এবং আপনার প্রয়োজনীয় কনফিডেন্স স্কোর থ্রেশহোল্ড কনফিগার করুন ( আপনার মডেল মূল্যায়ন করুন দেখুন):
সুইফট
let options = CustomImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = NSNumber(value: 0.0) // Evaluate your model in the Cloud console
// to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options)
অবজেক্টিভ-সি
CustomImageLabelerOptions *options =
[[CustomImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = @(0.0f); // Evaluate your model in the Cloud console
// to determine an appropriate value.
MLKImageLabeler *imageLabeler =
[MLKImageLabeler imageLabelerWithOptions:options];
যদি আপনার একটি রিমোটলি-হোস্টেড মডেল থাকে, তাহলে এটি চালানোর আগে আপনাকে এটি ডাউনলোড করা হয়েছে কিনা তা পরীক্ষা করতে হবে। আপনি মডেল ম্যানেজারের isModelDownloaded(remoteModel:) পদ্ধতি ব্যবহার করে মডেল ডাউনলোড টাস্কের অবস্থা পরীক্ষা করতে পারেন।
যদিও লেবেলার চালানোর আগে আপনাকে কেবল এটি নিশ্চিত করতে হবে, যদি আপনার কাছে একটি রিমোটলি-হোস্টেড মডেল এবং একটি স্থানীয়ভাবে-বান্ডেলড মডেল উভয়ই থাকে, তাহলে ImageLabeler ইন্সট্যান্ট করার সময় এই পরীক্ষাটি করা যুক্তিসঙ্গত হতে পারে: যদি এটি ডাউনলোড করা থাকে তবে রিমোট মডেল থেকে একটি লেবেলার তৈরি করুন, এবং অন্যথায় স্থানীয় মডেল থেকে।
সুইফট
var options: CustomImageLabelerOptions
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
options = CustomImageLabelerOptions(remoteModel: remoteModel)
} else {
options = CustomImageLabelerOptions(localModel: localModel)
}
options.confidenceThreshold = NSNumber(value: 0.0) // Evaluate your model in the Firebase console
// to determine an appropriate value.
let imageLabeler = ImageLabeler.imageLabeler(options: options)
অবজেক্টিভ-সি
MLKCustomImageLabelerOptions *options;
if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) {
options = [[MLKCustomImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = @(0.0f); // Evaluate your model in the Firebase console
// to determine an appropriate value.
MLKImageLabeler *imageLabeler =
[MLKImageLabeler imageLabelerWithOptions:options];
যদি আপনার কেবল একটি রিমোটলি-হোস্টেড মডেল থাকে, তাহলে আপনার মডেল-সম্পর্কিত কার্যকারিতা অক্ষম করা উচিত—যেমন, ধূসর-আউট অথবা আপনার UI-এর কিছু অংশ লুকান—যতক্ষণ না আপনি নিশ্চিত হন যে মডেলটি ডাউনলোড হয়েছে।
ডিফল্ট নোটিফিকেশন সেন্টারে অবজারভার সংযুক্ত করে আপনি মডেল ডাউনলোড স্ট্যাটাস পেতে পারেন। অবজারভার ব্লকে self এর জন্য একটি দুর্বল রেফারেন্স ব্যবহার করতে ভুলবেন না, কারণ ডাউনলোডে কিছুটা সময় লাগতে পারে এবং ডাউনলোড শেষ হওয়ার সাথে সাথে অরিজিনিং অবজেক্টটি মুক্ত হয়ে যেতে পারে। উদাহরণস্বরূপ:
সুইফট
NotificationCenter.default.addObserver(
forName: .mlkitMLModelDownloadDidSucceed,
object: nil,
queue: nil
) { [weak self] notification in
guard let strongSelf = self,
let userInfo = notification.userInfo,
let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
as? RemoteModel,
model.name == "your_remote_model"
else { return }
// The model was downloaded and is available on the device
}
NotificationCenter.default.addObserver(
forName: .mlkitMLModelDownloadDidFail,
object: nil,
queue: nil
) { [weak self] notification in
guard let strongSelf = self,
let userInfo = notification.userInfo,
let model = userInfo[ModelDownloadUserInfoKey.remoteModel.rawValue]
as? RemoteModel
else { return }
let error = userInfo[ModelDownloadUserInfoKey.error.rawValue]
// ...
}
অবজেক্টিভ-সি
__weak typeof(self) weakSelf = self;
[NSNotificationCenter.defaultCenter
addObserverForName:MLKModelDownloadDidSucceedNotification
object:nil
queue:nil
usingBlock:^(NSNotification *_Nonnull note) {
if (weakSelf == nil | note.userInfo == nil) {
return;
}
__strong typeof(self) strongSelf = weakSelf;
MLKRemoteModel *model = note.userInfo[MLKModelDownloadUserInfoKeyRemoteModel];
if ([model.name isEqualToString:@"your_remote_model"]) {
// The model was downloaded and is available on the device
}
}];
[NSNotificationCenter.defaultCenter
addObserverForName:MLKModelDownloadDidFailNotification
object:nil
queue:nil
usingBlock:^(NSNotification *_Nonnull note) {
if (weakSelf == nil | note.userInfo == nil) {
return;
}
__strong typeof(self) strongSelf = weakSelf;
NSError *error = note.userInfo[MLKModelDownloadUserInfoKeyError];
}];
2. ইনপুট ইমেজ প্রস্তুত করুন
UIImage অথবা CMSampleBufferRef ব্যবহার করে একটি VisionImage অবজেক্ট তৈরি করুন।
আপনি যদি UIImage ব্যবহার করেন, তাহলে এই পদক্ষেপগুলি অনুসরণ করুন:
-
UIImageব্যবহার করে একটিVisionImageঅবজেক্ট তৈরি করুন। সঠিক.orientationউল্লেখ করতে ভুলবেন না।সুইফট
let image = VisionImage(image: uiImage) visionImage.orientation = image.imageOrientation
অবজেক্টিভ-সি
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
যদি আপনি CMSampleBufferRef ব্যবহার করেন, তাহলে এই পদক্ষেপগুলি অনুসরণ করুন:
CMSampleBufferRefবাফারে থাকা চিত্র ডেটার ওরিয়েন্টেশন নির্দিষ্ট করুন।ছবির ওরিয়েন্টেশন পেতে:
সুইফট
func imageOrientation( deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position ) -> UIImage.Orientation { switch deviceOrientation { case .portrait: return cameraPosition == .front ? .leftMirrored : .right case .landscapeLeft: return cameraPosition == .front ? .downMirrored : .up case .portraitUpsideDown: return cameraPosition == .front ? .rightMirrored : .left case .landscapeRight: return cameraPosition == .front ? .upMirrored : .down case .faceDown, .faceUp, .unknown: return .up } }
অবজেক্টিভ-সি
- (UIImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: return position == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored : UIImageOrientationRight; case UIDeviceOrientationLandscapeLeft: return position == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored : UIImageOrientationUp; case UIDeviceOrientationPortraitUpsideDown: return position == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored : UIImageOrientationLeft; case UIDeviceOrientationLandscapeRight: return position == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored : UIImageOrientationDown; case UIDeviceOrientationUnknown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: return UIImageOrientationUp; } }
-
CMSampleBufferRefঅবজেক্ট এবং ওরিয়েন্টেশন ব্যবহার করে একটিVisionImageঅবজেক্ট তৈরি করুন:সুইফট
let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
অবজেক্টিভ-সি
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
৩. ইমেজ লেবেলারটি চালান
অ্যাসিঙ্ক্রোনাসলি:
সুইফট
imageLabeler.process(image) { labels, error in
guard error == nil, let labels = labels, !labels.isEmpty else {
// Handle the error.
return
}
// Show results.
}
অবজেক্টিভ-সি
[imageLabeler
processImage:image
completion:^(NSArray<MLKImageLabel *> *_Nullable labels,
NSError *_Nullable error) {
if (label.count == 0) {
// Handle the error.
return;
}
// Show results.
}];
সমলয়ে:
সুইফট
var labels: [ImageLabel]
do {
labels = try imageLabeler.results(in: image)
} catch let error {
// Handle the error.
return
}
// Show results.
অবজেক্টিভ-সি
NSError *error;
NSArray<MLKImageLabel *> *labels =
[imageLabeler resultsInImage:image error:&error];
// Show results or handle the error.
৪. লেবেলযুক্ত বস্তু সম্পর্কে তথ্য পান
যদি ইমেজ লেবেলিং অপারেশন সফল হয়, তাহলে এটি ImageLabel এর একটি অ্যারে ফেরত পাঠায়। প্রতিটি ImageLabel এমন কিছু উপস্থাপন করে যা ছবিতে লেবেল করা হয়েছিল। আপনি প্রতিটি লেবেলের টেক্সট বর্ণনা (যদি TensorFlow Lite মডেল ফাইলের মেটাডেটাতে উপলব্ধ থাকে), আত্মবিশ্বাস স্কোর এবং সূচক পেতে পারেন। উদাহরণস্বরূপ:
সুইফট
for label in labels {
let labelText = label.text
let confidence = label.confidence
let index = label.index
}
অবজেক্টিভ-সি
for (MLKImageLabel *label in labels) {
NSString *labelText = label.text;
float confidence = label.confidence;
NSInteger index = label.index;
}
রিয়েল-টাইম পারফর্ম্যান্স উন্নত করার টিপস
আপনি যদি রিয়েল-টাইম অ্যাপ্লিকেশনে ছবি লেবেল করতে চান, তাহলে সেরা ফ্রেমরেট অর্জনের জন্য এই নির্দেশিকাগুলি অনুসরণ করুন:
- থ্রটল ডিটেক্টরে কল করে। ডিটেক্টর চলাকালীন যদি একটি নতুন ভিডিও ফ্রেম পাওয়া যায়, তাহলে ফ্রেমটি ফেলে দিন।
- যদি আপনি ইনপুট ছবিতে গ্রাফিক্স ওভারলে করার জন্য ডিটেক্টরের আউটপুট ব্যবহার করেন, তাহলে প্রথমে ফলাফলটি পান, তারপর ছবিটি রেন্ডার করুন এবং এক ধাপে ওভারলে করুন। এটি করার মাধ্যমে, আপনি প্রতিটি ইনপুট ফ্রেমের জন্য শুধুমাত্র একবার ডিসপ্লে সারফেসে রেন্ডার করবেন। উদাহরণের জন্য শোকেস নমুনা অ্যাপে previewOverlayView এবং FIRDetectionOverlayView ক্লাসগুলি দেখুন।