0

I'm trying to create an app with a TensorFlow Lite model for recognising handwritten digits. I created a simple canvas for the user to draw on, which returns the Bitmap for whatever the user has drawn. The initial size of the bitmap is 523 x 1024 which I'm trying to scale down to 28 x 28 to pass in as an input to my model. However, the scaled-down image is almost to the point of unrecognizable.

I even tried to rescale the bitmap with https://stackoverflow.com/a/7468636/6712486 but to no avail. Attaching screenshots for reference Scaled Down Image. Uncompressed Image

Any insight would be greatly appreciated. Thanks

fun classify(bitmap: Bitmap) {
    check(isInterpreterInitialized) {"TFLite interpreter is not initialised"}
    val resizedImage = Bitmap.createScaledBitmap(bitmap, inputImageWidth, inputImageHeight, true)
    val bitmapByteBuffer = resizedImage?.toByteBuffer()
    getCompressedBitmap(bitmap)
    bitmapByteBuffer?.let {
      interpreter?.run(it, resultArray)
    }

  }
Kunal Kalra
  • 61
  • 1
  • 2

1 Answers1

0

You are scaling it down without preserving the aspect ratio that might be causing the issue. 28*28 pixel is really very low-resolution image so it is possible you won't be able to recognize it.

I am sure this is because of the aspect ratio. Preserve the aspect ratio - also, try to gradually decrease the width until it is unrecognizable. Here is the corresponding java code try this:-

public static Bitmap resizeBitmapWithPreservedAspectRatio(Bitmap bmp,
    int desiredWidth, int desiredHeight) {
    Matrix mat = new Matrix();
    float sx = (float) desiredWidth / bmp.getWidth();
    float sy = (float) desiredHeight / bmp.getHeight();
    if(desiredWidth>desiredHeight){
    mat.postScale(sx, sx);
    }else{
        mat.postScale(sy, sy);
    }
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
            mat, false);
    return bmp;
}

public static Bitmap resizeBitmapWithoutPreservingAspectRatio(Bitmap bmp,
    int desiredWidth, int desiredHeight) {
    Matrix mat = new Matrix();
    float sx = (float) desiredWidth / bmp.getWidth();
    float sy = (float) desiredHeight / bmp.getHeight();
    mat.postScale(sx, sy);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
            mat, false);
    return bmp;
}
Rahul Samaddar
  • 244
  • 2
  • 8