[Huawei Developers] Freemind R:
Introduction

To help understand the image content, the scene detection service can classify the scenario content of images and add labels, such as outdoor scenery, indoor places, and buildings.You can create more customised experiences for users based on the data detected from image. Currently Huawei supports detection of 102 scenarios. For details about the scenarios, refer to List of Scenario Identification Categories.

This service can be used to identify image sets by scenario and create intelligent album sets. You can also select various camera parameters based on the scene in your app, to help users to take better-looking photos.

Prerequisite

The scene detection service supports integration with Android 6.0 and later versions.

The scene detection needs READ_EXTERNAL_STORAGE and CAMERA in AndroidManifest.xml.

Implementation of dynamic permission for camera and storage is not covered in this article. Please make sure to integrate dynamic permission feature.

Development

1. Register as a developer account in AppGallery Connect.

2. Create an application and enable ML kit from AppGallery connect.

3. Refer to Service Enabling. Integrate AppGallery connect SDK. Refer to AppGallery Connect Service Getting Started.

4. Add Huawei Scene detection dependencies in app-level build.gradle.

// ML Scene Detection SDK
implementation 'com.huawei.hms:ml-computer-vision-scenedetection:2.0.3.300'
// Import the scene detection model package.
implementation 'com.huawei.hms:ml-computer-vision-scenedetection-model:2.0.3.300'
implementation 'com.huawei.hms:ml-computer-vision-cloud:2.0.3.300'

5. Sync the gradle.

We have an Activity (MainActivity.java) which has floating buttons to select static scene detection and live scene detection.

Static scene detection is used to detect scene in static images. When we select a photo, the scene detection service returns the results.

Camera stream (Live) scene detection can process camera streams, convert video frames into an MLFrame object, and detect scenarios using the static image detection method.

Implementation of Static scene detection

private void sceneDetectionEvaluation(Bitmap bitmap) {

//Create a scene detection analyzer instance based on the customized configuration.
MLSceneDetectionAnalyzerSetting setting = new MLSceneDetectionAnalyzerSetting.Factory()
// Set confidence for scene detection.
.setConfidence(confidence)
.create();
analyzer = MLSceneDetectionAnalyzerFactory.getInstance().getSceneDetectionAnalyzer(setting);
MLFrame frame = new MLFrame.Creator().setBitmap(bitmap).create();

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.
for( MLSceneDetection sceneDetection : result) {
sb.append("Detected Scene : " + sceneDetection.getResult() + " , " + "Confidence : " + sceneDetection.getConfidence() + "\n");
tvResult.setText(sb.toString());
if (analyzer != null) {
analyzer.stop();
}
}
}})
.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 result code. You can process the result 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 result code.
String errorMessage = mlException.getMessage();
Log.e(TAG, "MLException : " + errorMessage +", error code: "+ String.valueOf(errorCode));
} else {
// Other errors.
Log.e(TAG, "Exception : " + e.getMessage());
}
if (analyzer != null) {
analyzer.stop();
}
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
if(analyzer != null) {
analyzer.stop();
}
}

We can set the settings MLSceneDetectionAnalyzerSetting() and set the confidence level for scene detection. setConfidence() methods needs to get float value. After settings are fixed, we can create the analyzer with settings value. Then, we can set the frame with bitmap. Lastly, we have 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 can set the response to textView tvResult.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201106043921.44463723993892454705467452303511:50511118054857:2800:2D86D94CB7AABDDB3758C0C56705564642F1107DC93EFBD4886ED2B92F94B54A.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201106043921.44463723993892454705467452303511:50511118054857:2800:2D86D94CB7AABDDB3758C0C56705564642F1107DC93EFBD4886ED2B92F94B54A.gif[/IMG]

More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0204400184662360088