Overview

1. spaceGearSDK AAR

Copy the spaceGearSDK-debug.aar library file to objectSample/app/aar.

app/build.gradle

Set implementation AAR.


dependencies {
    .
    .
    .
    implementation files('aar/spaceGearSDK-debug.aar')
}

2. ObjectDetectionModel Init

MainActivity.java

//TODO intialize the tracker to draw rectangles
        tracker = new MultiBoxTracker(this);

        //TODO inialize object detector
        try {
            detector =
                    ObjectDetectionModel.create(
                            this,
                            Config.TFLITE_MODEL,
                            Config.TFLITE_LABEL,
                            TF_OD_API_INPUT_SIZE,
                            true);

            detector.setNumThreads(4);
            detector.setUseNNAPI(true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }        

During app launch, the ObjectDetectionModel from spaceGearSDK is initialized in the onCreate() method.

3. ObjectDetection Recognize Result

drawing/MultiBoxTracker.java

public synchronized void trackResults(final List<Detector.Recognition> results, final long timestamp) {
        processResults(results);
    }
    
private void processResults(final List<Detector.Recognition> results) {
        final List<Pair<Float, Detector.Recognition>> rectsToTrack = new LinkedList<Pair<Float, Detector.Recognition>>();

        screenRects.clear();
        final Matrix rgbFrameToScreen = new Matrix(getFrameToCanvasMatrix());

        for (final Detector.Recognition result : results) {
            if (result.getLocation() == null) {
                continue;
            }
            final RectF detectionFrameRect = new RectF(result.getLocation());

            final RectF detectionScreenRect = new RectF();
            rgbFrameToScreen.mapRect(detectionScreenRect, detectionFrameRect);

//      logger.v(
//          "Result! Frame: " + result.getLocation() + " mapped to screen:" + detectionScreenRect);

            screenRects.add(new Pair<Float, RectF>(result.getConfidence(), detectionScreenRect));

            if (detectionFrameRect.width() < MIN_SIZE || detectionFrameRect.height() < MIN_SIZE) {
                //logger.w("Degenerate rectangle! " + detectionFrameRect);
                continue;
            }

            rectsToTrack.add(new Pair<Float, Detector.Recognition>(result.getConfidence(), result));
        }

        trackedObjects.clear();
        if (rectsToTrack.isEmpty()) {
            // logger.v("Nothing to track, aborting.");
            return;
        }

        for (final Pair<Float, Detector.Recognition> potential : rectsToTrack) {
            final TrackedRecognition trackedRecognition = new TrackedRecognition();
            trackedRecognition.detectionConfidence = potential.first;
            trackedRecognition.location = new RectF(potential.second.getLocation());
            trackedRecognition.title = potential.second.getTitle();
            trackedRecognition.color = COLORS[trackedObjects.size()];
            trackedObjects.add(trackedRecognition);

            if (trackedObjects.size() >= COLORS.length) {
                break;
            }
        }
    }

The part where camera preview is received, object detection is performed, and the result values are obtained.

4. Model Change

Inside the "model" folder of the objectSample project, there are the following files: efficientdet_lite0.tflite, efficientdet_lite1.tflite, efficientdet_lite2.tflite, efficientdet_lite3.tflite, lite-model_ssd_mobilenet_v1_1_metadata_2.tflite, and lite-model_yolo-v5-tflite_tflite_model_1.tflite.

After copying the model files to objectSample/app/src/main/assets, modify util/Config.java.

public class Config {
    public static String TFLITE_MODEL = "lite-model_ssd_mobilenet_v1_1_metadata_2.tflite";
    //public static String TFLITE_MODEL = "efficientdet_lite0.tflite";
    public static String TFLITE_LABEL = "labelmap.txt";
}

Performance may vary depending on the model and the device.

5. Diagram

Last updated