एमएल किट का इस्तेमाल करके, इमेज में मौजूद इकाइयों को पहचाना जा सकता है और उन्हें लेबल किया जा सकता है. यह एपीआई, पसंद के मुताबिक इमेज की कैटगरी तय करने वाले कई तरह के मॉडल के साथ काम करता है. मॉडल के साथ काम करने से जुड़ी ज़रूरी शर्तों, पहले से ट्रेनिंग दिए गए मॉडल कहां मिलेंगे, और अपने मॉडल को ट्रेनिंग कैसे दी जा सकती है, इस बारे में दिशा-निर्देश पाने के लिए, कृपया एमएल किट वाले कस्टम मॉडल देखें.
कस्टम मॉडल को इंटिग्रेट करने के दो तरीके हैं. मॉडल को अपने ऐप्लिकेशन के एसेट फ़ोल्डर में डालकर, उसे बंडल किया जा सकता है या Firebase से डाइनैमिक तौर पर डाउनलोड किया जा सकता है. नीचे दी गई टेबल में दोनों विकल्पों की तुलना की गई है.
बंडल किया गया मॉडल | होस्ट किया गया मॉडल |
---|---|
यह मॉडल आपके ऐप्लिकेशन के APK का हिस्सा होता है, जिससे इसका साइज़ बढ़ जाता है. | यह मॉडल आपके APK का हिस्सा नहीं है. इसे Firebase मशीन लर्निंग पर अपलोड करके होस्ट किया जाता है. |
यह मॉडल तुरंत उपलब्ध है, भले ही Android डिवाइस ऑफ़लाइन हो | मॉडल को मांग पर डाउनलोड किया गया है |
Firebase प्रोजेक्ट की ज़रूरत नहीं है | Firebase प्रोजेक्ट की ज़रूरत होती है |
मॉडल अपडेट करने के लिए, आपको ऐप्लिकेशन को फिर से पब्लिश करना होगा | अपने ऐप्लिकेशन को फिर से पब्लिश किए बिना, मॉडल के अपडेट पुश करें |
पहले से मौजूद A/B टेस्टिंग की सुविधा मौजूद नहीं है | Firebase रिमोट कॉन्फ़िगरेशन की मदद से, आसानी से A/B टेस्टिंग की जा सकती है |
इसे आज़माएं
- बंडल किए गए मॉडल के इस्तेमाल के उदाहरण के लिए, विज़न क्विकस्टार्ट ऐप्लिकेशन देखें. साथ ही, होस्ट किए गए मॉडल के इस्तेमाल के उदाहरण के लिए, automl क्विकस्टार्ट ऐप्लिकेशन देखें.
शुरू करने से पहले
अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:
अपने ऐप्लिकेशन के साथ किसी मॉडल को बंडल करने के लिए:
pod 'GoogleMLKit/ImageLabelingCustom', '3.2.0'
Firebase से किसी मॉडल को डाइनैमिक तरीके से डाउनलोड करने के लिए,
LinkFirebase
डिपेंडेंसी जोड़ें:pod 'GoogleMLKit/ImageLabelingCustom', '3.2.0' pod 'GoogleMLKit/LinkFirebase', '3.2.0'
अपने प्रोजेक्ट के पॉड इंस्टॉल या अपडेट करने के बाद, Xcode प्रोजेक्ट खोलें. इसके लिए,
.xcworkspace
का इस्तेमाल करें. ML किट, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करती है.अगर आपको कोई मॉडल डाउनलोड करना है, तो पक्का करें कि आपने अपने iOS प्रोजेक्ट में Firebase को जोड़ा हो. ऐसा तब करें, जब आपने पहले से ऐसा न किया हो. मॉडल को बंडल करते समय ऐसा करने की ज़रूरत नहीं होती.
1. मॉडल लोड करें
लोकल मॉडल के सोर्स को कॉन्फ़िगर करना
मॉडल को अपने ऐप्लिकेशन के साथ बंडल करने के लिए:
मॉडल फ़ाइल (आम तौर पर
.tflite
या.lite
पर खत्म होती है) को अपने Xcode प्रोजेक्ट में कॉपी करें. ऐसा करते समय,Copy bundle resources
को चुनें. मॉडल फ़ाइल, ऐप्लिकेशन बंडल में शामिल की जाएगी और ML Kit में उपलब्ध होगी.मॉडल फ़ाइल का पाथ बताते हुए
LocalModel
ऑब्जेक्ट बनाएं:Swift
let localModel = LocalModel(path: localModelFilePath)
Objective-C
MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithPath:localModelFilePath];
Firebase से होस्ट किए गए मॉडल सोर्स को कॉन्फ़िगर करें
रिमोट तरीके से होस्ट किए गए मॉडल का इस्तेमाल करने के लिए, RemoteModel
ऑब्जेक्ट बनाएं. इसमें वह नाम बताएं जिसे आपने मॉडल को पब्लिश करते समय असाइन किया था:
Swift
let firebaseModelSource = FirebaseModelSource( name: "your_remote_model") // The name you assigned in // the Firebase console. let remoteModel = CustomRemoteModel(remoteModelSource: firebaseModelSource)
Objective-C
MLKFirebaseModelSource *firebaseModelSource = [[MLKFirebaseModelSource alloc] initWithName:@"your_remote_model"]; // The name you assigned in // the Firebase console. MLKCustomRemoteModel *remoteModel = [[MLKCustomRemoteModel alloc] initWithRemoteModelSource:firebaseModelSource];
इसके बाद, उन शर्तों को तय करते हुए मॉडल डाउनलोड टास्क शुरू करें जिनके तहत आपको डाउनलोड करने की अनुमति देनी है. अगर मॉडल, डिवाइस पर मौजूद नहीं है या मॉडल का कोई नया वर्शन उपलब्ध है, तो टास्क, 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
ऑब्जेक्ट बनाएं.
ये विकल्प उपलब्ध हैं:
विकल्प | |
---|---|
confidenceThreshold
|
पता लगाए गए लेबल का कम से कम कॉन्फ़िडेंस स्कोर. अगर यह नीति सेट नहीं है, तो मॉडल के मेटाडेटा में तय की गई, कैटगरी तय करने के लिए तय किए गए थ्रेशोल्ड का इस्तेमाल किया जाएगा. अगर मॉडल में कोई मेटाडेटा नहीं है या मेटाडेटा में डेटा की कैटगरी तय करने के लिए कोई थ्रेशोल्ड तय नहीं किया गया है, तो 0.0 की डिफ़ॉल्ट थ्रेशोल्ड का इस्तेमाल किया जाएगा. |
maxResultCount
|
लौटाए जाने वाले लेबल की ज़्यादा से ज़्यादा संख्या. इस नीति को सेट न करने पर, 10 की डिफ़ॉल्ट वैल्यू का इस्तेमाल किया जाएगा. |
अगर आपके पास सिर्फ़ स्थानीय तौर पर बंडल किया गया मॉडल है, तो अपने LocalModel
ऑब्जेक्ट से लेबलर बनाएं:
Swift
let options = CustomImageLabelerOptions(localModel: localModel) options.confidenceThreshold = NSNumber(value: 0.0) let imageLabeler = ImageLabeler.imageLabeler(options: options)
Objective-C
MLKCustomImageLabelerOptions *options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel]; options.confidenceThreshold = @(0.0); MLKImageLabeler *imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
अगर आपके पास रिमोट तरीके से होस्ट किया गया मॉडल है, तो चलाने से पहले आपको यह देखना होगा कि उसे डाउनलोड किया गया है या नहीं. मॉडल मैनेजर के isModelDownloaded(remoteModel:)
तरीके का इस्तेमाल करके, मॉडल डाउनलोड
टास्क की स्थिति देखी जा सकती है.
हालांकि, आपको लेबलर चलाने से पहले ही इसकी पुष्टि करनी होती है. अगर आपके पास रिमोट तौर पर होस्ट किया गया मॉडल और स्थानीय तौर पर बंडल किया गया मॉडल, दोनों हैं, तो ImageLabeler
को इंस्टैंशिएट करते समय, यह जांच करने से मदद मिल सकती है: अगर डाउनलोड किया गया है, तो रिमोट मॉडल से लेबलर बनाएं. ऐसा न करने पर, लोकल मॉडल से लेबलर बनाएं.
Swift
var options: CustomImageLabelerOptions! if (ModelManager.modelManager().isModelDownloaded(remoteModel)) { options = CustomImageLabelerOptions(remoteModel: remoteModel) } else { options = CustomImageLabelerOptions(localModel: localModel) } options.confidenceThreshold = NSNumber(value: 0.0) let imageLabeler = ImageLabeler.imageLabeler(options: options)
Objective-C
MLKCustomImageLabelerOptions *options; if ([[MLKModelManager modelManager] isModelDownloaded:remoteModel]) { options = [[MLKCustomImageLabelerOptions alloc] initWithRemoteModel:remoteModel]; } else { options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel]; } options.confidenceThreshold = @(0.0); 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. इमेज लेबलर चलाएं
किसी इमेज में मौजूद ऑब्जेक्ट को लेबल करने के लिए, image
ऑब्जेक्ट को ImageLabeler
के process()
तरीके में पास करें.
एसिंक्रोनस रूप से:
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 (label.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 लाइट मॉडल फ़ाइल के मेटाडेटा में उपलब्ध हो), कॉन्फ़िडेंस स्कोर, और इंडेक्स की जानकारी मिल सकती है.
उदाहरण के लिए:
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 देखें.