Apple प्लेटफ़ॉर्म पर AutoML-प्रशिक्षित मॉडल के साथ छवियों को लेबल करें

ऑटोएमएल विज़न एज का उपयोग करके अपने स्वयं के मॉडल को प्रशिक्षित करने के बाद, आप छवियों को लेबल करने के लिए इसे अपने ऐप में उपयोग कर सकते हैं।

ऑटोएमएल विज़न एज से प्रशिक्षित मॉडलों को एकीकृत करने के दो तरीके हैं। आप मॉडल की फ़ाइलों को अपने Xcode प्रोजेक्ट में कॉपी करके मॉडल को बंडल कर सकते हैं, या आप इसे फ़ायरबेस से गतिशील रूप से डाउनलोड कर सकते हैं।

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

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

  1. अपने पॉडफाइल में एमएल किट लाइब्रेरी शामिल करें:

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

    pod 'GoogleMLKit/ImageLabelingCustom'
    

    फ़ायरबेस से किसी मॉडल को गतिशील रूप से डाउनलोड करने के लिए, LinkFirebase निर्भरता जोड़ें:

    pod 'GoogleMLKit/ImageLabelingCustom'
    pod 'GoogleMLKit/LinkFirebase'
    
  2. अपने प्रोजेक्ट के पॉड्स को इंस्टॉल या अपडेट करने के बाद, अपने Xcode प्रोजेक्ट को इसके .xcworkspace का उपयोग करके खोलें। एमएल किट Xcode संस्करण 12.2 या उच्चतर में समर्थित है।

  3. यदि आप कोई मॉडल डाउनलोड करना चाहते हैं , तो सुनिश्चित करें कि आपने अपने एंड्रॉइड प्रोजेक्ट में फायरबेस जोड़ा है , यदि आपने पहले से ऐसा नहीं किया है। जब आप मॉडल को बंडल करते हैं तो इसकी आवश्यकता नहीं होती है।

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

स्थानीय मॉडल स्रोत कॉन्फ़िगर करें

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

  1. फायरबेस कंसोल से डाउनलोड किए गए ज़िप संग्रह से मॉडल और उसके मेटाडेटा को एक फ़ोल्डर में निकालें:

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

    तीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए. हम अनुशंसा करते हैं कि आप फ़ाइलों को वैसे ही उपयोग करें जैसे आपने उन्हें डाउनलोड किया था, बिना किसी संशोधन (फ़ाइल नाम सहित) के।

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

  3. मॉडल मेनिफेस्ट फ़ाइल का पथ निर्दिष्ट करते हुए 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];
    

फ़ायरबेस-होस्टेड मॉडल स्रोत कॉन्फ़िगर करें

दूरस्थ रूप से होस्ट किए गए मॉडल का उपयोग करने के लिए, एक 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];

फिर, उन शर्तों को निर्दिष्ट करते हुए मॉडल डाउनलोड कार्य शुरू करें जिनके तहत आप डाउनलोड करने की अनुमति देना चाहते हैं। यदि मॉडल डिवाइस पर नहीं है, या यदि मॉडल का एक नया संस्करण उपलब्ध है, तो कार्य फायरबेस से मॉडल को एसिंक्रोनस रूप से डाउनलोड करेगा:

तीव्र

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

यदि आपके पास केवल दूरस्थ रूप से होस्ट किया गया मॉडल है, तो आपको मॉडल-संबंधित कार्यक्षमता को अक्षम करना चाहिए - उदाहरण के लिए, अपने यूआई के हिस्से को ग्रे-आउट या छुपाएं - जब तक आप पुष्टि नहीं करते कि मॉडल डाउनलोड हो गया है।

आप पर्यवेक्षकों को डिफ़ॉल्ट अधिसूचना केंद्र में संलग्न करके मॉडल डाउनलोड स्थिति प्राप्त कर सकते हैं। पर्यवेक्षक ब्लॉक में 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];

3. छवि लेबलर चलाएँ

अतुल्यकालिक रूप से:

तीव्र

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.

4. लेबल वाली वस्तुओं के बारे में जानकारी प्राप्त करें

यदि छवि लेबलिंग ऑपरेशन सफल होता है, तो यह ImageLabel की एक सरणी लौटाता है। प्रत्येक ImageLabel उस चीज़ का प्रतिनिधित्व करता है जिसे छवि में लेबल किया गया था। आप प्रत्येक लेबल का टेक्स्ट विवरण (यदि टेन्सरफ्लो लाइट मॉडल फ़ाइल के मेटाडेटा में उपलब्ध है), कॉन्फिडेंस स्कोर और इंडेक्स प्राप्त कर सकते हैं। उदाहरण के लिए:

तीव्र

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

वास्तविक समय के प्रदर्शन को बेहतर बनाने के लिए युक्तियाँ

यदि आप वास्तविक समय एप्लिकेशन में छवियों को लेबल करना चाहते हैं, तो सर्वोत्तम फ्रैमरेट्स प्राप्त करने के लिए इन दिशानिर्देशों का पालन करें:

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