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

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

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

    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;
  

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

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

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

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

अगले चरण