Tensorflow Lite,YOLO Step By Step Error Finding

Viewed 52

enter image description here

enter image description here

enter image description here

enter image description here

Console Error enter image description here

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button selectBtn, predictBtn, captureBtn;
    TextView result;
    ImageView _imageView;
    Bitmap bitmap;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getPermission();

        String[] labels = new String[5];
        int cnt = 0;
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open("label.txt")));
            String line = bufferedReader.readLine();
            while(line!= null) {
                labels[cnt] = line;
                cnt++;
                line = bufferedReader.readLine();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        selectBtn = findViewById(R.id.selectBtn);
        predictBtn = findViewById(R.id.predictBtn);
        captureBtn = findViewById(R.id.captureBtn);
        result = findViewById(R.id.result);
        _imageView = findViewById(R.id.imageView);

        selectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent,10);
            }
        });
        captureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,12);


            }
        });


        predictBtn.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View view){

                try {
                    Yolov5sFp16 model = Yolov5sFp16.newInstance(MainActivity.this);// Creates inputs for reference.

                    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{4, 640, 640, 3}, DataType.FLOAT32);

                    bitmap = Bitmap.createScaledBitmap(bitmap, 640 ,640,true );

                    inputFeature0.loadBuffer(TensorImage.fromBitmap(bitmap).getBuffer());// Runs model inference and gets result.

                    Yolov5sFp16.Outputs outputs = model.process(inputFeature0);

                    TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

                    result.setText(labels[getMax(outputFeature0.getFloatArray())]+" ");


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




            }
        });
    }
    int getMax(float[] arr){
        int max = 0;
        for(int i=0; i<arr.length;i++){
            if(arr[i] > arr[max]) max = i;
        }
        return max;
    }

  /*  int getMax(float[] arr){
        int max = 0;
        for(int i=0; i<arr.length;i++){
            if(arr[i] > arr[max]) max = i;
        }
        return max;
    }
*/
     void getPermission(){
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             if(checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 11);
             }
         }
     }

     @Override
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        if(requestCode==11){
            if(grantResults.length>0){
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    this.getPermission();
                }
            }


        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
     }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        if(requestCode == 10){
            if(data!= null ){
                Uri uri = data.getData();
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
                    _imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        else if(requestCode==12){
            bitmap = (Bitmap) data.getExtras().get("data");
            _imageView.setImageBitmap(bitmap);

        }
        super.onActivityResult(requestCode, resultCode, data);


    }


}

yolov5s-fp16.tflite

try {
    Yolov5sFp16 model = Yolov5sFp16.newInstance(context);

    // Creates inputs for reference.
    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 640, 640, 3}, DataType.FLOAT32);
    inputFeature0.loadBuffer(byteBuffer);

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

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

continuation of this question

I want to make an application that guesses the name of the selected food image.I used yolov5 for training. Test images in colab are successful

I did step by step debugging.It gives error on this line. Please help me.

inputFeature0.loadBuffer(TensorImage.fromBitmap(bitmap).getBuffer());// Runs model inference and gets result.

What should I do ?

0 Answers
Related