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

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

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

  1. अगर आपने पहले से अपने ऐप्लिकेशन में Firebase नहीं जोड़ा है, तो ऐसा करने के लिए शुरुआती निर्देश में दिए गए चरणों को पूरा करें.
  2. अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:
    pod 'Firebase/MLVision', '6.25.0'
    pod 'Firebase/MLVisionAutoML', '6.25.0'
    
    प्रोजेक्ट के Pods को इंस्टॉल या अपडेट करने के बाद, अपना Xcode ज़रूर खोलें प्रोजेक्ट को .xcworkspace का इस्तेमाल करके बनाया गया है.
  3. अपने ऐप्लिकेशन में Firebase इंपोर्ट करें:
    SwiftObjective-C
    import Firebase
    @import Firebase;

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

ML Kit की मदद से, डिवाइस पर AutoML से जनरेट किए गए मॉडल चलाए जा सकते हैं. हालांकि, आपके पास ये विकल्प हैं ML Kit को कॉन्फ़िगर करें, ताकि आप Firebase से रिमोट तरीके से अपना मॉडल लोड कर सकें. इसके लिए, इन कमांड का इस्तेमाल करें: या दोनों का इस्तेमाल कर सकते हैं.

मॉडल को Firebase पर होस्ट करके, मॉडल को रिलीज़ किए बिना अपडेट किया जा सकता है ऐप्लिकेशन का नया वर्शन डाउनलोड किया है और आप Remote Config और A/B Testing का इस्तेमाल इन कामों के लिए कर सकते हैं उपयोगकर्ताओं के अलग-अलग सेट को डाइनैमिक तौर पर अलग-अलग मॉडल दिखाते हैं.

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

मॉडल को अपने ऐप्लिकेशन के साथ बंडल करके, अपने ऐप्लिकेशन की एमएल (मशीन लर्निंग) सुविधाओं को पक्का किया जा सकता है Firebase से होस्ट किया गया मॉडल उपलब्ध न होने पर भी काम करता है.

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

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

SwiftObjective-C
let remoteModel = AutoMLRemoteModel(
    name: "your_remote_model"  // The name you assigned in the Firebase console.
)
FIRAutoMLRemoteModel *remoteModel = [[FIRAutoMLRemoteModel alloc]
    initWithName:@"your_remote_model"];  // The name you assigned in the Firebase console.

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

SwiftObjective-C
let downloadConditions = ModelDownloadConditions(
  allowsCellularAccess: true,
  allowsBackgroundDownloading: true
)

