How to use Huawei ML Kit — Scene Detection feature ?

Ismail Oguzhan Ay
Huawei Developers
Published in
4 min readNov 2, 2020

Hello everyone, today we will talk about scene detection feature of ML Kit. Huawei supports so many features with Machine Learning Kit. It grows up each day and comes with new ML Kit features.

What is Scene Detection Service ?

The scene detection service can classify the scenario content of images and add labels, such as outdoor scenery, indoor places, and buildings, to help understand the image content. Based on the detected information, you can create more personalized app experience for users. Currently, on-device detection on 102 scenarios is supported. For details about the scenarios, please refer to List of Scenario Identification Categories.

  • This service is widely used in image-based scenario understanding.
  • You can integrate this service to classify image sets by scenario and generate intelligent album sets.
  • You can also select camera parameters based on the photographing scene in your app, to help users take better-looking photos.

Before the starting implementation

  • Check the assigning permissions, the scene detection needs WRITE_EXTERNAL_STORAGE and CAMERA in AndroidManifest.xml. For all permission necessaries, please check that list.
  • The scene detection service supports integration with Android 6.0 and later versions.

Development Process

We need to follow some steps for the integration of Huawei ML Kit.

  1. We need to register as a developer account in AppGallery Connect.
  2. We create an application and enable ML Kit from AppGallery Connect.
  3. After the configuration in AppGallery Connect, let’s integrate the Huawei ML Kit into our demo application.

a. We need to get agconnect-services.json file for configurations from AppGallery Connect. Then, we add it into our application project level under the app folder.

b. After that, we need to add dependencies into gradle files.

Now, we need to sync our gradle files.

Let’s start the coding !

We have an activity(MainActivity.kt) for selecting images with two buttons from camera or storage. So, it has a layout which is activity_main.xml. When we select a photo, the scene detection service returns the results. Let’s check them.

Under the res folder, we can add a xml file which name is file_paths. We used the file for image paths. If you get an error code 5, it is about the your picture size. Please check this link.

We create two buttons for selecting options with camera or storage. Firstly, we check required permissions. Then, we start the event with the button. For example, we select a use case with select an image from storage. After the getting required permissions, we called startActivityForResult() method. In onActivityResult() method, we get the image from storage as a bitmap, then we called performSceneDetection() method with bitmap.

We set the settings MLSceneDetectionAnalyzerSetting() and set the confidence level for scene detection. setConfidence() methods needs to get float value. After the set the settings, we create the analyzer with settings value. Then, we set the frame with bitmap. Lastly, we created a task for list of MLSceneDetection object. We have listener functions for success and failure. The service returns list of results. The results have two parameter which are result and confidence. We set the response to textView_service_response.

Finally, we completed demo application about the usage of scene detection with Huawei ML Kit. I hope this article will be useful for implementation and use case usages.

Thank You ! ..

For more details, please check these useful links :

--

--