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 इंपोर्ट करें:
      SwiftObjective-C
      import FirebaseMLModelDownloader
      @import FirebaseMLModelDownloader;
  1. अगर आपने अपने प्रोजेक्ट के लिए पहले से क्लाउड-आधारित एपीआई चालू नहीं किए हैं, तो ऐसा करें अब:

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

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

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

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

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

एक 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;

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

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

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

    SwiftObjective-C
    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)
    
    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() तरीके से इमेज पास करें:

    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) { return; }
    
                   // Task succeeded.
                   // ...
               }];
    

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

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

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

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

अगले चरण

Representing the Firebase brand

Oct 3, 2024 को अपडेट किया गया

Firebase gives you the tools and infrastructure you need to build better mobile and web apps, improve app quality, and grow your business.

Aug 29, 2024 को अपडेट किया गया

Firebase ML lets you add powerful machine learning features to your app with ready-to-use APIs and support for custom model deployment.

Feb 28, 2025 को अपडेट किया गया