iOS'te Makine Öğrenimi Kiti ile Önemli Noktaları Tanıma

Görüntülerdeki tanınmış önemli noktaları tanımak için Makine Öğrenimi Kiti'ni kullanabilirsiniz.

Başlamadan önce

  1. Firebase'i uygulamanıza henüz eklemediyseniz başlangıç kılavuzundaki adımlara bakın.
  2. ML Kit kitaplıklarını Podfile'ınıza ekleyin:
    pod 'Firebase/MLVision', '6.25.0'
    
    . Projenizin kapsüllerini yükledikten veya güncelledikten sonra Xcode projenin yaşam döngüsünü yürütmeniz gerekir..xcworkspace
  3. Uygulamanızda Firebase'i içe aktarın:

    Swift

    import Firebase

    Objective-C

    @import Firebase;
  4. Projeniz için Cloud tabanlı API'leri henüz etkinleştirmediyseniz etkinleştirin şimdi:

    1. ML Kit'i açın Firebase konsolunun API'leri sayfasında gösterilir.
    2. Projenizi daha önce Blaze fiyatlandırma planına yükseltmediyseniz Bunun için yeni sürüme geçin. (Yalnızca emin olun.)

      Bulut tabanlı API'ler yalnızca Blaze düzeyindeki projelerde kullanılabilir.

    3. Cloud tabanlı API'ler henüz etkinleştirilmemişse Bulut tabanlı API'leri etkinleştir'i tıklayın. API'ler.
    ziyaret edin.

Önemli nokta algılayıcıyı yapılandırma

Cloud algılayıcısı varsayılan olarak modelin kararlı sürümünü kullanır ve en fazla 10 sonuç döndürür. Bu ayarlardan herhangi birini değiştirmek isterseniz bunları bir VisionCloudDetectorOptions nesnesiyle aşağıdaki örnekte:

Swift

let options = VisionCloudDetectorOptions()
options.modelType = .latest
options.maxResults = 20

Objective-C

  FIRVisionCloudDetectorOptions *options =
      [[FIRVisionCloudDetectorOptions alloc] init];
  options.modelType = FIRVisionCloudModelTypeLatest;
  options.maxResults = 20;
  

Sonraki adımda VisionCloudDetectorOptions ayarını geçin nesnesini tanımlayın.

Önemli nokta algılayıcıyı çalıştırma

Bir görüntüdeki önemli noktaları tanımak için resmi UIImage veya bir CMSampleBufferRef - VisionCloudLandmarkDetector detect(in:) yöntem:

  1. VisionCloudLandmarkDetector öğesinin bir örneğini alın:

    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 veya UIImage kullanarak bir VisionImage nesnesi oluşturun CMSampleBufferRef.

    UIImage kullanmak için:

    1. Gerekirse resmi, imageOrientation özellik değeri .up.
    2. Doğru şekilde döndürülen öğeyi kullanarak bir VisionImage nesnesi oluşturun UIImage. Herhangi bir rotasyon meta verisi belirtme (varsayılan) değeri (.topLeft) kullanılmalıdır.

      Swift

      let image = VisionImage(image: uiImage)

      Objective-C

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

    CMSampleBufferRef kullanmak için:

    1. Aşağıdakini belirten bir VisionImageMetadata nesnesi oluşturun: içindeki resim verilerinin yönünü CMSampleBufferRef arabellek.

      Resmin yönünü öğrenmek için:

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

      Ardından meta veri nesnesini oluşturun:

      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. Şunu kullanarak bir VisionImage nesnesi oluşturun: CMSampleBufferRef nesnesi ve rotasyon meta verileri:

      Swift

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

      Objective-C

      FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer];
      image.metadata = metadata;
  3. Ardından resmi detect(in:) yöntemine iletin:

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

Tanınan önemli noktalar hakkında bilgi alın

Önemli nokta tanıma işlemi başarılı olursa bir VisionCloudLandmark dizisi nesneler tamamlama işleyiciye aktarılır. Her nesneden bu şekilde resimde tanınan bir önemli nokta hakkında bilgi

Örneğin:

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

Sonraki adımlar