iOS पर, इमेज को कस्टम मॉडल से लेबल करें

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

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

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

इसे आज़माएं

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

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

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

    pod 'GoogleMLKit/ImageLabelingCustom', '7.0.0'
    

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

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

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

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

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

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

  1. अपने Xcode में मॉडल फ़ाइल (आम तौर पर, जिसके आखिर में .tflite या .lite आता है) कॉपी करें प्रोजेक्ट शामिल करते समय ध्यान रखें कि ऐसा करते समय Copy bundle resources को चुनें. कॉन्टेंट बनाने मॉडल फ़ाइल, ऐप्लिकेशन बंडल में शामिल की जाएगी और ML किट में उपलब्ध होगी.

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

    SwiftObjective-C
    let localModel = LocalModel(path: localModelFilePath)
    MLKLocalModel *localModel =
        [[MLKLocalModel alloc] initWithPath:localModelFilePath];

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

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

SwiftObjective-C
let firebaseModelSource = FirebaseModelSource(
    name: "your_remote_model") // The name you assigned in
                               // the Firebase console.
let remoteModel = CustomRemoteModel(remoteModelSource: firebaseModelSource)
MLKFirebaseModelSource *firebaseModelSource =
    [[MLKFirebaseModelSource alloc]
        initWithName:@"your_remote_model"]; // The name you assigned in
                                            // the Firebase console.
MLKCustomRemoteModel *remoteModel =
    [[MLKCustomRemoteModel alloc]
        initWithRemoteModelSource:firebaseModelSource];

इसके बाद, उन शर्तों को तय करते हुए मॉडल डाउनलोड टास्क शुरू करें को डाउनलोड करने की अनुमति देनी है. अगर मॉडल डिवाइस पर नहीं है या नया डिवाइस है, तो मॉडल का वर्शन उपलब्ध है, तो टास्क एसिंक्रोनस रूप से Firebase से मिला मॉडल:

SwiftObjective-C
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] downloadModel:remoteModel
                                       conditions:downloadConditions];

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

इमेज लेबल करने की सेटिंग को कॉन्फ़िगर करें

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

ये विकल्प उपलब्ध हैं:

विकल्प
confidenceThreshold

पता लगाए गए लेबल का कम से कम कॉन्फ़िडेंस स्कोर. अगर यह नीति सेट नहीं है, तो किसी भी मॉडल के मेटाडेटा से तय किया गया क्लासिफ़ायर थ्रेशोल्ड इस्तेमाल किया जाएगा. अगर मॉडल में कोई मेटाडेटा नहीं है या मेटाडेटा में डेटा की कैटगरी तय करने वाले थ्रेशोल्ड को तय करें, तो डिफ़ॉल्ट थ्रेशोल्ड 0.0 होगा इस्तेमाल किया गया.

maxResultCount

लौटाए जाने वाले लेबल की ज़्यादा से ज़्यादा संख्या. अगर यह सेट नहीं है, तो 10 का इस्तेमाल किया जाएगा.

अगर आपके पास केवल स्थानीय रूप से बंडल किया गया मॉडल है, तो बस अपने LocalModel ऑब्जेक्ट:

SwiftObjective-C
let options = CustomImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = NSNumber(value: 0.0)
let imageLabeler = ImageLabeler.imageLabeler(options: options)
MLKCustomImageLabelerOptions *options =
    [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = @(0.0);
MLKImageLabeler *imageLabeler =
    [MLKImageLabeler imageLabelerWithOptions:options];

अगर आपके पास रिमोट तौर पर होस्ट किया गया मॉडल है, तो आपको यह देखना होगा कि डाउनलोड करने की सुविधा देता है. मॉडल के डाउनलोड होने की स्थिति देखी जा सकती है टास्क बनाने के लिए, मॉडल मैनेजर के isModelDownloaded(remoteModel:) तरीके का इस्तेमाल करें.

हालांकि, लेबलर को चलाने से पहले आपको इसकी पुष्टि करनी होगी, अगर रिमोट तौर पर होस्ट किया गया मॉडल और लोकल-बंडल्ड मॉडल, दोनों होने चाहिए, तो इससे ImageLabeler को इंस्टैंशिएट करते समय यह चेक करें: अगर रिमोट मॉडल की मदद से लेबलर को डाउनलोड किया गया है और स्थानीय मॉडल से उसे डाउनलोड किया गया है, तो नहीं तो.

SwiftObjective-C
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)
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 के लिए कमज़ोर रेफ़रंस का इस्तेमाल किया गया हो ब्लॉक है, क्योंकि डाउनलोड होने में कुछ समय लग सकता है और मूल ऑब्जेक्ट डाउनलोड पूरा होने पर खाली हो जाएगा. उदाहरण के लिए:

