How to Put TensorFlow AI model(Object recognition) in Android APP?

604 views Asked by At

I trained the model as: https://www.google.com.au/amp/s/blog.roboflow.com/training-a-tensorflow-object-detection-model-with-a-custom-dataset/amp/ And converted it to tflite. Then I try to put the AI model in an android APP. I followed: https://developers.google.com/ml-kit/vision/object-detection/custom-models/android?fbclid=IwAR07uNgzQ2c5PTp13TiPVeKGQsXaJnJR9jzyvtviXCRegFFJlM-_G799TlY converted the bitmap to InputImage Object. And do all the configurations. I converted the image and then load model try to print results:

    // getting bitmap of the image
    Bitmap photo = (Bitmap) data.getExtras().get("data");
    //convert image
    InputImage image = InputImage.fromBitmap(photo,0);
//load local model
LocalModel localModel =
                    new LocalModel.Builder()
                            .setAssetFilePath("mobilenet_v1_1.0_224_quant.tflite")
                            // or .setAbsoluteFilePath(absolute file path to tflite model)
                            .build();

            // Multiple object detection in static images
            CustomObjectDetectorOptions customObjectDetectorOptions =
                    new CustomObjectDetectorOptions.Builder(localModel)
                            .setDetectorMode(CustomObjectDetectorOptions.SINGLE_IMAGE_MODE)
                            .enableMultipleObjects()
                            .enableClassification()
                            .setClassificationConfidenceThreshold(0.5f)
                            .setMaxPerObjectLabelCount(3)
                            .build();

            ObjectDetector objectDetector =
                    ObjectDetection.getClient(customObjectDetectorOptions);

            objectDetector
                    .process(image)
                    .addOnFailureListener(e -> {System.out.println(e.getMessage());})
                    .addOnSuccessListener(results -> {
                        for (DetectedObject detectedObject : results) {
                            Rect boundingBox = detectedObject.getBoundingBox();
                            Integer trackingId = detectedObject.getTrackingId();
                            for (DetectedObject.Label label : detectedObject.getLabels()) {
                                String text = label.getText();
                                int index = label.getIndex();
                                float confidence = label.getConfidence();
                                System.out.println(text);
                                System.out.println(index);
                                System.out.println(confidence);
                            }

                            System.out.println(boundingBox);
                            System.out.println(trackingId);
                        }
                    });

But I got errors:

“Failed to initialize detector. Unexpected number of dimensions for output index 0: got 3D, expected either 2D."

Do you have any idea about this issue? Thank you so much if you can give me some solutions.

The full error:

E/native: calculator_graph.cc:776 INVALID_ARGUMENT: CalculatorGraph::Run() failed in Run:
    Calculator::Open() for node "[BoxClassifierCalculator, BoxClassifierCalculator with output stream: detection_results0]" failed: #vk Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
    pipeline_jni.cc:62 CalculatorGraph::Run() failed in Run:
    Calculator::Open() for node "[BoxClassifierCalculator, BoxClassifierCalculator with output stream: detection_results0]" failed: #vk Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
E/native: pipeline_jni.cc:209 Graph has errors:
    Calculator::Open() for node "[BoxClassifierCalculator, BoxClassifierCalculator with output stream: detection_results0]" failed: #vk Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
E/MobileVisionBase: Error preloading model resource
    com.google.mlkit.common.MlKitException: Failed to initialize detector. Unexpected number of dimensions for output index 0: got 3D, expected either 2D (BxN with B=1) or 4D (BxHxWxN with B=1, W=1, H=1).
        at com.google.mlkit.vision.vkp.PipelineManager.start(com.google.mlkit:vision-internal-vkp@@16.0.0:68)
        at com.google.mlkit.vision.objects.custom.internal.zzd.load(com.google.mlkit:object-detection-custom@@16.0.0:79)
        at com.google.mlkit.common.sdkinternal.ModelResource.zza(com.google.mlkit:common@@16.0.0:22)
        at com.google.mlkit.common.sdkinternal.zzn.call(com.google.mlkit:common@@16.0.0)
        at com.google.mlkit.common.sdkinternal.zzm.run(com.google.mlkit:common@@16.0.0:5)
        at com.google.mlkit.common.sdkinternal.zzq.run(com.google.mlkit:common@@16.0.0:3)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at com.google.android.gms.internal.mlkit_common.zzb.dispatchMessage(com.google.mlkit:common@@16.0.0:6)
        at android.os.Looper.loop(Looper.java:154)
        at android.os.HandlerThread.run(HandlerThread.java:61)
0

There are 0 answers