object detection tensorflow lite android studio

Viewed 34

I'm currently making object detection app with android studio for mobile device.. I am currently stuck with how to show only the confidence instead of showing the names of the classes too.. I hope someone can enlighten me what to do.

Here's the code.

    **public void classifyImage(Bitmap image){
    try {
        Model model = Model.newInstance(getApplicationContext());

        // Creates inputs for reference.
        TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.FLOAT32);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * imageSize * imageSize * 3);
        byteBuffer.order(ByteOrder.nativeOrder());

        int [] intValues = new int[imageSize * imageSize];
        image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
        int pixel = 0;
        for(int i=0; i<imageSize; i++){
            for(int j=0; j<imageSize; j++){
                int val=intValues[pixel++];
                byteBuffer.putFloat(((val>>16)&0xFF)*(1.f/255.f));
                byteBuffer.putFloat(((val>>8)&0xFF)*(1.f/255.f));
                byteBuffer.putFloat((val&0xFF)*(1.f/255.f));
            }
        }
        inputFeature0.loadBuffer(byteBuffer);

        // Runs model inference and gets result.
        Model.Outputs outputs = model.process(inputFeature0);
        TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

        float[] confidences = outputFeature0.getFloatArray();
        int maxPos=0;
        float maxConfidence=0;
        for(int i=0;i<confidences.length;i++){
            if(confidences[i]>maxConfidence){
                maxConfidence=confidences[i];
                maxPos=i;
            }
        }
        String[] classes = {"Mouse","Keyboard","Speaker","Laptop"};
        result.setText(classes[maxPos]);

        String s = "";
        for(int i=0;i<classes.length;i++){
            s+=String.format("%s:%.1f%%\n",classes[i],confidences[i]*100);
        }
        confidence.setText(s);


        // Releases model resources if no longer used.
        model.close();
    } catch (IOException e) {
        // TODO Handle the exception
    }

}**
0 Answers
Related