SwiftObjective-C
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]
    // ...
}
__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. इनपुट इमेज तैयार करें

एक VisionImage ऑब्जेक्ट को UIImage या CMSampleBuffer.

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

  • UIImage के साथ एक VisionImage ऑब्जेक्ट बनाएं. पक्का करें कि आपने सही .orientation तय किया हो.
    SwiftObjective-C
    let image = VisionImage(image: UIImage)
    visionImage.orientation = image.imageOrientation
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage.orientation = image.imageOrientation;

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

  • इसमें शामिल इमेज डेटा का ओरिएंटेशन तय करें CMSampleBuffer.

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

    SwiftObjective-C
    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 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 ऑब्जेक्ट और ओरिएंटेशन:
    SwiftObjective-C
    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. इमेज लेबलर चलाएं

किसी इमेज में ऑब्जेक्ट को लेबल करने के लिए, image ऑब्जेक्ट को ImageLabeler के process() तरीका.

एसिंक्रोनस तरीके से:

SwiftObjective-C
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 *_Nullable labels,
                   NSError *_Nullable error) {
        if (label.count == 0) {
            // Handle the error.
            return;
        }
        // Show results.
     }];

सिंक्रोनस:

SwiftObjective-C
var labels: [ImageLabel]
do {
    labels = try imageLabeler.results(in: image)
} catch let error {
    // Handle the error.
    return
}
// Show results.
NSError *error;
NSArray *labels =
    [imageLabeler resultsInImage:image error:&error];
// Show results or handle the error.

4. लेबल की गई इकाइयों के बारे में जानकारी पाएं

अगर इमेज लेबल करने की कार्रवाई सफल होती है, तो यह ImageLabel. हर ImageLabel कुछ ऐसा दिखाता है जो लेबल किया गया है. आप हर लेबल के टेक्स्ट की जानकारी पा सकते है (अगर TensorFlow Lite मॉडल फ़ाइल का मेटाडेटा), कॉन्फ़िडेंस स्कोर, और इंडेक्स. उदाहरण के लिए:

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

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

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

  • वीडियो फ़्रेम प्रोसेस करने के लिए, डिटेक्टर के results(in:) सिंक्रोनस एपीआई का इस्तेमाल करें. कॉल करें AVCaptureVideoDataOutputSampleBufferDelegate का captureOutput(_, didOutput:from:) फ़ंक्शन का इस्तेमाल, दिए गए वीडियो से सिंक्रोनस रूप से नतीजे पाने के लिए किया जाता है फ़्रेम. रखें AVCaptureVideoDataOutput का डिटेक्टर को कॉल थ्रॉटल करने के लिए, alwaysDiscardsLateVideoFrames को true के तौर पर सबमिट किया है. अगर नए डिटेक्टर के चलने के दौरान वीडियो फ़्रेम उपलब्ध हो जाता है. उसे छोड़ दिया जाएगा.
  • अगर ग्राफ़िक ओवरले करने के लिए डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो इनपुट इमेज को चुनने के बाद, पहले एमएल किट से नतीजा पाएं. इसके बाद, इमेज को रेंडर करें और ओवरले को एक ही चरण में पूरा करें. ऐसा करके, डिसप्ले सरफ़ेस पर रेंडर हो जाता है प्रोसेस किए गए हर इनपुट फ़्रेम के लिए, सिर्फ़ एक बार. updatePreviewOverlayViewWithLastFrame देखें उदाहरण के लिए, एमएल किट क्विकस्टार्ट सैंपल में.