[Huawei Developers] Freemind R:
Overview

"Hey, I just took some pictures of that gorgeous view. Take a look."

"Yes, let me see."

... (a few minutes later)

"Where are they?"

"Wait a minute. There are too many pictures."

Have you experienced this type of frustration before? Finding one specific image in a gallery packed with thousands of images, can be a daunting task. Wouldn't it be nice if there was a way to search for images by category, rather than having to browse through your entire album to find what you want?

Our thoughts exactly! That's why we created HUAWEI ML Kit's scene detection service, which empowers your app to build a smart album, bolstered by intelligent image classification, the result of detecting and labeling elements within images. With this service, you'll be able to locate any image in little time, and with zero hassle.

Features

ML Kit's scene detection service is able to classify and annotate images with food, flowers, plants, cats, dogs, kitchens, mountains, and washers, among a multitude of other items, as well as provide for an enhanced user experience based on the detected information.

The service contains the following features:

Multi-scenario detection

Detects 102 scenarios, with more scenarios continually added.

High detection accuracy

Detects a wide range of objects with a high degree of accuracy.

Fast detection response

Responds in milliseconds, and continually optimizes performance.

Simple and efficient integration

Facilitates simple and cost-effective integration, with APIs and SDK packages.

Applicable Scenarios

In addition to creating smart albums, retrieving, and classifying images, the scene detection service can also automatically select corresponding filters and camera parameters to help users take better images, by detecting where the users are located.

Development Practice

1. Preparations

1.1 Configure app information in AppGallery Connect.

Before you start developing an app, configure the app information in AppGallery Connect. For details, please refer to Development Guide.

1.2 Configure the Maven repository address for the HMS Core SDK, and integrate the SDK for the service.

(1) Open the build.gradle file in the root directory of your Android Studio project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201119010632.32927814664227265689209635991202:50511123020134:2800:9CCFE44AEB8D8039E5AC8FCDA0C61BE217B8E8AACEE17249BD9C5A733A89A512.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201119010632.32927814664227265689209635991202:50511123020134:2800:9CCFE44AEB8D8039E5AC8FCDA0C61BE217B8E8AACEE17249BD9C5A733A89A512.jpg[/IMG]

(2) Add the AppGallery Connect plug-in and the Maven repository.

Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.

Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.

If the agconnect-services.json file has been added to the app, go to buildscript > dependencies and add the AppGallery Connect plug-in configuration.

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}

allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

2. Code Development

Static Image Detection

2.1 Create a scene detection analyzer instance.

// Method 1: Use default parameter settings.
MLSceneDetectionAnalyzer analyzer = MLSceneDetectionAnalyzerFactory.getInstance().getSceneDetectionAnalyzer();
// Method 2: Create a scene detection analyzer instance based on the customized configuration.
MLSceneDetectionAnalyzerSetting setting = new MLSceneDetectionAnalyzerSetting.Factory()
// Set confidence for scene detection.
.setConfidence(confidence)
.create();
MLSceneDetectionAnalyzer analyzer =

2.2 Create an MLFrame object by using the android.graphics.Bitmap. JPG, JPEG, PNG, and BMP images are all supported.

MLFrame frame = new MLFrame.Creator().setBitmap(bitmap).create();

2.3 Implement scene detection.

// Method 1: Detect in synchronous mode.
SparseArray<MLSceneDetection> results = analyzer.analyseFrame(frame);
// Method 2: Detect in asynchronous mode.
Task<List<MLSceneDetection>> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<List<MLSceneDetection>>() {
public void onSuccess(List<MLSceneDetection> result) {
// Processing logic for scene detection success.
}})
.addOnFailureListener(new OnFailureListener() {
public void onFailure(Exception e) {
// Processing logic for scene detection failure.
// failure.
if (e instanceof MLException) {
MLException mlException = (MLException)e;
// Obtain the error code. You can process the error code and customize respective messages displayed to users.
int errorCode = mlException.getErrCode();
// Obtain the error information. You can quickly locate the fault based on the error code.
String errorMessage = mlException.getMessage();
} else {
// Other exceptions.
}
}
});

2.4 Stop the analyzer and release the detection resources when the detection ends.

if (analyzer != null) {
analyzer.stop();
}

Camera Stream Detection

You can process camera streams, convert them into an MLFrame object, and detect scenarios using the static image detection method.

If the synchronous detection API is called, you can also use the LensEngine class built into the SDK to detect scenarios in camera streams. The following is the sample code:

3.1 Create a scene detection analyzer, which can only be created on the device.

MLSceneDetectionAnalyzer analyzer = MLSceneDetectionAnalyzerFactory.getInstance().getSceneDetectionAnalyzer();

3.2 Create the SceneDetectionAnalyzerTransactor class for processing detection results. This class implements the MLAnalyzer.MLTransactor<T> API and uses the transactResult method in this API to obtain the detection results and implement specific services.

public class SceneDetectionAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLSceneDetection> {
@Override
public void transactResult(MLAnalyzer.Result<MLSceneDetection> results) {
SparseArray<MLSceneDetection> items = results.getAnalyseList();
// Determine detection result processing as required. Note that only the detection results are processed.
// Other detection-related APIs provided by ML Kit cannot be called.
}
@Override
public void destroy() {
// Callback method used to release resources when the detection ends.
}
}

3.3 Set the detection result processor to bind the analyzer to the result processor.

analyzer.setTransactor(new SceneDetectionAnalyzerTransactor());
// Create an instance of the LensEngine class provided by the HMS Core ML SDK to capture dynamic camera streams and pass the streams to the analyzer.
Context context = this.getApplicationContext();
LensEngine lensEngine = new LensEngine.Creator(context, this.analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(1440, 1080)
.applyFps(30.0f)
.enableAutomaticFocus(true)
.create();

3.4 Call the run method to start the camera and read camera streams for detection.

// Implement other logic of the SurfaceView control by yourself.
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
// Exception handling logic.
}

3.5 Stop the analyzer and release the detection resources when the detection ends.

if (analyzer != null) {
analyzer.stop();
}
if (lensEngine != null) {
lensEngine.release();
}

You'll find that the sky, plants, and mountains in all of your images will be identified in an instant. Pretty exciting stuff, wouldn't you say? Feel free to try it out yourself!

GitHub Source Code

Reference

Official website of Huawei Developers

Development Guide

HMS Core official community on Reddit

Demo and sample code

Discussions on Stack Overflow