在 iOS 上使用 Firebase ML 辨識地標

你可以使用 Firebase ML 辨識圖片中的知名地標。

事前準備

    如果尚未將 Firebase 加入應用程式,請按照下列步驟操作: 《入門指南》中的步驟。

    使用 Swift Package Manager 安裝及管理 Firebase 依附元件。

    1. 在 Xcode 中保持開啟應用程式專案,然後依序選擇 [檔案] >新增套件
    2. 在系統提示時,新增 Firebase Apple 平台 SDK 存放區:
    3.   https://github.com/firebase/firebase-ios-sdk.git
    4. 選擇 Firebase ML 程式庫。
    5. 在目標建構設定的「Other Linker Flags」部分中新增 -ObjC 標記。
    6. 完成後,Xcode 會自動開始解析並下載 複製到背景依附元件

    接著,進行一些應用程式內設定:

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

      Swift

      import FirebaseMLModelDownloader

      Objective-C

      @import FirebaseMLModelDownloader;
  1. 如果您尚未為專案啟用雲端式 API,請先啟用 現在:

    1. 開啟 Firebase ML Firebase 控制台的「API」頁面。
    2. 如果您尚未將專案升級至 Blaze 定價方案,請按一下 如要這麼做,請升級。(只有在您的 專案並未採用 Blaze 方案)。

      只有 Blaze 層級的專案可以使用以雲端為基礎的 API。

    3. 如果尚未啟用雲端式 API,請按一下「Enable Cloud-based API」(啟用雲端式 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;
  

在下一個步驟中,請傳遞 VisionCloudDetectorOptions 物件。

執行地標偵測工具

如要辨識圖片中的地標,請以 UIImageCMSampleBufferRefVisionCloudLandmarkDetectordetect(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];
}

後續步驟