Image Classification Feature of HMS Machine Learning Kit

Önder
Huawei Developers
Published in
5 min readJul 1, 2020

Hello everyone, in this blog I will explain to you what Image Classification is and how it is used.

What is Image Classification?

Image Classification is one of the features of HMS Machine Learning Kit. By using it, we can classify objects in images. We can give examples for object classes such as food, flower, sport etc.

If you want to create an application that files images, you can save your images by classifying them with image classification.

Image classification not only classifies the objects in the images in your mobile phone (static image detection), but also it classifies objects in the camera stream instantly (camera stream detection).

Object recognizing can be processed device-based and cloud-based. Let’s look at the differences.

Device-based recognition:

  1. More efficient
  2. Supports more than 400 image categories
  3. Supports both static image detection and camera stream detection

Cloud-based recognition:

  1. More accurate
  2. Supports more than 12000 image categories
  3. Supports only static image detection

Now, let’s look how to enable the service.

Enabling the service

We sign in to our Huawei Developer Account and click Console button.

Click AppGallery Connect. Then click My apps.

We click our application name. If you haven’t created an application yet, you should create a new one first.

We navigate to Develop tab.

Then, we click Manage APIs tab.

In Manage APIs tab, we can see all the kits. We can enable and disable them according to our project’s needs. For Image Classification, we need to enable ML Kit.

Then, we download agconnect-services.json file.

We copy it to app root of our application.

Configure maven repository addresses in build.gradle (project) file.

Integrating Image Classification SDK

There are two options for SDK, base sdk and full sdk. Full SDK version is recommended.

You can assign permissions to AndroidManifest.xml file, if you need any. If you will use camera stream recognition, you must assign camera permission. If you want to get a static image from the storage in your phone, you must assign external storage permission.

Development Process

In my project, I used both static image detection and camera stream detection. Static image detection gets a picture from the external storage and classifies items on it. Camera stream detection checks items while using the camera and return classes of items instantly.

Static Image Detection

Both device-based and cloud-based recognition supports static image detection. So, we have four methods to create image classification analyzer.

First two methods are for device-based recognition. Other ones are for cloud-based recognition. You can create the analyzer with default settings (Method 2 and 4) or you can customize them (Method 1 and 3).

Then, we create MLFrame object. It will contain the image data.

It requires the image data in Bitmap format. So first, we need to convert our images to Bitmap format from jpeg, png etc. In the project, I get an image from external storage and convert it to bitmap format.

After, we call asyncAnalyseFrame method to classify images.

If the recognition is successful, it returns a list of MLImageClassification objects. The name attribute of this class returns us the image classes such as sport, food etc.

After the recognition is completed, we release the resources.

Camera Stream Detection

Only device-based recognition supports camera stream detection. We create a new class called ClassificationAnalyzerTransactor to process detection results.

Then, we create the analyzer and bind the analyzer to the result processor.

We create an instance of LensEngine class to capture dynamic camera streams and pass them to the analyzer.

And then, we call the run method to start the camera and read streams. As long as the camera is running, recognition always continues. If your app needs to stop detection after finding a specific image class, you need to implement new methods. You can refer to the link for the implementation.

After the recognition, we release the resources.

With this implementation, we can get image classes in pictures and camera streams and use them in our applications.

References

--

--