iOS पर ML किट से इमेज को लेबल करें

किसी इमेज में पहचाने गए ऑब्जेक्ट को लेबल करने के लिए, एमएल किट का इस्तेमाल किया जा सकता है. इसके साथ दिया गया डिफ़ॉल्ट मॉडल ML Kit में 400 से ज़्यादा अलग-अलग लेबल इस्तेमाल किए जा सकते हैं.

इसे आज़माएं

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

  1. अपनी Podfile में, नीचे दिए गए ML Kit पॉड शामिल करें:
    pod 'GoogleMLKit/ImageLabeling', '3.2.0'
    
  2. अपने प्रोजेक्ट के Pods को इंस्टॉल या अपडेट करने के बाद, इसके .xcworkspace. ML Kit, Xcode के 12.4 या इसके बाद के वर्शन पर काम करता है.

अब आप इमेज को लेबल करने के लिए तैयार हैं.

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

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

अगर 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;

अगर 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];

2. इमेज लेबलर को कॉन्फ़िगर करें और चलाएं

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

  1. सबसे पहले, ImageLabeler का इंस्टेंस देखें.

Swift

let labeler = ImageLabeler.imageLabeler()

// Or, to set the minimum confidence required:
// let options = ImageLabelerOptions()
// options.confidenceThreshold = 0.7
// let labeler = ImageLabeler.imageLabeler(options: options)

Objective-C

MLKImageLabeler *labeler = [MLKImageLabeler imageLabeler];

// Or, to set the minimum confidence required:
// MLKImageLabelerOptions *options =
//         [[MLKImageLabelerOptions alloc] init];
// options.confidenceThreshold = 0.7;
// MLKImageLabeler *labeler =
//         [MLKImageLabeler imageLabelerWithOptions:options];
  1. इसके बाद, processImage() तरीके से इमेज पास करें:

Swift

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

    // Task succeeded.
    // ...
}

Objective-C

[labeler processImage:image
completion:^(NSArray *_Nullable labels,
            NSError *_Nullable error) {
   if (error != nil) { return; }

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

3. लेबल किए गए ऑब्जेक्ट के बारे में जानकारी पाना

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

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