我正在尝试使用TensorFlow Lite模型创建一个用于识别手写数字的应用。我创建了一个简单的画布供用户绘制,该画布将返回用户绘制的内容的位图。位图的初始大小为523 x 1024,我正尝试将其缩小为28 x 28,以作为模型的输入传入。但是,按比例缩小的图像几乎无法辨认。
我什至试图用https://stackoverflow.com/a/7468636/6712486重新缩放位图,但无济于事。附上屏幕截图以供参考Scaled Down Image。 Uncompressed Image
任何见识将不胜感激。谢谢
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)
}
}
您正在按比例缩小它,但未保留可能导致问题的纵横比。 28 * 28像素实际上是非常低分辨率的图像,因此您可能无法识别它。
我确定这是由于纵横比。保留长宽比-另外,尝试逐渐减小宽度,直到无法识别为止。这是相应的Java代码,请尝试以下操作:-
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;
}