Skip to main content

Recognize Landmark In Image Using Firebase ML Kit In Android Studio 2020(Complete Guide) With Source Code | Step By Step Tutorial | 5 Simple Step

In this post, we're going to implement landmark recognization in an app using the ml kit of the firebase.


Step 1: Add Firebase to your android project:

I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step. 

Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:

implementation 'com.google.firebase:firebase-ml-vision:24.0.1'

as shown below:

then click on sync now.


Step 3: Design the layout of the activity:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">
    <ImageView        android:id="@+id/image"        android:layout_width="500dp"        android:layout_height="500dp"        android:layout_above="@+id/selectImage"        android:layout_margin="30dp" />
    <Button        android:id="@+id/selectImage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Select Image !" />
    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/selectImage"        android:layout_margin="30dp"        android:textColor="@android:color/black"        android:textSize="15sp" /></RelativeLayout>

as shown below:


Step 4: Select Image from the device:

I recommend you to first go through the post on how to select or capture an image from the device before going further.

So now, let's open the image cropping activity to select the image on button click:



and now get the image by overriding onActivityResult method:


Step 5: Get the information about the recognized landmark:

**You need to upgrade to Blaze plan for using the landmark recognization**

So after upgrading follow this step:
1. Prepare the input image.
2. Configure and run the landmark detector.
3. Get information about the recognized landmark.

There are 5 ways of getting a firebase vision image object (Prepare the input image):
(i)By Bitmap,
(ii)By media.image,
(iii)By ByteBuffer,
(iv)By ByteArray,
(v)By File on device

We're creating using file path(last option) if u want to know how to create from other option then comment down below:

private void landmarkRecognitionFromImage(Uri uri) {

    try {
        //1. Prepare the input image        FirebaseVisionImage image = FirebaseVisionImage.fromFilePath(MainActivity.this, uri);
        //2. Configure and run the landmark detector        FirebaseVisionCloudDetectorOptions options =
                new FirebaseVisionCloudDetectorOptions.Builder()
                        .setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
                        .setMaxResults(15)
                        .build();
            //default setting:         // FirebaseVisionCloudLandmarkDetector detector = FirebaseVision.getInstance()         // .getVisionCloudLandmarkDetector();
         // To change the default settings:        FirebaseVisionCloudLandmarkDetector detector = FirebaseVision.getInstance()
                .getVisionCloudLandmarkDetector(options);
        //run the landmark detector        detector.detectInImage(image)
                .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionCloudLandmark>>() {
                    @Override                    public void onSuccess(List<FirebaseVisionCloudLandmark> firebaseVisionCloudLandmarks) {
                        for (FirebaseVisionCloudLandmark landmark : firebaseVisionCloudLandmarks) {
                           //3. Get information about the recognized landmark                            Rect bounds = landmark.getBoundingBox();                            String landmarkName = landmark.getLandmark();                            String entityId = landmark.getEntityId();                            float confidence = landmark.getConfidence();                            textView.append("bounds " + bounds + "\n" + "landmarkName " + landmarkName + "\n" + "entityId " + entityId + "\n" + "confidence " + confidence);                            // Multiple locations are possible, e.g., the location of the depicted                            // landmark and the location the picture was taken.                            for (FirebaseVisionLatLng loc : landmark.getLocations()) {
                                double latitude = loc.getLatitude();                                double longitude = loc.getLongitude();                                textView.append("latitude " + latitude + "\n");                                textView.append("longtiude " + longitude + "\n");                            }
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override                    public void onFailure(@NonNull Exception e) {
                        // Task failed with an exception                        // ...                    }
                });    } catch (IOException e) {
        e.printStackTrace();    }
}

Now, run the app :)

If everything is done correctly then you see the excepted output.

See the firebase doc for full reference.

You can see the full source code at GitHub.

If you face any problem or have any suggestion please comment it down we love to answer it.

Comment down what next topic you need a guide on? or Drop a message on our social media handle

 Happy coding and designing : )



Comments

Popular posts from this blog

How To Set Up Android Studio/VS Code For Flutter 2021 (Complete Guide) In Ubuntu | Step By Step Tutorial

In this post, we're going to set up the android studio for flutter - Google’s most awaited UI tool kit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase.  So a single codebase for making an application for multiple platforms. Step 1: Download flutter SDK from flutter.dev and after extracting it place the flutter folder inside it at your desired location like this:- Step 2: Open the terminal and Set the path up to the bin folder inside flutter:-  export PATH=$PATH:/home/himanshu/Desktop/fluttersdk/flutter/bin Step 3: Download java 8 and set JAVA_HOME and JRE_HOME path. Run this command in terminal:- sudo apt install openjdk-8-jdk openjdk-8-jre It will download the java 8 package in /usr/lib/jvm/ and now set the JAVA_HOME and JRE_HOME path up to this location for this, Run this command in the terminal:- export JAVA_HOME=/usr/lib/jvm/java...

Make Barcode Scanner App Using Firebase ML Kit In Android Studio 2020(Complete Guide) With Source Code | Step By Step Tutorial

In this post, we're going to develop an android app that scans the barcode from the image and produce the required output. This is the output after doing all the steps: You can create your own QR Code from a  barcode generator  with custom data like putting a URL or a mail message etc. The above one is a simple text QR Code. The highlighted text in the above screenshot is the same output you get if you scan the above QR Code with Google Lens. As I show it below: So, Now let's make it. Step 1: Add Firebase to your android project: I recommend you to see  how to add firebase to the android project in 5minutes  to know how to add it or if you already add it then you can move on to 2nd Step.    Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file: implementation 'com.google.firebase:firebase-ml-vision:24.0.1' implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.2' ...

How To Add Firebase To Android Project Within 5 Minutes 2021 (Complete Guide) | Step By Step Tutorial

In this post, we're going to add firebase to an android project.                                                                                        Step 1: Go to Firebase Console after sign in to your google account and click on add project . Step 2: Enter your android project name and click continue.  Again click continue. Now, click on the create project and the last click on continue. Our firebase project is ready now and now we have to link it with our android project. So let's follow this remaining step for linking the android project to the firebase project. Step 3: Click on the android icon (2nd one). Step 4: Enter the application id (package name) in the Android package name. You can find your application...



DMCA.com Protection Status

Copywrite © 2021 The MindfulCode