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

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

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

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

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

    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. अगर आपने अपने प्रोजेक्ट को पहले से ही इस्तेमाल के हिसाब से पैसे चुकाने वाले ब्लेज़ प्लान पर अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें पर क्लिक करें. (आपको अपग्रेड करने के लिए सिर्फ़ तब कहा जाएगा, जब आपका प्रोजेक्ट, ब्लेज़ प्लान पर न हो.)

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

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

अगले चरण