iOS पर, AutoML की मदद से बनाए गए मॉडल की मदद से इमेज को लेबल करना
AutoML Vision Edge का इस्तेमाल करके अपने खुद के मॉडल को ट्रेनिंग देने के बाद, इसे अपने ऐप्लिकेशन में इमेज को लेबल करने के लिए इस्तेमाल किया जा सकता है.
AutoML Vision Edge से ट्रेन किए गए मॉडल को इंटिग्रेट करने के दो तरीके हैं. मॉडल की फ़ाइलों को अपने Xcode प्रोजेक्ट में कॉपी करके, मॉडल को बंडल किया जा सकता है. इसके अलावा, आपके पास Firebase से इसे डाइनैमिक तौर पर डाउनलोड करने का भी विकल्प होता है.
मॉडल बंडलिंग विकल्प | |
---|---|
आपके ऐप्लिकेशन में बंडल किए गए |
|
Firebase की मदद से होस्ट किया गया |
|
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, ऐप्लिकेशन के सैंपल को आज़माएं.
शुरू करने से पहले
1. अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:किसी मॉडल को अपने ऐप्लिकेशन के साथ बंडल करने के लिए:
pod 'GoogleMLKit/ImageLabelingAutoML'Firebase से किसी मॉडल को डाइनैमिक तरीके से डाउनलोड करने के लिए,
LinkFirebase
डिपेंडेंसी जोड़ें:
pod 'GoogleMLKit/ImageLabelingAutoML' pod 'GoogleMLKit/LinkFirebase'2. अपने प्रोजेक्ट के पॉड इंस्टॉल या अपडेट करने के बाद, Xcode प्रोजेक्ट के
.xcworkspace
code> का इस्तेमाल करें. ML किट, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करती है.
3. अगर आपको कोई मॉडल डाउनलोड करना है, तो पक्का करें कि आपने
अपने iOS प्रोजेक्ट में Firebase को जोड़ा हो. ऐसा तब करें,
जब आपने पहले से ऐसा न किया हो. मॉडल को बंडल करते समय,
ऐसा करना ज़रूरी नहीं है.
1. मॉडल लोड करें
लोकल मॉडल का सोर्स कॉन्फ़िगर करें
मॉडल को अपने ऐप्लिकेशन से जोड़ने के लिए:1. Firebase कंसोल से डाउनलोड किए गए ZIP संग्रह से, मॉडल और उसका मेटाडेटा एक फ़ोल्डर में निकालें:
your_model_directory |____dict.txt |____manifest.json |____model.tfliteतीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए. हमारा सुझाव है कि आप फ़ाइलों में बदलाव किए बिना (जिनमें फ़ाइलों के नाम भी शामिल हैं), उन्हें डाउनलोड करते ही इस्तेमाल करें.
2. फ़ोल्डर को अपने Xcode प्रोजेक्ट में कॉपी करें. ऐसा करते समय, फ़ोल्डर के रेफ़रंस बनाएं विकल्प को चुनें. मॉडल फ़ाइल और मेटाडेटा, ऐप्लिकेशन बंडल में शामिल किए जाएंगे और ये एमएल किट में उपलब्ध होंगे.
3. मॉडल मेनिफ़ेस्ट फ़ाइल का पाथ तय करते हुए,
AutoMLImageLabelerLocalModel
ऑब्जेक्ट बनाएं:
Swift
guard let manifestPath = Bundle.main.path( forResource: "manifest", ofType: "json", inDirectory: "your_model_directory" ) else { return } let localModel = AutoMLImageLabelerLocalModel(manifestPath: manifestPath)
Objective-C
NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest" ofType:@"json" inDirectory:@"your_model_directory"]; MLKAutoMLImageLabelerLocalModel *localModel = [[MLKAutoMLImageLabelerLocalModel alloc] initWithManifestPath:manifestPath];
Firebase से होस्ट किए गए मॉडल सोर्स को कॉन्फ़िगर करें
रिमोट तरीके से होस्ट किए गए मॉडल का इस्तेमाल करने के लिए, AutoMLImageLabelerRemoteModel
ऑब्जेक्ट बनाएं. इसमें वह नाम बताएं जिसे आपने मॉडल को पब्लिश करते समय असाइन किया था:
Swift
let remoteModel = AutoMLImageLabelerRemoteModel( name: "your_remote_model" // The name you assigned in // the Firebase console. )
Objective-C
MLKAutoMLImageLabelerRemoteModel *remoteModel = [[MLKAutoMLImageLabelerRemoteModel alloc] initWithName:@"your_remote_model"]; // The name you assigned in // the Firebase console.
इसके बाद, उन शर्तों को तय करते हुए मॉडल डाउनलोड टास्क शुरू करें जिनके तहत आपको डाउनलोड करने की अनुमति देनी है. अगर मॉडल डिवाइस पर मौजूद नहीं है या मॉडल का कोई नया वर्शन उपलब्ध है, तो यह टास्क Firebase से मॉडल को एसिंक्रोनस रूप से डाउनलोड कर देगा:
Swift
let downloadConditions = ModelDownloadConditions( allowsCellularAccess: true, allowsBackgroundDownloading: true ) let downloadProgress = ModelManager.modelManager().download( remoteModel, conditions: downloadConditions )
Objective-C
MLKModelDownloadConditions *downloadConditions = [[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES allowsBackgroundDownloading:YES]; NSProgress *downloadProgress = [[MLKModelManager modelManager] downloadModel:remoteModel conditions:downloadConditions];
कई ऐप्लिकेशन अपने इनिशलाइज़ेशन कोड में डाउनलोड टास्क शुरू करते हैं. हालांकि, मॉडल का इस्तेमाल करने से पहले, किसी भी समय ऐसा किया जा सकता है.
अपने मॉडल से इमेज लेबलर बनाएं
मॉडल सोर्स को कॉन्फ़िगर करने के बाद, उनमें से किसी एक से ImageLabeler
ऑब्जेक्ट बनाएं.
अगर आपके पास सिर्फ़ स्थानीय तौर पर बंडल किया गया मॉडल है, तो अपने AutoMLImageLabelerLocalModel
ऑब्जेक्ट से लेबलर बनाएं. इसके बाद, अपनी पसंद के मुताबिक कॉन्फ़िडेंस स्कोर की सीमा कॉन्फ़िगर करें (अपने मोड का आकलन करना देखें:
Swift
let options = AutoMLImageLabelerOptions(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)
Objective-C
MLKAutoMLImageLabelerOptions *options = [[MLKAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel]; options.confidenceThreshold = @(0.0); // Evaluate your model in the Firebase console // to determine an appropriate value. MLKImageLabeler *imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
अगर आपके पास रिमोट तरीके से होस्ट किया गया मॉडल है, तो उसे चलाने से पहले आपको यह देखना होगा कि
उसे डाउनलोड किया गया है या नहीं. मॉडल मैनेजर के isModelDownloaded
(remoteModel:) तरीके का इस्तेमाल करके, मॉडल डाउनलोड टास्क
की स्थिति देखी जा सकती है.
हालांकि, आपको लेबलर का इस्तेमाल करने से पहले ही इसकी पुष्टि करनी होती है. अगर आपके पास
कहीं से होस्ट किया गया मॉडल और स्थानीय रूप से बंडल किया गया मॉडल, दोनों हैं, तो ImageLabeler
को इंस्टैंशिएट करते समय,
इस जांच को करने में मदद मिल सकती है: अगर रिमोट मॉडल
डाउनलोड किया गया है, तो उससे लेबलर बनाएं. ऐसा न होने पर, लोकल मॉडल से लेबल बनाएं.
Swift
var options: AutoMLImageLabelerOptions! if (ModelManager.modelManager().isModelDownloaded(remoteModel)) { options = AutoMLImageLabelerOptions(remoteModel: remoteModel) } else { options = AutoMLImageLabelerOptions(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)
Objective-C
MLKAutoMLImageLabelerOptions *options; if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) { options = [[MLKAutoMLImageLabelerOptions alloc] initWithRemoteModel:remoteModel]; } else { options = [[MLKAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel]; } options.confidenceThreshold = @(0.0); // Evaluate your model in the Firebase console // to determine an appropriate value. MLKImageLabeler *imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
अगर आपके पास सिर्फ़ रिमोट तरीके से होस्ट किया गया मॉडल है, तो आपको मॉडल से जुड़ी सुविधा बंद करनी चाहिए. उदाहरण के लिए, अपने यूज़र इंटरफ़ेस के हिस्से को धूसर करें या छिपाएं. ऐसा तब तक होगा, जब तक मॉडल के डाउनलोड होने की पुष्टि न हो जाए.
मॉडल डाउनलोड की स्थिति जानने के लिए, ऑब्ज़र्वर को डिफ़ॉल्ट सूचना केंद्र में अटैच करें. ऑब्ज़र्वर ब्लॉक में self
के लिए कमज़ोर रेफ़रंस का इस्तेमाल करें. ऐसा इसलिए, क्योंकि डाउनलोड होने में कुछ समय लग सकता है और डाउनलोड पूरा होने तक मूल ऑब्जेक्ट को फ़्री किया जा सकता है. उदाहरण के लिए:
Swift
NotificationCenter.default.addObserver( forName: .mlkitModelDownloadDidSucceed, 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: .mlkitModelDownloadDidFail, 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] // ... }
Objective-C
__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
या
CMSampleBuffer
का इस्तेमाल करके, VisionImage
ऑब्जेक्ट बनाएं.
अगर UIImage
का इस्तेमाल किया जाता है, तो यह तरीका अपनाएं:
UIImage
की मदद सेVisionImage
ऑब्जेक्ट बनाएं. पक्का करें कि आपने सही.orientation
डाला हो.Swift
let image = VisionImage(image: UIImage) visionImage.orientation = image.imageOrientation
Objective-C
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image]; visionImage.orientation = image.imageOrientation;
अगर CMSampleBuffer
का इस्तेमाल किया जाता है, तो यह तरीका अपनाएं:
-
CMSampleBuffer
में शामिल इमेज डेटा का ओरिएंटेशन बताएं.इमेज ओरिएंटेशन पाने के लिए:
Swift
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 } }
Objective-C
- (UIImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored : UIImageOrientationRight; case UIDeviceOrientationLandscapeLeft: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored : UIImageOrientationUp; case UIDeviceOrientationPortraitUpsideDown: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored : UIImageOrientationLeft; case UIDeviceOrientationLandscapeRight: return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored : UIImageOrientationDown; case UIDeviceOrientationUnknown: case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: return UIImageOrientationUp; } }
CMSampleBuffer
ऑब्जेक्ट और ओरिएंटेशन का इस्तेमाल करके,VisionImage
ऑब्जेक्ट बनाएं:Swift
let image = VisionImage(buffer: sampleBuffer) image.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition)
Objective-C
MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer]; image.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
3. इमेज पर लेबल लगाने वाला टूल चलाएं
एसिंक्रोनस रूप से:
Swift
imageLabeler.process(image) { labels, error in guard error == nil, let labels = labels, !labels.isEmpty else { // Handle the error. return } // Show results. }
Objective-C
[imageLabeler processImage:image completion:^(NSArray*_Nullable labels, NSError *_Nullable error) { if (labels.count == 0) { // Handle the error. return; } // Show results. }];
सिंक्रोनस रूप से:
Swift
var labels: [ImageLabel] do { labels = try imageLabeler.results(in: image) } catch let error { // Handle the error. return } // Show results.
Objective-C
NSError *error; NSArray*labels = [imageLabeler resultsInImage:image error:&error]; // Show results or handle the error.
4. लेबल किए गए ऑब्जेक्ट के बारे में जानकारी पाना
अगर इमेज को लेबल करने की कार्रवाई पूरी हो जाती है, तो यहImageLabel
का अरे दिखाता है. हर ImageLabel
कुछ ऐसा दिखाता है जिसे
इमेज में लेबल किया गया है. आपको हर लेबल के टेक्स्ट की जानकारी (अगर TensorFlow Lite मॉडल फ़ाइल के मेटाडेटा में उपलब्ध हो), कॉन्फ़िडेंस स्कोर, और इंडेक्स की जानकारी मिल सकती है.
उदाहरण के लिए:
Swift
for label in labels { let labelText = label.text let confidence = label.confidence let index = label.index }
Objective-C
for (MLKImageLabel *label in labels) { NSString *labelText = label.text; float confidence = label.confidence; NSInteger index = label.index; }
रीयल-टाइम में परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह
अगर आपको रीयल-टाइम ऐप्लिकेशन में इमेज को लेबल करना है, तो सबसे अच्छी फ़्रेमरेट पाने के लिए, इन दिशा-निर्देशों का पालन करें:
- वीडियो फ़्रेम को प्रोसेस करने के लिए, डिटेक्टर के
results(in:)
सिंक्रोनस एपीआई का इस्तेमाल करें. दिए गए वीडियो फ़्रेम से इस तरीके को सिंक करते हुए नतीजे पाने के लिए,AVCaptureVideoDataOutputSampleBufferDelegate
केcaptureOutput(_, didOutput:from:)
फ़ंक्शन से इस तरीके को कॉल करें. डिटेक्टर के पास कॉल रोकने के लिए,AVCaptureVideoDataOutput
केalwaysDiscardsLateVideoFrames
कोtrue
के तौर पर सेट करें. अगर डिटेक्टर के चालू रहने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो उसे हटा दिया जाएगा. - अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो सबसे पहले एमएल किट से नतीजा पाएं. इसके बाद, एक ही बार में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, प्रोसेस किए गए हर इनपुट फ़्रेम के लिए सिर्फ़ एक बार डिसप्ले की सतह पर इमेज बनाई जाती है. उदाहरण के लिए, एमएल किट के क्विकस्टार्ट सैंपल में updatePreviewOverlayViewWithLastFrame देखें.