How to implement realtime tflite model in Android Studio?

15 Views Asked by At

I am trying to do a realtime model implementation on a mobile application. I trained the model in Teachable Machine and exported it out as model_unquanted.tflite. When I import it into my Android Studio, it gives me the following Kotlin code to implement it:

val model = ModelUnquant.newInstance(context)

// Creates inputs for reference.
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 224, 224, 3), DataType.FLOAT32)
inputFeature0.loadBuffer(byteBuffer)

// Runs model inference and gets result.
val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer

// Releases model resources if no longer used.
model.close()

The following is my implementation on realtime update:

    override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {
            bitmap = textureView.bitmap!!
            val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 224, 224, 3), DataType.FLOAT32)

            val byteBuffer: ByteBuffer = ByteBuffer.allocate(224* 224* 3)
            byteBuffer.rewind()

            inputFeature0.loadBuffer(byteBuffer)

            val outputs = model.process(inputFeature0)
            val outputFeature0 = outputs.outputFeature0AsTensorBuffer

            var mutable = bitmap.copy(Bitmap.Config.ARGB_8888, true)
            val canvas = android.graphics.Canvas(mutable)

            val h = mutable.height
            val w = mutable.width

            val xPosition = 10 // Adjust this value as needed
            val yPosition = 30 // Adjust this value as needed

            canvas.drawText(outputFeature0.toString(), xPosition.toFloat(), yPosition.toFloat(), paint)

            imageView.setImageBitmap(mutable)

        }

The logcat error:

FATAL EXCEPTION: main
Process: com.example.tflite_realtime, PID: 14671
java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.
0

There are 0 best solutions below