ML Kit का इस्तेमाल करके, इमेज में मौजूद चीज़ों की पहचान की जा सकती है और उन्हें लेबल किया जा सकता है. यह एपीआई, इमेज के कॉन्टेंट के आधार पर उसकी कैटगरी तय करने वाले कई कस्टम मॉडल के साथ काम करता है. मॉडल की ज़रूरी शर्तों, पहले से ट्रेन किए गए मॉडल ढूंढने की जगह, और अपने मॉडल को ट्रेन करने के तरीके के बारे में जानने के लिए, ML Kit के साथ कस्टम मॉडल का इस्तेमाल करना लेख पढ़ें.
कस्टम मॉडल को इंटिग्रेट करने के दो तरीके हैं. मॉडल को अपने ऐप्लिकेशन के ऐसेट फ़ोल्डर में रखकर बंडल किया जा सकता है. इसके अलावा, इसे Cloud Storage से डाइनैमिक तरीके से डाउनलोड किया जा सकता है. यहां दी गई टेबल में, इन दोनों विकल्पों की तुलना की गई है.
| बंडल किया गया मॉडल | होस्ट किया गया मॉडल |
|---|---|
| मॉडल, आपके ऐप्लिकेशन के APK का हिस्सा होता है. इससे ऐप्लिकेशन का साइज़ बढ़ जाता है. | मॉडल, आपके APK का हिस्सा नहीं होता. इसे Cloud Storage पर अपलोड करके होस्ट किया जाता है. हमारा सुझाव है कि आप Firebase के लिए Cloud Storage का इस्तेमाल करें. |
| मॉडल तुरंत उपलब्ध हो जाता है. भले ही, Android डिवाइस ऑफ़लाइन हो | आपके ऐप्लिकेशन में, ज़रूरत पड़ने पर मॉडल डाउनलोड करने के लिए कोड शामिल होना चाहिए |
| Firebase प्रोजेक्ट की ज़रूरत नहीं होती | Firebase प्रोजेक्ट की ज़रूरत होती है (अगर Firebase के लिए Cloud Storage का इस्तेमाल किया जा रहा है). |
| मॉडल को अपडेट करने के लिए, आपको अपना ऐप्लिकेशन फिर से पब्लिश करना होगा | ऐप्लिकेशन को फिर से पब्लिश किए बिना, मॉडल के अपडेट पुश करें |
| कोई इन-बिल्ट A/B टेस्टिंग नहीं होती | Firebase रिमोट कॉन्फ़िगरेशन के साथ A/B टेस्टिंग |
इसे आज़माएं
- बंडल किए गए मॉडल के इस्तेमाल का उदाहरण देखने के लिए, विज़न क्विकस्टार्ट ऐप्लिकेशन देखें. होस्ट किए गए मॉडल के इस्तेमाल का उदाहरण देखने के लिए, AutoML क्विकस्टार्ट ऐप्लिकेशन देखें.
शुरू करने से पहले
अपने Podfile में, ML Kit की लाइब्रेरी शामिल करें:
pod 'GoogleMLKit/ImageLabelingCustom', '8.0.0'अपने प्रोजेक्ट के पॉड इंस्टॉल या अपडेट करने के बाद,
.xcworkspaceका इस्तेमाल करके अपना Xcode प्रोजेक्ट खोलें. ML Kit, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करता है.अगर आपको Firebase के लिए Cloud Storage का इस्तेमाल करके कोई मॉडल डाउनलोड करना है, तो पक्का करें कि आपने अपने iOS प्रोजेक्ट में Firebase जोड़ा हो. अगर आपने ऐसा नहीं किया है, तो 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 के लिए Cloud Storage का इस्तेमाल करें. लागू करने के बारे में ज़्यादा जानने के लिए, Firebase ML से Cloud Storage पर माइग्रेट करने से जुड़ी गाइड देखें.
इमेज लेबलर कॉन्फ़िगर करना
मॉडल के सोर्स कॉन्फ़िगर करने के बाद, उनमें से किसी एक से 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];
अगर आपके पास रिमोट तरीके से होस्ट किया गया मॉडल है, तो आपको इसे चलाने से पहले यह देखना होगा कि यह डाउनलोड हो गया है या नहीं.
हालांकि, आपको लेबलर चलाने से पहले सिर्फ़ इसकी पुष्टि करनी होती है. अगर आपके पास रिमोट तरीके से होस्ट किया गया मॉडल और लोकल तरीके से बंडल किया गया मॉडल, दोनों हैं, तो ImageLabeler को इंस्टैंशिएट करते समय, इस जांच को करना सही हो सकता है: अगर रिमोट मॉडल डाउनलोड हो गया है, तो उससे एक लेबलर बनाएं. अगर ऐसा नहीं है, तो लोकल मॉडल से एक लेबलर बनाएं.
Swift
// Path where your download logic saves the model let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let localModelURL = documentDirectory.appendingPathComponent("my_remote_model.tflite") let model: LocalModel if FileManager.default.fileExists(atPath: localModelURL.path) { // Use the downloaded model model = LocalModel(path: localModelURL.path) } else { // Fall back to bundled model guard let bundledModelPath = Bundle.main.path(forResource: "model", ofType: "tflite") else { return } model = LocalModel(path: bundledModelPath) } let options = CustomImageLabelerOptions(localModel: model) let imageLabeler = ImageLabeler.imageLabeler(options: options)
Objective-C
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *localModelPath = [documentsDirectory stringByAppendingPathComponent:@"my_remote_model.tflite"]; MLKLocalModel *model; if ([NSFileManager.defaultManager fileExistsAtPath:localModelPath]) { // Use the downloaded model model = [[MLKLocalModel alloc] initWithPath:localModelPath]; } else { // Fall back to bundled model NSString *bundledModelPath = [NSBundle.mainBundle pathForResource:@"model" ofType:@"tflite"]; model = [[MLKLocalModel alloc] initWithPath:bundledModelPath]; } MLKCustomImageLabelerOptions *options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:model]; MLKImageLabeler *imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
अगर आपके पास सिर्फ़ रिमोट तरीके से होस्ट किया गया मॉडल है, तो आपको मॉडल से जुड़ी सुविधाओं को बंद कर देना चाहिए. जैसे, यूज़र इंटरफ़ेस (यूआई) के किसी हिस्से को धुंधला कर देना या छिपा देना. ऐसा तब तक करें, जब तक आपको यह पुष्टि न हो जाए कि मॉडल डाउनलोड हो गया है.
Swift
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let localModelURL = documentDirectory.appendingPathComponent("my_remote_model.tflite") if FileManager.default.fileExists(atPath: localModelURL.path) { // Model is already cached, initialize immediately self.initializeLabeler(with: localModelURL) } else { // Model is not yet available, show loading UI and start download self.showLoadingUI() let storage = Storage.storage() let modelRef = storage.reference(forURL: "gs://YOUR_BUCKET/path/to/model.tflite") modelRef.write(toFile: localModelURL) { url, error in self.hideLoadingUI() if let error = error { // Handle download error self.showErrorUI() } else if let modelURL = url { // Download success, initialize labeler self.initializeLabeler(with: modelURL) } } } func initializeLabeler(with modelURL: URL) { let localModel = LocalModel(path: modelURL.path) let options = CustomImageLabelerOptions(localModel: localModel) self.imageLabeler = ImageLabeler.imageLabeler(options: options) // Enable ML-related UI features here self.enableMLFeatures() }
Objective-C
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *localModelPath = [documentsDirectory stringByAppendingPathComponent:@"my_remote_model.tflite"]; NSURL *localModelURL = [NSURL fileURLWithPath:localModelPath]; if ([NSFileManager.defaultManager fileExistsAtPath:localModelPath]) { // Model is already cached, initialize immediately [self initializeLabelerWithURL:localModelURL]; } else { // Model is not yet available, show loading UI and start download [self showLoadingUI]; FIRStorage *storage = [FIRStorage storage]; FIRStorageReference *modelRef = [storage referenceForURL:@"gs://YOUR_BUCKET/path/to/model.tflite"]; [modelRef writeToFile:localModelURL completion:^(NSURL * _Nullable URL, NSError * _Nullable error) { [self hideLoadingUI]; if (error != nil) { // Handle download error [self showErrorUI]; } else { // Download success, initialize labeler [self initializeLabelerWithURL:URL]; } }]; } - (void)initializeLabelerWithURL:(NSURL *)modelURL { MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithPath:modelURL.path]; MLKCustomImageLabelerOptions *options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel]; self.imageLabeler = [MLKImageLabeler imageLabelerWithOptions:options]; // Enable ML-related UI features here [self enableMLFeatures]; }
2. इनपुट इमेज तैयार करना
VisionImage ऑब्जेक्ट बनाने के लिए, UIImage या
CMSampleBuffer का इस्तेमाल करें.
अगर UIImage का इस्तेमाल किया जाता है, तो यह तरीका अपनाएं:
VisionImageऑब्जेक्ट,UIImageकी मदद से बनाएं. पक्का करें कि.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; } }
VisionImageऑब्जेक्ट और ओरिएंटेशन का इस्तेमाल करके,CMSampleBufferऑब्जेक्ट बनाएं: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, इमेज में लेबल की गई किसी चीज़ को दिखाता है. हर लेबल की टेक्स्ट जानकारी (अगर LiteRT मॉडल फ़ाइल के मेटाडेटा में उपलब्ध है), कॉन्फ़िडेंस स्कोर, और इंडेक्स पाया जा सकता है. उदाहरण के लिए:
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'scaptureOutput(_, didOutput:from:)फ़ंक्शन से कॉल करें. डिटेक्टर को कॉल करने की संख्या को कम करने के लिए,AVCaptureVideoDataOutputकेalwaysDiscardsLateVideoFramesकोtrueके तौर पर सेट करें. अगर डिटेक्टर के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध होता है, तो उसे छोड़ दिया जाएगा. - अगर इनपुट इमेज पर ग्राफ़िक्स ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो पहले ML Kit से नतीजे पाएं. इसके बाद, एक ही चरण में इमेज रेंडर करें और ओवरले करें. ऐसा करने से, प्रोसेस किए गए हर इनपुट फ़्रेम के लिए, डिसप्ले सर्फ़ेस पर सिर्फ़ एक बार रेंडर किया जाता है. उदाहरण के लिए, ML Kit के क्विकस्टार्ट सैंपल में updatePreviewOverlayViewWithLastFrame देखें.