在 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標誌新增至目標建置設定的「其他連結器標誌」部分。
    6. 完成後,Xcode 將自動開始在背景解析並下載您的依賴項。

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

    1. 在您的應用程式中,導入 Firebase:

      迅速

      import FirebaseMLModelDownloader

      Objective-C

      @import FirebaseMLModelDownloader;
  1. 如果您尚未為您的專案啟用基於雲端的 API,請立即執行此操作:

    1. 開啟 Firebase 控制台的Firebase ML API 頁面
    2. 如果您尚未將項目升級到 Blaze 定價計劃,請按一下升​​級來執行此操作。 (只有當您的專案不在 Blaze 計劃中時,系統才會提示您升級。)

      只有 Blaze 等級的項目才能使用基於雲端的 API。

    3. 如果尚未啟用基於雲端的 API,請按一下啟用基於雲端的 API

配置地標檢測器

預設情況下,雲端偵測器使用模型的穩定版本並傳回最多 10 個結果。如果要變更其中任一設置,請使用VisionCloudDetectorOptions物件指定它們,如下例所示:

迅速

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

Objective-C

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

在下一個步驟中,在建立雲端偵測器物件時傳遞VisionCloudDetectorOptions物件。

運行地標檢測器

若要辨識影像中的地標,請將影像作為UIImageCMSampleBufferRef傳遞給VisionCloudLandmarkDetectordetect(in:)方法:

  1. 取得VisionCloudLandmarkDetector的實例:

    迅速

    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

    迅速

    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:)方法:

    迅速

    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物件的陣列將會傳遞到完成處理程序。從每個物件中,您可以獲得有關圖像中識別的地標的資訊。

例如:

迅速

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

下一步