在 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. -ObjC 標記新增至目標的建構設定「Other Linker Flags」部分。
    6. 完成後,Xcode 就會自動開始在背景中解析並下載依附元件。

    接下來,請執行一些應用程式內設定:

    1. 在應用程式中匯入 Firebase:

      Swift

      import FirebaseMLModelDownloader

      Objective-C

      @import FirebaseMLModelDownloader;
  1. 如果您尚未為專案啟用雲端 API,請立即啟用:

    1. 開啟 Firebase 控制台的 Firebase ML API 頁面
    2. 如果您尚未將專案升級至 Blaze 定價方案,請按一下「Upgrade」進行升級 (只有在專案未採用 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 物件。

執行地標偵測器

如要辨識圖片中的地標,請將圖片做為 UIImageCMSampleBufferRef 傳遞至 VisionCloudLandmarkDetectordetect(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.0) 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];
}

後續步驟