Get and List Tag Information of Your Photos in 3 Easy Steps

Feyza Ürkut
Huawei Developers
Published in
4 min readMar 8, 2023
Photo by Angèle Kamp on Unsplash

Introduction

Hi, everyone👋

In this article, I will explain how we can group photos with tag information using ML Kit. We will classify the photos taken from the gallery and obtain tag information. Finally, we will look at listing these tags dynamically. I will tell you about these operations through the Photography App, let’s get started!

Contents

Introducing the Photography App

1- Integration of ML Kit

2- Getting TAG Information: On-Cloud Static Image Detection

3- Listing TAG Information with FlexboxLayout

Output

Conclusion

References

Photography App

Photography App is a reference application for HMS Kits to phones running with the Android-based HMS Service. Photography App basically is an app that has everything you could want from a photo application.

Thanks to Huawei ML Kit Image Classification Photography App can separate your photos by tags and you can display all photos which has same tags.

Image Classification via ML Kit

The image classification service classifies elements in images into intuitive categories, such as people, objects, environments, activities, or artwork, to define image themes and application scenarios.

It supports both on-device and on-cloud recognition modes.

  • On-device recognition refers to the process of running the detection algorithm model on the device and returning the recognition result. It supports more than 400 common image categories.
  • On-cloud recognition refers to the process of calling an API on the cloud where the detection algorithm model is running. It supports 12,000 image categories.

In addition, this service allows you to customize image classification models.

1- Integration of ML Kit

  1. Create an app in App Gallery Connect and integrate the App Gallery Connect SDK into your app. For details, you can follow the preparations codelab: Preparations for Integrating HUAWEI HMS Core
  2. Enable ML Kit in Manage APIs bar.
AppGallery Connect: Enable ML Kit

3. Full SDK Integration: Add following dependencies to app level build.gradle file in the dependencies block:

2- Getting TAG Information: On-Cloud Static Image Detection

  1. When using on-cloud services of ML Kit, you can set the API key or access token in PhotoApplication class.

2. Create an image classification analyzer instance with using MLRemoteClassificationAnalyzerSetting in MLKitModule class.

3. Create an MLFrame object using android.graphics.Bitmap, which is the image data in bitmap format. JPG, JPEG, PNG, and BMP images are supported.

4. Call the asyncAnalyseFrame method to classify images.

Note: In order to improve application performance, instead of running the ML Kit every time we open the application, we save the results we obtained with the ML Kit at the first opening of the application and run the ML Kit again in case of changes in the gallery. For this reason, we used Room DB to store the initial results and change results locally. We have come up with a nice result both as an application performance improvement and to show how to use a Huawei Kit with Room DB.

3- Listing TAG Information with FlexboxLayout

  1. First of all, we should create FlexboxLayout item in the layout file.
fragment_home.xml: FlexboxLayout

2. In the fragment, we call the filterAndSortParameterList method. With this method, we group the recurring items in our parameter list, sort them and create a new list with first 10 item.

3. Now, we have the `TAG` list that we had created in view model. Then, we create a TextView and assign them to FlexboxLayout dynamically.

Output

Titles: Listing TAG Information
Detail of Titles

Conclusion

In this article, I talked about obtaining tag information from the ML Kit Image Classification and dynamically listing these tags. A useful feature that you can easily implement in different applications.

Have a nice workday 🙌

--

--