目录

class info
com.huawei.hms.mlsdk.classification.MLImageClassificationAnalyzer

Represents the image classification analyzer, which detects the classification (MLImageClassification) of a supplied image.

There are two types of image classification analyzers. 

One is the on-device analyzer, which runs a classification model on the device and returns the classification result. The other is the on-cloud analyzer, which runs a classification model on the cloud after the cloud API is called.

An on-device image classification analyzer can be created using MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer() or MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer(MLLocalClassificationAnalyzerSetting). 

The former uses the default configuration in MLLocalClassificationAnalyzerSetting, and the latter uses the customized configuration.

Sample Code

  1. // Create an on-device image classification analyzer that uses the default configuration.
  2. MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer();
  3. // You can also create an on-cloud classification analyzer in a similar way.
  4. // MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getRemoteImageClassificationAnalyzer();
  5. // Create an MLFramMLFrameing the bitmap.It is recommended that the image size be greater than or equal to 224 x 224.
  6. MLFrame frame = MLFrame.fromBitmap(bitmap);
  7. Task<List<MLImageClassification>> task = analyzer.asyncAnalyseFrame(frame);
  8. task.addOnSuccessListener(new OnSuccessListener<List<MLImageClassification>>() {
  9.   public void onSuccess(List<MLImageClassification>classifications) {
  10.      // Process the classification result.
  11.   }
  12. }).addOnFailureListener(new OnFailureListener() {
  13.   public void onFailure(Exception e) {
  14.      // Processing failure.
  15.   }
  16. });

Constant Summary

TypeNameDesc
intTYPE_LOCALThe image classification analyzer uses an on-device model.
intTYPE_REMOTEThe image classification analyzer uses an on-cloud model.

Public Method Summary

Return typeMethod name
android.util.SparseArray<MLImageClassification>analyseFrame(MLFrame frame)
com.huawei.hmf.tasks.Task<List<MLImageClassification>>asyncAnalyseFrame(MLFrame frame)
intgetAnalyzerType()
voidstop()throws IOException

Constants

Constant
public static final int TYPE_LOCAL

The image classification analyzer uses an on-device model.

Constant value: 0

Constant
public static final int TYPE_REMOTE

The image classification analyzer uses an on-cloud model.

Constant value: 1

Public Methods

Method
public android.util.SparseArray<MLImageClassification> analyseFrame(MLFrame frame)
Classifies images by synchronous processing.

Parameters

Parameter nameParameter desc
frameImage to be classified.


Return

Typedesc
android.util.SparseArray<MLImageClassification>Returns a set of classification results.
Method
public com.huawei.hmf.tasks.Task<List<MLImageClassification>> asyncAnalyseFrame(MLFrame frame)
Classifies images by asynchronous processing.

Parameters

Parameter nameParameter desc
frameImage to be classified.


Return

Typedesc
com.huawei.hmf.tasks.Task<List<MLImageClassification>>Returns the asynchronous task that encapsulates the classification result. For details about the usage, see the sample code.
Method
public int getAnalyzerType()
Obtains the type of the image classification analyzer.

Return

Typedesc
intReturns the type of the image classification analyzer.
0: TYPE_LOCAL, indicating that the image classification analyzer uses an on-device model.
1: TYPE_REMOTE, indicating that the image classification analyzer uses an on-cloud model.
Method
public void stop() throws IOException
Closes the resources used by the image classification analyzer.

Throws

Exception nameException desc
IOExceptionInput/output exception.