如何使用TensorFlow Lite解释器从检测到的对象中获取位置/坐标?

时间:2020-07-13 17:00:25

标签: android tensorflow object-detection tensorflow-lite android-camerax

我正在使用相机api(camerax)和自训练的tflite模型构建用于物体检测的应用程序。

对于tflite的集成,我尝试了两种方法。首先是ML Kit,其次是TensorFlow Lite推理。我更喜欢第二种方法,但是在获取检测到的对象的检测框的坐标方面存在问题

作为基础,我使用了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();

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);

    }

所以我想我只需要以某种方式从输出张量(labelProbArray)中读取检测到的对象的位置/坐标即可。但是如何?希望有人能帮忙!

0 个答案:

没有答案