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 ML खोलें Firebase कंसोल का एपीआई पेज.
    2. अगर आपने पहले से अपने प्रोजेक्ट को Blaze प्राइसिंग प्लान में अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें. (आपको अपग्रेड करने के लिए तभी कहा जाएगा, जब प्रोजेक्ट ब्लेज़ प्लान में नहीं है.)

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

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

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

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

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

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

  1. अगर ज़रूरी हो, तो इमेज को घुमाएं, ताकि इसकी imageOrientation प्रॉपर्टी .up है.
  2. स्क्रीन की दिशा को सही तरीके से घुमाने के लिए, VisionImage ऑब्जेक्ट बनाएं UIImage. कोई भी रोटेशन मेटाडेटा तय न करें—डिफ़ॉल्ट .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. VisionImage ऑब्जेक्ट बनाने के लिए, CMSampleBufferRef ऑब्जेक्ट और रोटेशन मेटाडेटा:

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

अगले चरण

  • Cloud API का इस्तेमाल करने वाले ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, आपको Google Ads के ट्रैफ़िक को रोकने और उसे कम करने के लिए, अनधिकृत एपीआई ऐक्सेस का असर.