Search code examples
androidandroid-studiotensorflowobject-detectionbounding-box

Object Detection on Android with Tensorflow Lite


Trying to implement a custom object detection model with Tensorflow Lite, using Android Studio. I am following the guidance provided here: Running on mobile with TensorFlow Lite, however with no success. The example model runs properly showing all the detected labels. Nonetheless, when I try with my custom model I am not getting any labels at all. I have also tried with other models(from the internet but the outcome is the same). It is like that the labels are not being passed with the write way. I copied my detect.tflite and labelmap.txt, I changed the TF_OD_API_INPUT_SIZE and the TF_OD_API_IS_QUANTIZED in the DetectorActivity.java but still not getting results(detected class with a bounding box and a score).

The Logcat shows the following:

2020-10-11 18:37:54.315 31681-31681/org.tensorflow.lite.examples.detection E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
2020-10-11 18:37:54.315 31681-31681/org.tensorflow.lite.examples.detection E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
2020-10-11 18:37:54.315 31681-31681/org.tensorflow.lite.examples.detection E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
2020-10-11 18:37:54.315 31681-31681/org.tensorflow.lite.examples.detection E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
2020-10-11 18:37:54.315 31681-31681/org.tensorflow.lite.examples.detection E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
2020-10-11 18:37:54.315 31681-31681/org.tensorflow.lite.examples.detection E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
2020-10-11 18:37:54.859 31681-31681/org.tensorflow.lite.examples.detection E/tensorflow: CameraActivity: Exception!
    java.lang.IllegalStateException: This model does not contain associated files, and is not a Zip file.
        at org.tensorflow.lite.support.metadata.MetadataExtractor.assertZipFile(MetadataExtractor.java:325)
        at org.tensorflow.lite.support.metadata.MetadataExtractor.getAssociatedFile(MetadataExtractor.java:165)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:118)
        at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:96)
        at org.tensorflow.lite.examples.detection.CameraActivity.onPreviewFrame(CameraActivity.java:200)
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1157)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

How can I take the detection? Do I need an additional file(metadata) relative with the labels or I am doing sth else wrong? The above case is tested with an Android 7 device. Thanks!


Solution

  • This is an issue with this documentation specifically that wasn't updated.

    The main problem is that the sample was updated to use models with Metadata attached to it, specifically with the labels embedded as an asset of the model.

    When you add your labels file to the model, everything should just work.