आईओएस पर एमएल किट के साथ लैंडमार्क पहचानें

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

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

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

    तीव्र

    import Firebase

    उद्देश्य सी

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

    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. UIImage या CMSampleBufferRef उपयोग करके एक VisionImage ऑब्जेक्ट बनाएं।

    UIImage उपयोग करने के लिए:

    1. यदि आवश्यक हो, तो छवि को घुमाएँ ताकि उसकी imageOrientation गुण .up हो।
    2. सही ढंग से घुमाए गए UIImage उपयोग करके एक VisionImage ऑब्जेक्ट बनाएं। कोई रोटेशन मेटाडेटा निर्दिष्ट न करें—डिफ़ॉल्ट मान, .topLeft , का उपयोग किया जाना चाहिए।

      तीव्र

      let image = VisionImage(image: uiImage)

      उद्देश्य सी

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

    CMSampleBufferRef का उपयोग करने के लिए:

    1. एक VisionImageMetadata ऑब्जेक्ट बनाएं जो CMSampleBufferRef बफर में मौजूद छवि डेटा के ओरिएंटेशन को निर्दिष्ट करता है।

      छवि अभिविन्यास प्राप्त करने के लिए:

      तीव्र

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

      उद्देश्य सी

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

      फिर, मेटाडेटा ऑब्जेक्ट बनाएं:

      तीव्र

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

      उद्देश्य सी

      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. CMSampleBufferRef ऑब्जेक्ट और रोटेशन मेटाडेटा का उपयोग करके एक VisionImage ऑब्जेक्ट बनाएं:

      तीव्र

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

      उद्देश्य सी

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

अगले कदम