I am building an app for object detection with a camera api (camerax) and a self-trained tflite model.
For the integration of tflite I have tried two approaches. First with the ML Kit and second with the TensorFlow Lite inference. I like the second approach better but there I have the problem to get the coordinates for the detection boxes of the detected objects.
As a basis I used the code from: https://github.com/soum-io/TensorFlowLiteInceptionTutorial/blob/master/app/src/main/java/com/soumio/inceptiontutorial/Classify.java.
import org.tensorflow.lite.Interpreter;
.
.
.
private final Interpreter.Options tfliteOptions = new Interpreter.Options();
private Interpreter tflite;
private byte[] [] labelProbArray = null;
private int[] intValues;
private int DIM_IMG_SIZE_X = 224;
private int DIM_IMG_SIZE_Y = 224;
private int DIM_PIXEL_SIZE = 3;
.
.
.
choosenModel = "detect_224_quant.tflite";
choosenLabel = "labelmap.txt";
intValues = new int[DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y];
tflite = new Interpreter(loadModelFile(), tfliteOptions);
labelList = loadLabelList();
imgData = ByteBuffer.allocateDirect(DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE);
imgData.order(ByteOrder.nativeOrder());
labelProbArray = new byte[1][labelList.size()];
Bitmap bitmap_orig = toBitmap(image);
Bitmap bitmap = getResizedBitmap(bitmap_orig, DIM_IMG_SIZE_X, DIM_IMG_SIZE_Y);
convertBitmapToByteBuffer(bitmap); //create imgData
tflite.run(imgData, labelProbArray);
printLabels();
Output for printLabels
private void printLabels() {
for (int i = 0; i < labelList.size(); i++){
sortedLabels.add(
new AbstractMap.SimpleEntry<>(labelList.get(i), (labelProbArray[0][i] & 0xff) / 255.0f));
if (sortedLabels.size() > RESULTS_TO_SHOW) {
sortedLabels.poll();
}
}
final int size = sortedLabels.size();
for (int i = 0; i < size; i++){
Map.Entry<String, Float> label = sortedLabels.poll();
topLabels[i] = label.getKey();
topConfidence[i] = String.format("%.0f%%", label.getValue()*100);
}
So I guess I'll just have to read the position/ coordinates from the detected object out of the output tensor (labelProbArray) somehow. But how? I hope somebody can help!