Firebase is back at Google I/O on May 10! Register now

आईओएस पर फायरबेस एमएल के साथ लैंडमार्क्स को पहचानें

संग्रह की मदद से व्यवस्थित रहें अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.

आप किसी इमेज में जाने-माने लैंडमार्क की पहचान करने के लिए Firebase ML का इस्तेमाल कर सकते हैं.

शुरू करने से पहले

    यदि आपने पहले से ही अपने ऐप में Firebase नहीं जोड़ा है, तो आरंभ करने की मार्गदर्शिका में दिए गए चरणों का पालन करके ऐसा करें।

    फायरबेस निर्भरताओं को स्थापित और प्रबंधित करने के लिए स्विफ्ट पैकेज मैनेजर का उपयोग करें।

    1. Xcode में, अपने ऐप प्रोजेक्ट को खोलने के साथ, File > Add Packages पर नेविगेट करें।
    2. संकेत दिए जाने पर, Firebase Apple प्लेटफ़ॉर्म SDK रिपॉजिटरी जोड़ें:
    3.   https://github.com/firebase/firebase-ios-sdk
    4. फायरबेस एमएल लाइब्रेरी चुनें।
    5. समाप्त होने पर, Xcode स्वचालित रूप से पृष्ठभूमि में आपकी निर्भरताओं को हल करना और डाउनलोड करना शुरू कर देगा।

    अगला, कुछ इन-ऐप सेटअप करें:

    1. अपने ऐप में, फायरबेस आयात करें:

      तीव्र

      import FirebaseMLModelDownloader

      उद्देश्य सी

      @import FirebaseMLModelDownloader;
  1. यदि आपने अपने प्रोजेक्ट के लिए क्लाउड-आधारित API को पहले से सक्षम नहीं किया है, तो अभी ऐसा करें:

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

      केवल ब्लेज़-स्तरीय परियोजनाएँ ही क्लाउड-आधारित API का उपयोग कर सकती हैं।

    3. यदि क्लाउड-आधारित API पहले से सक्षम नहीं हैं, तो क्लाउड-आधारित API सक्षम करें पर क्लिक करें

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

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

तीव्र

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 का उदाहरण प्राप्त करें:

    तीव्र

    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. क्लाउड विजन को कॉल करने के लिए, छवि को बेस 64-एन्कोडेड स्ट्रिंग के रूप में स्वरूपित किया जाना चाहिए। UIImage को संसाधित करने के लिए:

    तीव्र

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

    उद्देश्य सी

    NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0f);
    NSString *base64encodedImage =
      [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
  3. फिर, इमेज को detect(in:) मेथड में पास करें:

    तीव्र

    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 ऑब्जेक्ट्स की एक सरणी पूर्णता हैंडलर को पास कर दी जाएगी। प्रत्येक वस्तु से, आप छवि में पहचाने गए एक लैंडमार्क के बारे में जानकारी प्राप्त कर सकते हैं।

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

तीव्र

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

अगले कदम