Apple प्लैटफ़ॉर्म पर, AutoML की मदद से ट्रेन किए गए मॉडल की इमेज को लेबल करें

AutoML Vision Edge इस्तेमाल करके, मॉडल को ट्रेनिंग देने के बाद, ऐप्लिकेशन में इमेज को लेबल करने के लिए इसका इस्तेमाल किया जा सकता है.

AutoML Vision Edge से ट्रेन किए गए मॉडल को इंटिग्रेट करने के दो तरीके हैं. अपने Xcode प्रोजेक्ट में मॉडल की फ़ाइलों को कॉपी करके, मॉडल को बंडल किया जा सकता है या Firebase से डाइनैमिक तौर पर डाउनलोड किया जा सकता है.

मॉडल बंडलिंग के विकल्प
आपके ऐप्लिकेशन में शामिल
  • मॉडल, बंडल का हिस्सा है
  • Apple डिवाइस के ऑफ़लाइन होने पर भी, यह मॉडल तुरंत उपलब्ध हो जाता है
  • Firebase प्रोजेक्ट की ज़रूरत नहीं होती है
Firebase के साथ होस्ट किया गया
  • Firebase मशीन लर्निंग पर अपलोड करके मॉडल को होस्ट करें
  • ऐप्लिकेशन बंडल का साइज़ कम करता है
  • मॉडल को मांग पर डाउनलोड किया जाता है
  • अपने ऐप्लिकेशन को फिर से पब्लिश किए बिना, मॉडल के अपडेट पुश करें
  • Firebase रिमोट कॉन्फ़िगरेशन की मदद से आसान A/B टेस्टिंग
  • Firebase प्रोजेक्ट होना ज़रूरी है

शुरू करने से पहले

  1. अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:

    अपने ऐप्लिकेशन के साथ किसी मॉडल को बंडल करने के लिए:

    pod 'GoogleMLKit/ImageLabelingCustom'
    

    Firebase से मॉडल को डाइनैमिक तौर पर डाउनलोड करने के लिए, LinkFirebase डिपेंडेंसी जोड़ें:

    pod 'GoogleMLKit/ImageLabelingCustom'
    pod 'GoogleMLKit/LinkFirebase'
    
  2. प्रोजेक्ट के Pods को इंस्टॉल या अपडेट करने के बाद, उसके .xcworkspace का इस्तेमाल करके अपना Xcode प्रोजेक्ट खोलें. ML Kit, Xcode के 12.2 या इसके बाद के वर्शन पर काम करता है.

  3. अगर आपको कोई मॉडल डाउनलोड करना है, तो पक्का करें कि आपने अपने Android प्रोजेक्ट में Firebase जोड़ें. अगर आपने पहले से ऐसा नहीं किया है. मॉडल को बंडल करते समय, ऐसा करना ज़रूरी नहीं है.

1. मॉडल लोड करें

लोकल मॉडल सोर्स कॉन्फ़िगर करना

मॉडल को अपने ऐप्लिकेशन के साथ बंडल करने के लिए:

  1. Firebase कंसोल से डाउनलोड किए गए ZIP संग्रह से मॉडल और उसके मेटाडेटा को एक फ़ोल्डर में एक्सट्रैक्ट करें:

    your_model_directory
      |____dict.txt
      |____manifest.json
      |____model.tflite
    

    तीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए. हमारा सुझाव है कि आप फ़ाइलों को बिना बदलाव (इसमें फ़ाइल के नाम भी शामिल हैं) को डाउनलोड करने के बाद ही उनका इस्तेमाल करें.

  2. फ़ोल्डर को अपने Xcode प्रोजेक्ट में कॉपी करें. ऐसा करते समय फ़ोल्डर के रेफ़रंस बनाएं विकल्प का ध्यान रखें. मॉडल फ़ाइल और मेटाडेटा को ऐप्लिकेशन बंडल में शामिल किया जाएगा और ML Kit में भी उपलब्ध कराया जाएगा.

  3. मॉडल मेनिफ़ेस्ट फ़ाइल का पाथ बताते हुए LocalModel ऑब्जेक्ट बनाएं:

    Swift

    guard let manifestPath = Bundle.main.path(
        forResource: "manifest",
        ofType: "json",
        inDirectory: "your_model_directory"
    ) else { return true }
    let localModel = LocalModel(manifestPath: manifestPath)
    

    Objective-C

    NSString *manifestPath =
        [NSBundle.mainBundle pathForResource:@"manifest"
                                      ofType:@"json"
                                 inDirectory:@"your_model_directory"];
    MLKLocalModel *localModel =
        [[MLKLocalModel alloc] initWithManifestPath:manifestPath];
    

