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

आप किसी छवि में प्रसिद्ध स्थलों को पहचानने के लिए फायरबेस एमएल का उपयोग कर सकते हैं।

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

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

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

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

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

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

      तीव्र

      import FirebaseMLModelDownloader

      उद्देश्य सी

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

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

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

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

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

डिफ़ॉल्ट रूप से, क्लाउड डिटेक्टर मॉडल के स्थिर संस्करण का उपयोग करता है और 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.0) 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];
}

अगले कदम