Recognize Landmarks with Firebase ML on iOS

You can use Firebase ML to recognize well-known landmarks in an image.

Before you begin

    If you have not already added Firebase to your app, do so by following the steps in the getting started guide.

    Use Swift Package Manager to install and manage Firebase dependencies.

    1. In Xcode, with your app project open, navigate to File> Add Packages.
    2. When prompted, add the Firebase Apple platforms SDK repository:
    3.  https://github.com/firebase/firebase-ios-sdk.git
    4. Choose the Firebase ML library.
    5. Add the -ObjC flag to the Other Linker Flags section of your target's build settings.
    6. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.

    Next, perform some in-app setup:

    1. In your app, import Firebase:

      Swift

      importFirebaseMLModelDownloader

      Objective-C

      @importFirebaseMLModelDownloader;
  1. If you haven't already enabled Cloud-based APIs for your project, do so now:

    1. Open the Firebase ML APIs page in the Firebase console.
    2. If you haven't already upgraded your project to the pay-as-you-go Blaze pricing plan, click Upgrade to do so. (You'll be prompted to upgrade only if your project isn't on the Blaze pricing plan.)

      Only projects on the Blaze pricing plan can use Cloud-based APIs.

    3. If Cloud-based APIs aren't already enabled, click Enable Cloud-based APIs.

Configure the landmark detector

By default, the Cloud detector uses the stable version of the model and returns up to 10 results. If you want to change either of these settings, specify them with a VisionCloudDetectorOptions object as in the following example:

Swift

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

Objective-C

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

In the next step, pass the VisionCloudDetectorOptions object when you create the Cloud detector object.

Run the landmark detector

To recognize landmarks in an image, pass the image as a UIImage or a CMSampleBufferRef to the VisionCloudLandmarkDetector's detect(in:) method:

  1. Get an instance of VisionCloudLandmarkDetector:

    Swift

    lazyvarvision=Vision.vision()
    letcloudDetector=vision.cloudLandmarkDetector(options:options)
    // Or, to use the default settings:
    // let cloudDetector = vision.cloudLandmarkDetector()

    Objective-C

    FIRVision*vision=[FIRVisionvision];
    FIRVisionCloudLandmarkDetector*landmarkDetector=[visioncloudLandmarkDetector];
    // Or, to change the default settings:
    // FIRVisionCloudLandmarkDetector *landmarkDetector =
    // [vision cloudLandmarkDetectorWithOptions:options];
  2. In order to call Cloud Vision, the image must be formatted as a base64-encoded string. To process a UIImage:

    Swift

    guardletimageData=uiImage.jpegData(compressionQuality:1.0)else{return}
    letbase64encodedImage=imageData.base64EncodedString()

    Objective-C

    NSData*imageData=UIImageJPEGRepresentation(uiImage,1.0f);
    NSString*base64encodedImage=
    [imageDatabase64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
  3. Then, pass the image to the detect(in:) method:

    Swift

    cloudDetector.detect(in:visionImage){landmarks,errorin
    guarderror==nil,letlandmarks=landmarks,!landmarks.isEmptyelse{
    // ...
    return
    }
    // Recognized landmarks
    // ...
    }

    Objective-C

    [landmarkDetectordetectInImage:image
    completion:^(NSArray<FIRVisionCloudLandmark*>*landmarks,
    NSError*error){
    if(error!=nil){
    return;
    }elseif(landmarks!=nil){
    // Got landmarks
    }
    }];

Get information about the recognized landmarks

If landmark recognition succeeds, an array of VisionCloudLandmark objects will be passed to the completion handler. From each object, you can get information about a landmark recognized in the image.

For example:

Swift

forlandmarkinlandmarks{
letlandmarkDesc=landmark.landmark
letboundingPoly=landmark.frame
letentityId=landmark.entityId
// A landmark can have multiple locations: for example, the location the image
// was taken, and the location of the landmark depicted.
forlocationinlandmark.locations{
letlatitude=location.latitude
letlongitude=location.longitude
}
letconfidence=landmark.confidence
}

Objective-C

for(FIRVisionCloudLandmark*landmarkinlandmarks){
NSString*landmarkDesc=landmark.landmark;
CGRectframe=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*locationinlandmark.locations){
doublelatitude=[location.latitudedoubleValue];
doublelongitude=[location.longitudedoubleValue];
}
floatconfidence=[landmark.confidencefloatValue];
}

Next steps

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月31日 UTC.