iOS पर Firebase ML की मदद से लैंडमार्क की पहचान करना

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. अगर क्लाउड-आधारित एपीआई पहले से चालू नहीं हैं, तो क्लाउड-आधारित एपीआई चालू करें पर क्लिक करें.

लैंडमार्क डिटेक्टर कॉन्फ़िगर करें

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

Swift

let options = VisionCloudDetectorOptions()
options.modelType = .latest
options.maxResults = 20

Objective-C

  FIRVisionCloudDetectorOptions *options =
      [[FIRVisionCloudDetectorOptions alloc] init];
  options.modelType = FIRVisionCloudModelTypeLatest;
  options.maxResults = 20;
  

अगले चरण में, क्लाउड डिटेक्टर ऑब्जेक्ट बनाते समय VisionCloudDetectorOptions ऑब्जेक्ट पास करें.

लैंडमार्क डिटेक्टर चलाएं

किसी इमेज में लैंडमार्क की पहचान करने के लिए, VisionCloudLandmarkDetector के detect(in:) तरीके में इमेज को UIImage या CMSampleBufferRef के तौर पर पास करें:

  1. VisionCloudLandmarkDetector का इंस्टेंस पाएं:

    Swift

    lazy var vision = Vision.vision()
    
    let cloudDetector = vision.cloudLandmarkDetector(options: options)
    // Or, to use the default settings:
    // let cloudDetector = vision.cloudLandmarkDetector()
    

    Objective-C

    FIRVision *vision = [FIRVision vision];
    FIRVisionCloudLandmarkDetector *landmarkDetector = [vision cloudLandmarkDetector];
    // Or, to change the default settings:
    // FIRVisionCloudLandmarkDetector *landmarkDetector =
    //     [vision cloudLandmarkDetectorWithOptions:options];
    
  2. Cloud Vision को कॉल करने के लिए, इमेज को base64 कोड में बदली गई स्ट्रिंग के तौर पर फ़ॉर्मैट किया जाना चाहिए. UIImage प्रोसेस करने के लिए:

    Swift

    guard let imageData = uiImage.jpegData(compressionQuality: 1.0) else { return }
    let base64encodedImage = imageData.base64EncodedString()

    Objective-C

    NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0f);
    NSString *base64encodedImage =
      [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
  3. इसके बाद, detect(in:) तरीके से इमेज पास करें:

    Swift

    cloudDetector.detect(in: visionImage) { landmarks, error in
      guard error == nil, let landmarks = landmarks, !landmarks.isEmpty else {
        // ...
        return
      }
    
      // Recognized landmarks
      // ...
    }
    

    Objective-C

    [landmarkDetector detectInImage:image
                         completion:^(NSArray<FIRVisionCloudLandmark *> *landmarks,
                                      NSError *error) {
      if (error != nil) {
        return;
      } else if (landmarks != nil) {
        // Got landmarks
      }
    }];
    

जाने-पहचाने लैंडमार्क के बारे में जानकारी पाएं

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

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

Swift

for landmark in landmarks {
  let landmarkDesc = landmark.landmark
  let boundingPoly = landmark.frame
  let entityId = landmark.entityId

  // A landmark can have multiple locations: for example, the location the image
  // was taken, and the location of the landmark depicted.
  for location in landmark.locations {
    let latitude = location.latitude
    let longitude = location.longitude
  }

  let confidence = landmark.confidence
}

Objective-C

for (FIRVisionCloudLandmark *landmark in landmarks) {
   NSString *landmarkDesc = landmark.landmark;
   CGRect frame = landmark.frame;
   NSString *entityId = landmark.entityId;

   // A landmark can have multiple locations: for example, the location the image
   // was taken, and the location of the landmark depicted.
   for (FIRVisionLatitudeLongitude *location in landmark.locations) {
     double latitude = [location.latitude doubleValue];
     double longitude = [location.longitude doubleValue];
   }

   float confidence = [landmark.confidence floatValue];
}

अगले चरण