preface

More photos in your phone album? Want to quickly sift through thousands of photos to find the one you took at Disneyland last year? Huawei Image Kit Image theme tags can help you efficiently sort out the images in your album, easily identify 400+ photo scenes, and help you quickly find the one you want!

Application scenarios

When there are more pictures in the mobile phone album, labels can be classified and stored, so that users can quickly find the pictures they want;

On social platforms, in order to let more users see your beautiful pictures, the “Tag generation” step can automatically generate corresponding labels for your pictures, and share pictures in corresponding sections, so as to bring more exposure to your pictures.

The development of actual combat

Here’s how to integrate Imgae Kit’s image theme tag capabilities:

The development of preparation

The development steps of image theme label service are as follows. The developer needs to provide pictures and related parameters to get the corresponding return values.

Step 1: Import the image subject label service pack.

import com.huawei.hms.image.vision.*;
Copy the code

Step 2: Get the image subject tag service instance.

// Get the ImageVisionImpl object
ImageVisionImpl imageVisionAPI = ImageVision.getInstance(this);
Copy the code

Step 3: Service initialization, the same as the filter service, refer to the related description in the development steps.

Step 4: Build the parameter object.

RequestJson:

The taskJson field information for the image subject tag is as follows:

Note: When using the image subject tag service, the developer needs to ensure that the token provided is valid, otherwise the service cannot be used. For details about how to obtain the token, see Obtaining the Token.

Image theme tag service requestJson example:

{
  "requestId":""."taskJson": {"language":"cn"},
  "authJson": {"projectId":"projectIdTest"."appId":"appIdTest"."authApiKey":"authApiKeyTest"."clientSecret":"CSecretTest"."clientId":"CIdTest"."token":"tokenTest"}}Copy the code

Step 5: Image subject tag service to obtain results.

When invoking the image theme label interface, the developer needs to enter the Bitmap of the image to be processed (see Step 4). This function requires a network connection. If not, an error code is returned. Note that because the interface involves network requests, a child thread needs to be opened to invoke the interface.

// Get the return value
new Thread(new Runnable() {
    @Override    
    public void run(a) {
        ImageVisionResult result = imageVisionAPI.analyzeImageThemeTag(requestJson, imageBitmap);
        }           
    }).start();
Copy the code

Result Return value:

Response Return value:

JSONArray tags:

JSONArray objectList:

JSONObject box:

Step 6: Stop the service.

When the topic label effect is no longer needed, the interface is called to stop the service. When stopCode is 0, the execution succeeds.

if (null! = imageVisionAPI) {int stopCode = imageVisionAPI.stop();
}
Copy the code

Making the source code

Github source: github.com/HMS-Core/hm…


The original link: developer.huawei.com/consumer/cn… Author: Pepper