Iam trying to implement tensorflow lite models in android device with grayscale input, but most of the documentation that i found is using rgb input. Is there any example of how to using tflite file in android with grayscale image or it is still not supported ?
0
votes
When you have the gray scale, you only have 1 channel of an image. The rest is analogical with the rgb image.
- Benjamin
i already tried that but there is always an error with BufferOverflowException
- Christian Jeremi
Then the problem is probably with the Buffer Overflow, not with the grayscale image.
- Benjamin
1 Answers
0
votes
Here's how I am converting my RGB bitmap to a corresponding grayscale byte buffer directly:
private ByteBuffer getByteBuffer(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
ByteBuffer mImgData = ByteBuffer
.allocateDirect(4 * width * height);
mImgData.order(ByteOrder.nativeOrder());
int[] pixels = new int[width*height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int pixel : pixels) {
mImgData.putFloat((float) Color.red(pixel));
}
return mImgData;
}
If you need normalized values [0, 1] just divide by 255.0:
float value = (float) Color.red(pixel)/255.0f;
mImgData.putFloat(value);
You can then use this in your interpreter as:
ByteBuffer input = getByteBuffer(bitmap);
tflite.run(input, outputValue);
P.S. I was also getting BufferOverflowException because I was decoding 28x28 image from drawables which was being resized to 56x56 due to dpi conversions. I put it in drawable-nodpi and then it worked correctly. Make sure your image is the right size.