Firebase ML Kit 101 : Landmark Recognition

Hitanshu Dhawan
AndroIDIOTS
Published in
4 min readNov 20, 2018

Landmark Recognition is the process of recognising popular landmarks in an image.

Many social media giants use this landmark recognition technology in their apps to know and understand their users better. It also helps them to provide a unique and personalised experience to their users. Hence, improving the overall user experience.

With ML Kit’s Landmark Recognition API, you can get the landmarks that were recognised in the image along with their geographic coordinates.

Firebase ML Kit Series

In this series of articles, we will deep dive into different ML Kit APIs that it offers…

Let’s look into the ML Kit’s Landmark Recognition API and how we can integrate it into our apps.

ML Kit’s Landmark Recognition

ML Kit’s Landmark Recognition API is a cloud-based API and requires your firebase project to be upgraded to the Blaze plan.

With ML Kit’s Landmark Recognition API you can get the following information about the landmarks from an image.

image courtesy: https://firebase.google.com/docs/ml-kit/recognize-landmarks

Note: Firebase ML Kit is in beta as of January ‘19.

Let’s Code!

Step 1 : Add Firebase to your app

Offcourse! You can add Firebase to your app by following the steps mentioned here.

Step 2 : Include the dependency

You need to include the ML Kit dependency in your app-level build.gradle file.

dependencies {
// ...
implementation 'com.google.firebase:firebase-ml-vision:19.0.2'
}

Step 3 : Get! — the Image

ML Kit provides an easy way to recognise landmarks from variety of image types like Bitmap, media.Image, ByteBuffer, byte[], or a file on the device. You just need to create a FirebaseVisionImage object from the above mentioned image types and pass it to the model.

creating FirebaseVisionImage object from different image types

In my sample app I’ve used a Bitmap image to create a FirebaseVisionImage object.

val image = FirebaseVisionImage.fromBitmap(bitmap)

To create FirebaseVisionImage object from other image types, please refer to the official documentation.

Step 4 : Set! — the Model

Now, It’s time to prepare our Landmark Recognition model.

val detector = FirebaseVision.getInstance().visionCloudLandmarkDetector

By default, the landmark recognition API uses the STABLE version of the model and returns not more than 10 landmarks.

You can change this configuration by passing in an object of FirebaseVisionCloudDetectorOptions to the landmark recognition model.

val options = FirebaseVisionCloudDetectorOptions.Builder()
.setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
.setMaxResults(5)
.build()

val detector = FirebaseVision.getInstance().getVisionCloudLandmarkDetector(options)

Step 5 : Gooo!

Finally, we can pass our image to the model for Landmark Recognition.

detector.detectInImage(image)
.addOnSuccessListener {
//
Task completed successfully
}
.addOnFailureListener {
//
Task failed with an exception
}

Step 6 : Extract the information

Voilà! That’s it!
If the landmark recognition was successful, the success listener will receive a list of FirebaseVisionCloudLandmark objects.
Each FirebaseVisionCloudLandmark object represents a landmark that was recognised and contains all the information related to it.

You can extract all this information like this.

Have a Look!

This is what you can achieve with ML Kit’s Landmark Recognition API.

The full source-code with other ML Kit APIs can be found here!

Thanks for reading! Share this article if you found it useful.
Please do Clap 👏 to show some love :)

Let’s become friends on LinkedIn, GitHub, Facebook, Twitter.

--

--

Hitanshu Dhawan
AndroIDIOTS

Senior Software Engineer @ Urban Company | Google Certified Android Developer