Firebase से होस्ट किए गए मॉडल सोर्स को कॉन्फ़िगर करना

रिमोट तरीके से होस्ट किए गए मॉडल का इस्तेमाल करने के लिए, CustomRemoteModel ऑब्जेक्ट बनाएं. साथ ही, उस मॉडल का नाम बताएं जिसे पब्लिश करते समय आपने मॉडल को असाइन किया था:

Swift

// 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)

Objective-C

// 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 से मॉडल को एसिंक्रोनस रूप से डाउनलोड करेगा:

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] downloadRemoteModel:remoteModel
                                             conditions:downloadConditions];

कई ऐप्लिकेशन अपने इनिशलाइज़ेशन कोड में डाउनलोड टास्क शुरू करते हैं, लेकिन मॉडल का इस्तेमाल करने से पहले, किसी भी समय ऐसा किया जा सकता है.

अपने मॉडल से इमेज लेबलर बनाएं

अपने मॉडल सोर्स को कॉन्फ़िगर करने के बाद, उनमें से किसी एक से ImageLabeler ऑब्जेक्ट बनाएं.

अगर आपके पास सिर्फ़ लोकल बंडल किया गया मॉडल है, तो बस अपने LocalModel ऑब्जेक्ट से लेबलर बनाएं और अपनी ज़रूरत के हिसाब से कॉन्फ़िडेंस स्कोर की सीमा कॉन्फ़िगर करें (अपने मॉडल का आकलन करें देखें):

Swift

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)

Objective-C

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 को इंस्टैंशिएट करते समय यह जांच करने की समझ आ सकती है: अगर डाउनलोड किया गया है, तो रिमोट मॉडल से कोई लेबलर बनाएं, नहीं तो स्थानीय मॉडल से.

Swift

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)

Objective-C

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];

अगर आपके पास सिर्फ़ रिमोट तौर पर होस्ट किया गया मॉडल है, तो आपको मॉडल से जुड़ी फ़ंक्शनलिटी बंद करनी चाहिए. उदाहरण के लिए, अपने यूज़र इंटरफ़ेस (यूआई) का कुछ हिस्सा धूसर करना या छिपाना. ऐसा तब तक करना चाहिए, जब तक आप यह पुष्टि न कर लें कि मॉडल डाउनलोड हो गया है.

डिफ़ॉल्ट नोटिफ़िकेशन सेंटर में ऑब्ज़र्वर अटैच करके, मॉडल डाउनलोड की स्थिति देखी जा सकती है. ऑब्ज़र्वर ब्लॉक में, self के लिए कमज़ोर रेफ़रंस का इस्तेमाल करना न भूलें. ऐसा इसलिए, क्योंकि डाउनलोड होने में कुछ समय लग सकता है. साथ ही, डाउनलोड पूरा होने तक, ओरिजनल ऑब्जेक्ट को फ़्री किया जा सकता है. उदाहरण के लिए:

Swift

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]
    // ...
}

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 या CMSampleBufferRef का इस्तेमाल करके, 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;

अगर CMSampleBufferRef का इस्तेमाल किया जाता है, तो यह तरीका अपनाएं:

  • CMSampleBufferRef बफ़र में शामिल इमेज के डेटा का ओरिएंटेशन बताएं.

    इमेज का ओरिएंटेशन पाने के लिए:

    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 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 ऑब्जेक्ट बनाएं:

    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<MLKImageLabel *> *_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<MLKImageLabel *> *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;
}

रीयल-टाइम परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह

अगर आपको किसी रीयल-टाइम ऐप्लिकेशन में इमेज को लेबल करना है, तो सबसे सही फ़्रेमरेट पाने के लिए, इन दिशा-निर्देशों का पालन करें:

  • डिटेक्टर को कॉल थ्रॉटल करें. अगर डिटेक्टर के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो फ़्रेम को छोड़ दें.
  • अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो सबसे पहले नतीजा पाएं. इसके बाद, एक ही चरण में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, हर इनपुट फ़्रेम के लिए आपको डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर करना होगा. उदाहरण के लिए, शोकेस सैंपल ऐप्लिकेशन में previewOverlayView और FIRDetectionOverlayView क्लास देखें.