iOS에서 Firebase ML을 사용하여 랜드마크 인식

Firebase ML을 사용하여 이미지 속 유명 랜드마크를 인식할 수 있습니다.

시작하기 전에

    앱에 Firebase를 아직 추가하지 않았다면 시작 가이드의 단계에 따라 추가합니다.

    Swift Package Manager를 사용해 Firebase 종속 항목을 설치하고 관리하세요.

    1. 앱 프로젝트를 연 상태로 Xcode에서 File(파일) > Add Packages(패키지 추가)로 이동합니다.
    2. 메시지가 표시되면 Firebase Apple 플랫폼 SDK 저장소를 추가합니다.
    3.   https://github.com/firebase/firebase-ios-sdk.git
    4. Firebase ML 라이브러리를 선택합니다.
    5. 완료되면 Xcode가 백그라운드에서 자동으로 종속 항목을 확인하고 다운로드하기 시작합니다.

    그런 다음 몇 가지 인앱 설정을 수행합니다.

    1. 앱에서 Firebase를 가져옵니다.

      Swift

      import FirebaseMLModelDownloader

      Objective-C

      @import FirebaseMLModelDownloader;
  1. 프로젝트에 클라우드 기반 API를 아직 사용 설정하지 않았으면 지금 설정하세요.

    1. Firebase Console의 Firebase ML API 페이지를 엽니다.
    2. 프로젝트를 Blaze 요금제로 아직 업그레이드하지 않은 경우 업그레이드를 클릭하여 업그레이드하세요. 프로젝트가 Blaze 요금제가 아닌 경우에만 업그레이드하라는 메시지가 표시됩니다.

      Blaze 수준 프로젝트만 클라우드 기반 API를 사용할 수 있습니다.

    3. 클라우드 기반 API가 아직 사용 설정되지 않은 경우 클라우드 기반 API 사용 설정을 클릭합니다.

랜드마크 감지기 구성

기본적으로 Cloud 감지기에서는 모델의 정식 버전을 사용해 최대 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;
  

다음 단계에서 Cloud 감지기 객체를 만들 때 VisionCloudDetectorOptions 객체를 전달합니다.

랜드마크 감지기 실행

이미지 속 랜드마크를 인식하려면 이미지를 UIImage 또는 CMSampleBufferRefVisionCloudLandmarkDetectordetect(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. Cloud Vision을 호출하려면 이미지의 형식을 base64로 인코딩된 문자열로 지정해야 합니다. UIImage를 처리하려면 다음 안내를 따르세요.

    Swift

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

    Objective-C

    NSData *imageData = UIImageJPEGRepresentation(uiImage, 1.0f);
    NSString *base64encodedImage =
      [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
  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];
}

다음 단계