我正在制作我的第一个 ML 集成 android 应用程序,我正在尝试将此 ocr 模型添加到我的应用程序中。但我面临这个错误
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)
这就是我尝试整合它的方式
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()
我看到了许多其他相同的问题并尝试了这些解决方案,我尝试将 tensorBuffer 的大小更改 4 次,我尝试将 byteBuffer 直接传递给 model.process() 函数。但他们都没有工作。可能是什么问题,请帮助。