1

Nowadays, mobile phone photography is more and more convenient, and you can record a good life with just one shot. But the more photos there are, the more troublesome it is to organize. Sometimes we want to reprocess and share photos, and we need to keep rummaging through the albums. HMS Core Machine Learning Service (ML Kit) provides an image classification service, which is convenient for users to quickly filter image collections, define image themes, and label classification information in multiple scenarios. For example, at the end of the semester, the teacher often asks parents and children to make a parent-child growth manual. After the education service app integrates the picture classification service, it can classify and summarize all the photos since the semester into an album; go out to play multiple attractions or travel through different cities, travel Sharing apps can help users organize and classify pictures based on a certain location or a certain object; in the whole scene, the mobile photo album needs to synchronize photos to the big screen for rotation, and file sharing apps can set sharing rules and upload them by category.

Next, developers are invited to experience through the following actual development , and we look forward to further bringing convenient image classification services to users~

development practice

1 Development preparation

Before development, you need to configure the Maven warehouse address of HMS Core SDK:
repositories {
maven {
url' https://cmc.centralrepo.rnd.huawei.com/artifactory/product_maven/ ' }
}

And integrate this SDK:

  1. dependencies {
  2. // Introduce the basic SDK
    implementation 'com.huawei.hms:ml-computer-vision-classification:3.3.0.300'
  3. // Import the image classification model package
  4. implementation 'com.huawei.hms:ml-computer-vision-image-classification-model:3.3.0.300'

}

Then, please refer to the instructions for using cloud authentication information to set the authentication information of your application.

2 Editing project integration

2.1 Set the authentication information of the application

Application authentication information can be set through api_key or Access Token.

The Access Token is set through the setAccessToken method, which can be initialized and set once when the application starts, and there is no need to set it multiple times.
MLApplication.getInstance().setAccessToken("your access token");

The api_key is set by the setApiKey method, which can be initialized and set once when the application starts, and there is no need to set it multiple times.
MLApplication.getInstance().setApiKey("your ApiKey");

2.2 Creating a Still Image Detector

// Method 1: Device-side identification is configured using custom parameters.

MLLocalClassificationAnalyzerSetting setting =
new MLLocalClassificationAnalyzerSetting.Factory()
.setMinAcceptablePossibility(0.8f)
.create();
MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer(setting);

// Method 2: The device-side identification uses the default parameter configuration.

MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer();

2.3 Create MLFrame

// Create MLFrame through bitmap, bitmap is the input image data in Bitmap format. The supported image formats include: jpg/jpeg/png/bmp, and the recommended image size is not less than 112*112 pixels.

MLFrame frame = MLFrame.fromBitmap(bitmap);

2.4 Call the asyncAnalyseFrame method for image classification

 Task<List<MLImageClassification>> task = analyzer.asyncAnalyseFrame(frame); 

task.addOnSuccessListener(new OnSuccessListener<List<MLImageClassification>>() {

    @Override
        
    public void onSuccess(List<MLImageClassification> classifications) {
        
        // 识别成功。
                
        // 遍历返回的列表MLImageClassification,获取分类名称等信息。
                
    }
        
}).addOnFailureListener(new OnFailureListener() {

    @Override
        
    public void onFailure(Exception e) {
        
        // 识别失败。
                
        // Recognition failure.
                
        try {
            MLException mlException = (MLException)e;
            // 获取错误码,开发者可以对错误码进行处理,根据错误码进行差异化的页面提示。
            int errorCode = mlException.getErrCode();
            // 获取报错信息,开发者可以结合错误码,快速定位问题。
            String errorMessage = mlException.getMessage();
        } catch (Exception error) {
            // 转换错误处理。
        }
    }
});

2.5 Stop the analyzer after recognition is complete

 try {
    if (analyzer != null) {
       analyzer.stop();
    }
} catch (IOException e) {
 // 异常处理。
}

3 Demonstration and Description

3.1 Description

The image classification function includes static device-side, static cloud-side, and dynamic detection. The above examples are only static device-side functions.

3.2 Demonstration

Learn more details>>

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙应用,和开发者共同成长。