Closed samsgates closed 4 years ago
I have tried to implement tflite file in android code as below, but image output not generated well
private fun preprocess(bitmap: Bitmap) {
bitmap.getPixels(this.pixelValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height) this.inputByteBuffer!!.rewind() for (row in 0 until this.inputSize!!.getHeight()) { for (col in 0 until this.inputSize!!.getWidth()) { val pixel = this.pixelValues!![row * this.inputSize!!.getWidth() + col] this.inputByteBuffer!!.putFloat( ( (pixel shr 16 and 255).toFloat() - 127.5f) / 1.0f) this.inputByteBuffer!!.putFloat( ( (pixel shr 8 and 255).toFloat() - 127.5f) / 1.0f) this.inputByteBuffer!!.putFloat( ( (pixel and 255).toFloat() - 127.5f) / 1.0f) } } }
outputByteBuffer!!.rewind() interpreter!!.run(inputByteBuffer, outputByteBuffer)
private fun postprocess(): IntArray {
val width = this.inputSize!!.getWidth() val height = this.inputSize!!.getHeight() val output = IntArray(width * height) this.outputByteBuffer!!.rewind() for (row in 0 until height) { val rowOffset = row * width * 3 * 4 for (col in 0 until width) { val colOffset = col * 3 * 4 val rValue = this.outputByteBuffer!!.getFloat(rowOffset + colOffset) val gValue = this.outputByteBuffer!!.getFloat(rowOffset + colOffset + 4) val bValue = this.outputByteBuffer!!.getFloat(rowOffset + colOffset + 8) val v1 = ((rValue.toInt() and 255 shl 16) ) + 127.5f val v2 = ( (gValue.toInt() and 255 shl 8) ) + 127.5f val v3 = ((bValue.toInt() and 255) ) + 127.5f val pixel = -16777216 + v1 + v2 + v3 output[row * width + col] = pixel.toInt() } } return output }
Sorry that I am not familiar with android. Maybe it's because pre-processing or post-processing? You can refer to my code for details.
I have tried to implement tflite file in android code as below, but image output not generated well
Preprocess image
private fun preprocess(bitmap: Bitmap) {
Run model
outputByteBuffer!!.rewind() interpreter!!.run(inputByteBuffer, outputByteBuffer)
Post process image
private fun postprocess(): IntArray {
Image output