iOS पर एमएल किट की मदद से लैंडमार्क की पहचान करना

एमएल किट का इस्तेमाल करके, किसी इमेज में मौजूद मशहूर लैंडमार्क को पहचाना जा सकता है.

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

  1. अगर आपने पहले से अपने ऐप्लिकेशन में Firebase नहीं जोड़ा है, तो ऐसा करने के लिए शुरुआती निर्देश में दिए गए चरणों को पूरा करें.
  2. अपनी Podfile में ML Kit लाइब्रेरी शामिल करें:
    pod 'Firebase/MLVision', '6.25.0'
    
    अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है प्रोजेक्ट के Pods को इंस्टॉल या अपडेट करने के बाद, अपना Xcode ज़रूर खोलें प्रोजेक्ट के .xcworkspace का इस्तेमाल करके बनाया गया है.
  3. अपने ऐप्लिकेशन में, Firebase इंपोर्ट करें:

    Swift

    import Firebase

    Objective-C

    @import Firebase;
  4. अगर आपने अपने प्रोजेक्ट के लिए पहले से क्लाउड-आधारित एपीआई चालू नहीं किए हैं, तो ऐसा करें अब:

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

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

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

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

क्लाउड डिटेक्टर, डिफ़ॉल्ट रूप से इस मॉडल के स्टेबल वर्शन का इस्तेमाल करता है और 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 को पास करें ऑब्जेक्ट की पहचान करती है.

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

किसी इमेज में लैंडमार्क की पहचान करने के लिए, इमेज को 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. UIImage याVisionImage CMSampleBufferRef.

    UIImage का इस्तेमाल करने के लिए:

    1. अगर ज़रूरी हो, तो इमेज को घुमाएं, ताकि इसकी imageOrientation प्रॉपर्टी .up है.
    2. स्क्रीन की दिशा को सही तरीके से घुमाने के लिए, VisionImage ऑब्जेक्ट बनाएं UIImage. कोई भी रोटेशन मेटाडेटा तय न करें—डिफ़ॉल्ट .topLeft वैल्यू का इस्तेमाल करना ज़रूरी है.

      Swift

      let image = VisionImage(image: uiImage)

      Objective-C

      FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];

    CMSampleBufferRef का इस्तेमाल करने के लिए:

    1. एक VisionImageMetadata ऑब्जेक्ट बनाएं, जो में शामिल इमेज डेटा का ओरिएंटेशन CMSampleBufferRef बफ़र.

      इमेज का ओरिएंटेशन पाने के लिए:

      Swift

      func imageOrientation(
          deviceOrientation: UIDeviceOrientation,
          cameraPosition: AVCaptureDevice.Position
          ) -> VisionDetectorImageOrientation {
          switch deviceOrientation {
          case .portrait:
              return cameraPosition == .front ? .leftTop : .rightTop
          case .landscapeLeft:
              return cameraPosition == .front ? .bottomLeft : .topLeft
          case .portraitUpsideDown:
              return cameraPosition == .front ? .rightBottom : .leftBottom
          case .landscapeRight:
              return cameraPosition == .front ? .topRight : .bottomRight
          case .faceDown, .faceUp, .unknown:
              return .leftTop
          }
      }

      Objective-C

      - (FIRVisionDetectorImageOrientation)
          imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation
                                 cameraPosition:(AVCaptureDevicePosition)cameraPosition {
        switch (deviceOrientation) {
          case UIDeviceOrientationPortrait:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationLeftTop;
            } else {
              return FIRVisionDetectorImageOrientationRightTop;
            }
          case UIDeviceOrientationLandscapeLeft:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationBottomLeft;
            } else {
              return FIRVisionDetectorImageOrientationTopLeft;
            }
          case UIDeviceOrientationPortraitUpsideDown:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationRightBottom;
            } else {
              return FIRVisionDetectorImageOrientationLeftBottom;
            }
          case UIDeviceOrientationLandscapeRight:
            if (cameraPosition == AVCaptureDevicePositionFront) {
              return FIRVisionDetectorImageOrientationTopRight;
            } else {
              return FIRVisionDetectorImageOrientationBottomRight;
            }
          default:
            return FIRVisionDetectorImageOrientationTopLeft;
        }
      }

      इसके बाद, मेटाडेटा ऑब्जेक्ट बनाएं:

      Swift

      let cameraPosition = AVCaptureDevice.Position.back  // Set to the capture device you used.
      let metadata = VisionImageMetadata()
      metadata.orientation = imageOrientation(
          deviceOrientation: UIDevice.current.orientation,
          cameraPosition: cameraPosition
      )

      Objective-C

      FIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init];
      AVCaptureDevicePosition cameraPosition =
          AVCaptureDevicePositionBack;  // Set to the capture device you used.
      metadata.orientation =
          [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                       cameraPosition:cameraPosition];
    2. VisionImage ऑब्जेक्ट बनाने के लिए, CMSampleBufferRef ऑब्जेक्ट और रोटेशन मेटाडेटा:

      Swift

      let image = VisionImage(buffer: sampleBuffer)
      image.metadata = metadata

      Objective-C

      FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer];
      image.metadata = metadata;
  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];
}

अगले चरण

  • Cloud API का इस्तेमाल करने वाले ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, आपको Google Ads के ट्रैफ़िक को रोकने और उसे कम करने के लिए, अनधिकृत एपीआई ऐक्सेस का असर.