Apple प्लैटफ़ॉर्म पर, इमेज को Firebase एमएल की मदद से लेबल करें

किसी इमेज में पहचाने गए ऑब्जेक्ट को लेबल करने के लिए, Firebase ML का इस्तेमाल किया जा सकता है. इस एपीआई की सुविधाओं के बारे में जानने के लिए, खास जानकारी देखें.

वेब कंटेनर इंस्टॉल करने से पहले

    अगर आपने पहले से अपने ऐप्लिकेशन में Firebase नहीं जोड़ा है, तो शुरुआती निर्देश में दिए गए निर्देशों का पालन करके ऐसा करें.

    Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift पैकेज मैनेजर का इस्तेमाल करें.

    1. Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलने के लिए, फ़ाइल > पैकेज जोड़ें पर जाएं.
    2. जब कहा जाए, तब Firebase Apple प्लैटफ़ॉर्म SDK टूल का रिपॉज़िटरी जोड़ें:
    3.   https://github.com/firebase/firebase-ios-sdk.git
    4. Firebase ML लाइब्रेरी चुनें.
    5. अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में -ObjC फ़्लैग जोड़ें.
    6. यह काम पूरा होने के बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी को अपने-आप रिज़ॉल्व और डाउनलोड करना शुरू कर देगा.

    इसके बाद, कुछ इन-ऐप्लिकेशन सेटअप करें:

    1. अपने ऐप्लिकेशन में, Firebase इंपोर्ट करें:

      Swift

      import FirebaseMLModelDownloader

      Objective-C

      @import FirebaseMLModelDownloader;
  1. अगर आपने पहले से अपने प्रोजेक्ट के लिए क्लाउड-आधारित एपीआई चालू नहीं किए हैं, तो अभी करें:

    1. Firebase कंसोल का Firebase ML एपीआई पेज खोलें.
    2. अगर आपने अपने प्रोजेक्ट को पहले से ब्लेज़ प्राइसिंग प्लान में अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें पर क्लिक करें. (अगर आपका प्रोजेक्ट ब्लेज़ प्लान में नहीं है, तो आपको अपग्रेड करने के लिए कहा जाएगा.)

      सिर्फ़ ब्लेज़-लेवल के प्रोजेक्ट ही क्लाउड-आधारित एपीआई का इस्तेमाल कर सकते हैं.

    3. अगर क्लाउड-आधारित एपीआई पहले से चालू नहीं हैं, तो क्लाउड-आधारित एपीआई चालू करें पर क्लिक करें.

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

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

UIImage या CMSampleBufferRef का इस्तेमाल करके, VisionImage ऑब्जेक्ट बनाएं.

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

  1. अगर ज़रूरी हो, तो इमेज को घुमाएं, ताकि उसकी imageOrientation प्रॉपर्टी .up हो.
  2. सही तरीके से घुमाए गए UIImage का इस्तेमाल करके, VisionImage ऑब्जेक्ट बनाएं. कोई भी रोटेशन मेटाडेटा तय न करें—डिफ़ॉल्ट वैल्यू, .topLeft, का इस्तेमाल किया जाना चाहिए.

    Swift

    let image = VisionImage(image: uiImage)

    Objective-C

    FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];

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

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

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

    Swift

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

    Objective-C

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

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

    Swift

    let cameraPosition = AVCaptureDevice.Position.back  // Set to the capture device you used.
    let metadata = VisionImageMetadata()
    metadata.orientation = imageOrientation(
        deviceOrientation: UIDevice.current.orientation,
        cameraPosition: cameraPosition
    )

    Objective-C

    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. CMSampleBufferRef ऑब्जेक्ट और रोटेशन मेटाडेटा का इस्तेमाल करके VisionImage ऑब्जेक्ट बनाएं:

    Swift

    let image = VisionImage(buffer: sampleBuffer)
    image.metadata = metadata

    Objective-C

    FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer];
    image.metadata = metadata;

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

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

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

    Swift

    let labeler = Vision.vision().cloudImageLabeler()
    
    // Or, to set the minimum confidence required:
    // let options = VisionCloudImageLabelerOptions()
    // options.confidenceThreshold = 0.7
    // let labeler = Vision.vision().cloudImageLabeler(options: options)
    

    Objective-C

    FIRVisionImageLabeler *labeler = [[FIRVision vision] cloudImageLabeler];
    
    // Or, to set the minimum confidence required:
    // FIRVisionCloudImageLabelerOptions *options =
    //         [[FIRVisionCloudImageLabelerOptions alloc] init];
    // options.confidenceThreshold = 0.7;
    // FIRVisionImageLabeler *labeler =
    //         [[FIRVision vision] cloudImageLabelerWithOptions:options];
    
  2. इसके बाद, 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<FIRVisionImageLabel *> *_Nullable labels,
                            NSError *_Nullable error) {
                   if (error != nil) { return; }
    
                   // Task succeeded.
                   // ...
               }];
    

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

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

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

Swift

for label in labels {
    let labelText = label.text
    let entityId = label.entityID
    let confidence = label.confidence
}

Objective-C

for (FIRVisionImageLabel *label in labels) {
   NSString *labelText = label.text;
   NSString *entityId = label.entityID;
   NSNumber *confidence = label.confidence;
}

अगले चरण