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 object analyzer.
- // Use MLObjectAnalyzerSetting.TYPE_PICTURE for static image detection.
- MLObjectAnalyzerSetting setting = new MLObjectAnalyzerSetting.Factory()
- .setAnalyzerType(MLObjectAnalyzerSetting.TYPE_PICTURE)
- .allowMultiResults()
- .allowClassification()
- .create();
- MLObjectAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalObjectAnalyzer(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 288 x 288 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 object detection.
- // Create a task to process the result returned by the object detector.
- Task<List<MLObject>> task = analyzer.asyncAnalyseFrame(frame);
- // Asynchronously process the result returned by the object detector.
- task.addOnSuccessListener(new OnSuccessListener<List<MLObject>>() {
- @Override
- public void onSuccess(List<MLObject> objects) {
- // 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.
- }
- }
5. The asynchronous call mode is used in preceding sample code. Object detection also supports synchronous call of the analyseFrame function to obtain the detection result.
- SparseArray<MLObject> objects = analyzer.analyseFrame(frame);
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 object analyzer.
- MLObjectAnalyzer analyzer =
- MLAnalyzerFactory.getInstance().getLocalObjectAnalyzer();
2. Create the ObjectAnalyzerTransactor 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 ObjectAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLObject> {
- @Override
- public void transactResult(MLAnalyzer.Result<MLObject> results) {
- SparseArray<MLObject> 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 ObjectAnalyzerTransactor());
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();
- this.lensEngine = new LensEngine.Creator(context, this.analyzer)
- .setLensType(LensEngine.BACK_LENS)
- .applyDisplayDimension(1440, 1080)
- .applyFps(30.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 = findViewById(R.id.surface_view);
- 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.