let downloadProgress = ModelManager.modelManager().download(
  remoteModel,
  conditions: downloadConditions
)
FIRModelDownloadConditions *downloadConditions =
    [[FIRModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
                                         allowsBackgroundDownloading:YES];

NSProgress *downloadProgress =
    [[FIRModelManager modelManager] downloadRemoteModel:remoteModel
                                             conditions:downloadConditions];

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

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

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

  1. डाउनलोड किए गए ZIP फ़ॉर्मैट वाले संग्रह से मॉडल और उसका मेटाडेटा निकालें Firebase कंसोल से फ़ोल्डर में:
    your_model_directory
      |____dict.txt
      |____manifest.json
      |____model.tflite
    
    तीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए. हमारा सुझाव है कि आप अपनी फ़ाइलों का इस्तेमाल इस तौर पर करें आपने बिना कोई बदलाव किए उन्हें डाउनलोड कर लिया हो. इसमें फ़ाइल के नाम भी शामिल हैं.
  2. फ़ोल्डर को अपने Xcode प्रोजेक्ट में कॉपी करें. ऐसा करते समय, ऐसा करने पर, फ़ोल्डर के रेफ़रंस बनाएं. मॉडल फ़ाइल और मेटाडेटा ऐप्लिकेशन बंडल में शामिल किया जाएगा और ML किट में उपलब्ध होगा.
  3. AutoMLLocalModel ऑब्जेक्ट बनाएं, जिसमें मॉडल मेनिफ़ेस्ट का पाथ बताया गया हो फ़ाइल:
    SwiftObjective-C
    guard let manifestPath = Bundle.main.path(
        forResource: "manifest",
        ofType: "json",
        inDirectory: "your_model_directory"
    ) else { return true }
    let localModel = AutoMLLocalModel(manifestPath: manifestPath)
    
    NSString *manifestPath = [NSBundle.mainBundle pathForResource:@"manifest"
                                                           ofType:@"json"
                                                      inDirectory:@"your_model_directory"];
    FIRAutoMLLocalModel *localModel = [[FIRAutoMLLocalModel alloc] initWithManifestPath:manifestPath];
    

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

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

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

SwiftObjective-C
let options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
options.confidenceThreshold = 0  // Evaluate your model in the Firebase console
                                 // to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: options)
FIRVisionOnDeviceAutoMLImageLabelerOptions *options =
    [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
options.confidenceThreshold = 0;  // Evaluate your model in the Firebase console
                                  // to determine an appropriate value.
FIRVisionImageLabeler *labeler =
    [[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];

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

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

SwiftObjective-C
var options: VisionOnDeviceAutoMLImageLabelerOptions?
if (ModelManager.modelManager().isModelDownloaded(remoteModel)) {
  options = VisionOnDeviceAutoMLImageLabelerOptions(remoteModel: remoteModel)
} else {
  options = VisionOnDeviceAutoMLImageLabelerOptions(localModel: localModel)
}
options.confidenceThreshold = 0  // Evaluate your model in the Firebase console
                                 // to determine an appropriate value.
let labeler = Vision.vision().onDeviceAutoMLImageLabeler(options: options)
VisionOnDeviceAutoMLImageLabelerOptions *options;
if ([[FIRModelManager modelManager] isModelDownloaded:remoteModel]) {
  options = [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
  options = [[FIRVisionOnDeviceAutoMLImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = 0.0f;  // Evaluate your model in the Firebase console
                                     // to determine an appropriate value.
FIRVisionImageLabeler *labeler = [[FIRVision vision] onDeviceAutoMLImageLabelerWithOptions:options];

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

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

SwiftObjective-C
NotificationCenter.default.addObserver(
    forName: .firebaseMLModelDownloadDidSucceed,
    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: .firebaseMLModelDownloadDidFail,
    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:FIRModelDownloadDidSucceedNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              FIRRemoteModel *model = note.userInfo[FIRModelDownloadUserInfoKeyRemoteModel];
              if ([model.name isEqualToString:@"your_remote_model"]) {
                // The model was downloaded and is available on the device
              }
            }];

[NSNotificationCenter.defaultCenter
    addObserverForName:FIRModelDownloadDidFailNotification
                object:nil
                 queue:nil
            usingBlock:^(NSNotification *_Nonnull note) {
              if (weakSelf == nil | note.userInfo == nil) {
                return;
              }
              __strong typeof(self) strongSelf = weakSelf;

              NSError *error = note.userInfo[FIRModelDownloadUserInfoKeyError];
            }];

2. इनपुट इमेज तैयार करें

इसके बाद, हर उस इमेज के लिए जिसे आप लेबल करना चाहते हैं, एक VisionImage ऑब्जेक्ट का इस्तेमाल करके विकल्प को चुनना होगा और इसे VisionImageLabeler (अगले सेक्शन में बताया गया है).

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

UIImage का इस्तेमाल करने के लिए:

  1. अगर ज़रूरी हो, तो इमेज को घुमाएं, ताकि इसकी imageOrientation प्रॉपर्टी .up है.
  2. स्क्रीन की दिशा को सही तरीके से घुमाने के लिए, VisionImage ऑब्जेक्ट बनाएं UIImage. कोई भी रोटेशन मेटाडेटा तय न करें—डिफ़ॉल्ट .topLeft वैल्यू का इस्तेमाल करना ज़रूरी है.
    SwiftObjective-C
    let image = VisionImage(image: uiImage)
    FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];

CMSampleBufferRef का इस्तेमाल करने के लिए:

  1. एक VisionImageMetadata ऑब्जेक्ट बनाएं, जो में शामिल इमेज डेटा का ओरिएंटेशन CMSampleBufferRef बफ़र.

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

    SwiftObjective-C
    func imageOrientation(
        deviceOrientation: UIDeviceOrientation,
        cameraPosition: AVCaptureDevice.Position
        ) -> VisionDetectorImageOrientation {
        switch deviceOrientation {
        case .portrait:
            return cameraPosition == .front ? .leftTop : .rightTop
        case .landscapeLeft:
            return cameraPosition == .front ? .bottomLeft : .topLeft
        case .portraitUpsideDown:
            return cameraPosition == .front ? .rightBottom : .leftBottom
        case .landscapeRight:
            return cameraPosition == .front ? .topRight : .bottomRight
        case .faceDown, .faceUp, .unknown:
            return .leftTop
        }
    }
    - (FIRVisionDetectorImageOrientation)
        imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                               cameraPosition:(AVCaptureDevicePosition)cameraPosition {
      switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationLeftTop;
          } else {
            return FIRVisionDetectorImageOrientationRightTop;
          }
        case UIDeviceOrientationLandscapeLeft:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationBottomLeft;
          } else {
            return FIRVisionDetectorImageOrientationTopLeft;
          }
        case UIDeviceOrientationPortraitUpsideDown:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationRightBottom;
          } else {
            return FIRVisionDetectorImageOrientationLeftBottom;
          }
        case UIDeviceOrientationLandscapeRight:
          if (cameraPosition == AVCaptureDevicePositionFront) {
            return FIRVisionDetectorImageOrientationTopRight;
          } else {
            return FIRVisionDetectorImageOrientationBottomRight;
          }
        default:
          return FIRVisionDetectorImageOrientationTopLeft;
      }
    }

    इसके बाद, मेटाडेटा ऑब्जेक्ट बनाएं:

    SwiftObjective-C
    let cameraPosition = AVCaptureDevice.Position.back  // Set to the capture device you used.
    let metadata = VisionImageMetadata()
    metadata.orientation = imageOrientation(
        deviceOrientation: UIDevice.current.orientation,
        cameraPosition: cameraPosition
    )
    FIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init];
    AVCaptureDevicePosition cameraPosition =
        AVCaptureDevicePositionBack;  // Set to the capture device you used.
    metadata.orientation =
        [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                     cameraPosition:cameraPosition];
  2. VisionImage ऑब्जेक्ट बनाने के लिए, CMSampleBufferRef ऑब्जेक्ट और रोटेशन मेटाडेटा:
    SwiftObjective-C
    let image = VisionImage(buffer: sampleBuffer)
    image.metadata = metadata
    FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer];
    image.metadata = metadata;

3. इमेज लेबलर चलाएं

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

SwiftObjective-C
labeler.process(image) { labels, error in
    guard error == nil, let labels = labels else { return }

    // Task succeeded.
    // ...
}
[labeler
    processImage:image
      completion:^(NSArray<FIRVisionImageLabel *> *_Nullable labels, NSError *_Nullable error) {
        if (error != nil || labels == nil) {
          return;
        }

        // Task succeeded.
        // ...
      }];

इमेज को लेबल करने की प्रोसेस पूरी होने पर, VisionImageLabel ऑब्जेक्ट का कलेक्शन होगा पूरा होने के हैंडलर को भेजा जाता है. हर ऑब्जेक्ट से, आपको यह जानकारी मिल सकती है किसी सुविधा के बारे में जानकारी होती है जिसे इमेज में पहचाना गया है.

उदाहरण के लिए:

SwiftObjective-C
for label in labels {
    let labelText = label.text
    let confidence = label.confidence
}
for (FIRVisionImageLabel *label in labels) {
  NSString *labelText = label.text;
  NSNumber *confidence = label.confidence;
}

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

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