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

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

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

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

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

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

SwiftObjective-C
let options = VisionCloudDetectorOptions()
options.modelType = .latest
options.maxResults = 20
  FIRVisionCloudDetectorOptions *options =
      [[FIRVisionCloudDetectorOptions alloc] init];
  options.modelType = FIRVisionCloudModelTypeLatest;
  options.maxResults = 20;
  

अगले चरण में, VisionCloudDetectorOptions को पास करें ऑब्जेक्ट की पहचान करती है.

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

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

  1. VisionCloudLandmarkDetector का इंस्टेंस पाएं:
    SwiftObjective-C
    lazy var vision = Vision.vision()
    
    let cloudDetector = vision.cloudLandmarkDetector(options: options)
    // Or, to use the default settings:
    // let cloudDetector = vision.cloudLandmarkDetector()
    FIRVision *vision = [FIRVision vision];
    FIRVisionCloudLandmarkDetector *landmarkDetector = [vision cloudLandmarkDetector];
    // Or, to change the default settings:
    // FIRVisionCloudLandmarkDetector *landmarkDetector =
    //     [vision cloudLandmarkDetectorWithOptions:options];
  2. Cloud Vision को कॉल करने के लिए, इमेज को base64-एन्कोडेड के रूप में फ़ॉर्मैट किया जाना चाहिए स्ट्रिंग. UIImage प्रोसेस करने के लिए:
    SwiftObjective-C
    guard let imageData = uiImage.jpegData(compressionQuality: 1.0) else { return }
    let base64encodedImage = imageData.base64EncodedString()
    NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0f);
    NSString *base64encodedImage =
      [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
  3. इसके बाद, detect(in:) तरीके से इमेज पास करें:
    SwiftObjective-C
    cloudDetector.detect(in: visionImage) { landmarks, error in
      guard error == nil, let landmarks = landmarks, !landmarks.isEmpty else {
        // ...
        return
      }
    
      // Recognized landmarks
      // ...
    }
    [landmarkDetector detectInImage:image
                         completion:^(NSArray<FIRVisionCloudLandmark *> *landmarks,
                                      NSError *error) {
      if (error != nil) {
        return;
      } else if (landmarks != nil) {
        // Got landmarks
      }
    }];

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

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

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

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

अगले चरण

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

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

Developer documentation for Firebase

Nov 9, 2024 को अपडेट किया गया