目录

Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.

Static image detection

1. Create an image classification analyzer. You can create the analyzer using the MLLocalClassificationAnalyzerSetting class.

  1. // Method 1: Use customized parameter settings for device-based recognition.
  2. MLLocalClassificationAnalyzerSetting setting =  
  3.    new MLLocalClassificationAnalyzerSetting.Factory()        
  4.      .setMinAcceptablePossibility(0.8f)
  5.      .create();  
  6. MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer(setting);
  7. // Method 2: Use default parameter settings for device-based recognition.
  8. MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalImageClassificationAnalyzer();
  9. // Method 3: Use customized parameter settings for cloud-based recognition.
  10. MLRemoteClassificationAnalyzerSetting setting =  
  11.    new MLRemoteClassificationAnalyzerSetting.Factory()        
  12.      .setMinAcceptablePossibility(0.8f)
  13.      .create();
  14. MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getRemoteImageClassificationAnalyzer(setting);
  15. // Method 4: Use default parameter settings for cloud-based recognition.
  16. MLImageClassificationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getRemoteImageClassificationAnalyzer();

2. Create an MLFrame object by using android.graphics.Bitmap. JPG, JPEG, PNG, and BMP images are supported. It is recommended that the image size be greater than or equal to 112 x 112 px.

  1. // Create an MLFrame object using the bitmap, which is the image data in bitmap format.
  2. MLFrame frame = MLFrame.fromBitmap(bitmap);

3. Call the asyncAnalyseFrame method to classify images.

  1. Task<List<MLImageClassification>> task = analyzer.asyncAnalyseFrame(frame);
  2. task.addOnSuccessListener(new OnSuccessListener<List<MLImageClassification>>() {    
  3.     @Override
  4.     public void onSuccess(List<MLImageClassification> classifications) {
  5.        // Recognition success.
  6.     }
  7. }).addOnFailureListener(new OnFailureListener() {
  8.     @Override
  9.     public void onFailure(Exception e) {
  10.        // Recognition failure.
  11.         // Recognition failure.
  12.         try {
  13.             MLException mlException = (MLException)e;
  14.    // Obtain the result codes. You can process the result codes and customize respective messages displayed to users. For details about the result codes, please refer to Result Codes of ML Kit.
  15.             int errorCode = mlException.getErrCode();
  16.   // Obtain the error information. You can quickly locate the fault based on the result code.
  17.             String errorMessage = mlException.getMessage();
  18.         } catch (Exception error) {
  19.           // Handle the conversion error.
  20.         }
  21.      }
  22. });

4. After the recognition is complete, stop the analyzer to release recognition resources.

  1. try {
  2.    if (analyzer != null) {
  3.       analyzer.stop();
  4. } catch (IOException e) {
  5.    // Exception handling.
  6. }

The asynchronous call mode is used in preceding sample code. Image classification also supports synchronous call of the analyseFrame function to obtain the detection result.

  1. SparseArray<MLImageClassification> classifications = analyzer.analyseFrame(frame);

Camera stream detection

You can process camera streams, convert video frames into an MLFrame object, and detect objects using the static image detection method. If the synchronous detection API is called, you can also use the LensEngine class built in the SDK to detect objects in camera streams. The sample code is as follows:

1. Create an image classification analyzer, which can be created only on the device.

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

2. Create the ClassificationAnalyzerTransactor 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.

  1. public class ClassificationAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLImageClassification> {
  2.    @Override
  3.    public void transactResult(MLAnalyzer.Result<MLImageClassification> results) {
  4.        SparseArray<MLImageClassification> items = results.getAnalyseList();
  5.      // Determine detection result processing as required. Note that only the detection results are processed.
  6.        // Other detection-related APIs provided by HUAWEI ML Kit cannot be called.
  7.    }
  8.    @Override
  9.    public void destroy() {
  10.        // Callback method used to release resources when the detection ends.
  11.    }

  12. }

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

  1. analyzer.setTransactor(new ClassificationAnalyzerTransactor());

4. 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.

  1. Context context = this.getApplicationContext();
  2. this.lensEngine = new LensEngine.Creator(context, this.analyzer)
  3.    .setLensType(LensEngine.BACK_LENS)
  4.    .applyDisplayDimension(1440, 1080)
  5.    .applyFps(30.0f)
  6.    .enableAutomaticFocus(true)
  7.    .create();

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

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

6. After the detection is complete, stop the analyzer to release detection resources.

  1. if (analyzer != null) {
  2.    try {
  3.        analyzer.stop();
  4.    } catch (IOException e) {
  5.         // Exception handling.
  6.    }
  7. }
  8. if (lensEngine != null) {
  9.    lensEngine.release();
  10. }

In camera stream detection, when MLAnalyzer.MLTransactor<T> is inherited to process detection results, if your app needs to stop detection after a specific result is detected and continue detection after the result is processed, please refer to Development for Multi Detections in Camera Stream Detection Mode.