Implement Image Labeling 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 image labeling that recognizes entities in an image using the firebase ml kit.
This is the output after doing all the steps:
So, Now make it happen.
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:
as shown below.
Then click on Sync Now.
Step 3: Design the layout of the activity:
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: Run the image labeler and get information about the labeled object
Use either on-device model (Spark (default)) or cloud-based model (blaze (upgrade))
See the full difference between these models.
3 steps to get the information about the labeled object:
1. Prepare the input image.
2. Configure and run the image labeler.
3. Get information about labeled objects.
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:
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.
So, Now make it happen.
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-image-label-model:19.0.0'
as shown below.
Then click on Sync Now.
<?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="16sp" />
</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: Run the image labeler and get information about the labeled object
Use either on-device model (Spark (default)) or cloud-based model (blaze (upgrade))
See the full difference between these models.
3 steps to get the information about the labeled object:
1. Prepare the input image.
2. Configure and run the image labeler.
3. Get information about labeled objects.
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 labelImage(Uri uri) { try { // 1. Prepare the input image FirebaseVisionImage image = FirebaseVisionImage.fromFilePath(MainActivity.this, uri); // 2. Configure and run the image labeler FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() .getOnDeviceImageLabeler(); // Or, to set the minimum confidence required:
// FirebaseVisionOnDeviceImageLabelerOptions options =
// new FirebaseVisionOnDeviceImageLabelerOptions.Builder()
// .setConfidenceThreshold(0.7f)
// .build();
// FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance()
// .getOnDeviceImageLabeler(options); labeler.processImage(image) .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() { @Override
public void onSuccess(List<FirebaseVisionImageLabel> labels) { for (FirebaseVisionImageLabel label : labels) { //3. Get information about labeled objects
String text = label.getText();
String entityId = label.getEntityId();
float confidence = label.getConfidence();
textView.append("Text- " + text + ", " + "EntityId- " + entityId + ", " + "Confidence- " + ("" + confidence * 100).subSequence(0, 4) + "%" + "\n\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
Post a Comment