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.
1. Create an image segmentation analyzer.
Integrate the human body segmentation model package.
- // Method 1: Use default parameter settings to configure the image segmentation analyzer.
- // The default mode is human body segmentation in fine mode. All segmentation results of human body segmentation are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
- MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer();
- // Method 2: Use MLImageSegmentationSetting to customize the image segmentation analyzer.
- MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory()
- // Set whether to support fine segmentation. The value true indicates fine segmentation, and the value false indicates fast segmentation.
- .setExact(false)
- // Set the human body segmentation mode.
- .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
- // Set returned result types.
- // MLImageSegmentationScene.ALL: All segmentation results are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
- // MLImageSegmentationScene.MASK_ONLY: Only pixel-level label information is returned.
- // MLImageSegmentationScene.FOREGROUND_ONLY: Only a human body image with a transparent background is returned.
- // MLImageSegmentationScene.GRAYSCALE_ONLY: Only a gray-scale image with a white human body and black background is returned.
- .setScene(MLImageSegmentationScene.FOREGROUND_ONLY)
- .create();
- MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);
Integrate the multiclass segmentation model package.
When a multiclass segmentation model package is used on an image, an image segmentation analyzer can be created only by using MLImageSegmentationSetting.
- MLImageSegmentationSetting setting = new MLImageSegmentationSetting
- .Factory()
- // Set whether to support fine segmentation. The value true indicates fine segmentation, and the value false indicates fast segmentation.
- .setExact(true)
- // Set image segmentation.
- .setAnalyzerType(MLImageSegmentationSetting.IMAGE_SEG)
- .create();
- MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);
2. Create an MLFrame object by using android.graphics.Bitmap for the analyzer to detect images. JPG, JPEG, and PNG images are supported. It is recommended that the image size be greater than or equal to 224 x 224 px.
- // Create an MLFrame object using the bitmap, which is the image data in bitmap format.
- MLFrame frame = MLFrame.fromBitmap(bitmap);
3. Call the asyncAnalyseFrame method to perform image segmentation.
- // Create a task to process the result returned by the image segmentation analyzer.
- Task<MLImageSegmentation> task = analyzer.asyncAnalyseFrame(frame);
- // Asynchronously process the result returned by the image segmentation analyzer.
- task.addOnSuccessListener(new
- OnSuccessListener<MLImageSegmentation>() {
- @Override
- public void onSuccess(MLImageSegmentation segmentation) {
- // Detection success.
- }})
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- // Detection failure.
- }
- });
4. After the detection is complete, stop the analyzer to release detection resources.
- if (analyzer != null) {
- try {
- analyzer.stop();
- } catch (IOException e) {
- // Exception handling.
- }
- }
The asynchronous call mode is used in preceding sample code. Image segmentation also supports synchronous call of the analyseFrame function to obtain the detection result.
- SparseArray<MLImageSegmentation> segmentations = analyzer.analyseFrame(frame);
You can process camera streams, convert video frames into an MLFrame object, and detect objects using the static image detection method. You can also use the LensEngine class built in the SDK to perform image segmentation on camera streams. The sample code is as follows:
1. Create an image segmentation analyzer.
- // Use MLImageSegmentationSetting to customize the image segmentation analyzer.
- MLImageSegmentationSetting setting = new MLImageSegmentationSetting
- .Factory()
- // Set whether to support fine segmentation. The value true indicates fine segmentation, and the value false indicates fast segmentation.
- .setExact(false)
- // Set returned result types.
- // MLImageSegmentationScene.ALL: All segmentation results are returned (pixel-level label information, human body image with a transparent background, gray-scale image with a white human body and black background, and an original image for segmentation).
- // MLImageSegmentationScene.MASK_ONLY: Only pixel-level label information and an original image for segmentation are returned.
- // MLImageSegmentationScene.FOREGROUND_ONLY: A human body image with a transparent background and an original image for segmentation are returned.
- // MLImageSegmentationScene.GRAYSCALE_ONLY: A gray-scale image with a white human body and black background and an original image for segmentation are returned.
- .setScene(MLImageSegmentationScene.FOREGROUND_ONLY)
- // Set the segmentation mode. For camera stream segmentation, only MLImageSegmentationSetting.BODY_SEG (only the human body and background) can be set.
- .setAnalyzerType(MLImageSegmentationSetting.BODY_SEG)
- .create();
- MLImageSegmentationAnalyzer analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting);
2. Create the ImageSegmentAnalyzerTransactor 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.
- public class ImageSegmentAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLImageSegmentation> {
- @Override
- public void transactResult(MLAnalyzer.Result<MLImageSegmentation> results) {
- SparseArray<MLImageSegmentation> items = results.getAnalyseList();
- // Determine detection result processing as required. Note that only the detection results are processed.
- // Other detection-related APIs provided by HUAWEI ML Kit cannot be called.
- }
- @Override
- public void destroy() {
- // Callback method used to release resources when the detection ends.
- }
- }
3. Set the detection result processor to bind the analyzer to the result processor.
- analyzer.setTransactor(new ImageSegmentAnalyzerTransactor());
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.
- Context context = this.getApplicationContext();
- LensEngine lensEngine = new LensEngine.Creator(context,analyzer)
- // Set the front or rear camera mode. LensEngine.BACK_LENS indicates the rear camera, and LensEngine.FRONT_LENS indicates the front camera.
- .setLensType(LensEngine.FRONT_LENS)
- .applyDisplayDimension(1280, 720)
- .applyFps(20.0f)
- .enableAutomaticFocus(true)
- .create();
5. Call the run method to start the camera and read camera streams for detection.
- // Implement other logic of the SurfaceView control by yourself.
- SurfaceView mSurfaceView = new SurfaceView(this);
- try {
- lensEngine.run(mSurfaceView.getHolder());
- } catch (IOException e) {
- // Exception handling logic.
- }
6. After the detection is complete, stop the analyzer to release detection resources.
- if (analyzer != null) {
- try {
- analyzer.stop();
- } catch (IOException e) {
- // Exception handling.
- }
- }
- if (lensEngine != null) {
- lensEngine.release();
- }
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.