I am making my first ML integrated android app and I am trying to add this ocr model into my app. But I am facing this error
Caused by: java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.
at org.tensorflow.lite.support.common.SupportPreconditions.checkArgument(SupportPreconditions.java:104)
at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:296)
at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:323)
at com.security.ml.MainActivity.onDetect(MainActivity.kt:36)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
at android.view.View.performClick(View.java:7585)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7541)
at android.view.View.access$3900(View.java:842)
at android.view.View$PerformClick.run(View.java:28875)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:255)
at android.app.ActivityThread.main(ActivityThread.java:8212)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
This is how I tried to integrate it
val model = Linear.newInstance(this)
val imageview = findViewById<ImageView>(R.id.imageview)
var bitmap = imageview.getDrawable().toBitmap()
bitmap = Bitmap.createScaledBitmap(bitmap, 32, 32, true)
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 32, 32, 1), DataType.FLOAT32)
val tensorImage = TensorImage(DataType.FLOAT32)
tensorImage.load(bitmap)
val byteBuffer = tensorImage.buffer
inputFeature0.loadBuffer(byteBuffer)
val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer
model.close()
Toast.makeText(this, outputFeature0.floatArray[0].toString(),Toast.LENGTH_LONG).show()
I saw many other same questions and tried those solution, I tried to change the size of tensorBuffer by 4 times, I tried to pass the byteBuffer directly into model.process() funtion. But none of them worked. What can be the problem, please Help.