Skip to content

Instantly share code, notes, and snippets.

🤓
Did you do good today?

Mirosław Stanek frogermcs

View GitHub Profile
View recognizeImage.java
public List<Classification> recognizeImage(Bitmap bitmap) {
ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
float[][] result = new float[1][GtsrbModelConfig.OUTPUT_LABELS.size()];
interpreter.run(byteBuffer, result);
return getSortedResult(result);
}
View ConvertToBytes.java
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(MODEL_INPUT_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[INPUT_IMG_SIZE_WIDTH * INPUT_IMG_SIZE_HEIGHT];
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
int pixel = 0;
for (int i = 0; i < INPUT_IMG_SIZE_WIDTH; ++i) {
for (int j = 0; j < INPUT_IMG_SIZE_HEIGHT; ++j) {
final int val = intValues[pixel++];
byteBuffer.putFloat((((val >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
View ImageUtils.java
public static Bitmap prepareImageForClassification(Bitmap bitmap) {
Paint paint = new Paint();
Bitmap finalBitmap = Bitmap.createScaledBitmap(
bitmap,
GtsrbModelConfig.INPUT_IMG_SIZE_WIDTH,
GtsrbModelConfig.INPUT_IMG_SIZE_HEIGHT,
false);
Canvas canvas = new Canvas(finalBitmap);
canvas.drawBitmap(finalBitmap, 0, 0, paint);
return finalBitmap;
View MobileNetConfig.java
public static final int INPUT_IMG_SIZE_WIDTH = 224;
public static final int INPUT_IMG_SIZE_HEIGHT = 224;
public static final int FLOAT_TYPE_SIZE = 4;
public static final int PIXEL_SIZE = 3;
public static final int MODEL_INPUT_SIZE = FLOAT_TYPE_SIZE * INPUT_IMG_SIZE_WIDTH * INPUT_IMG_SIZE_HEIGHT * PIXEL_SIZE;
public static final int IMAGE_MEAN = 0;
public static final float IMAGE_STD = 255.0f;
View MnistModelConfig.java
public class MnistModelConfig {
public static String MODEL_FILENAME = "mnist_model.tflite";
public static final int INPUT_IMG_SIZE_WIDTH = 28;
public static final int INPUT_IMG_SIZE_HEIGHT = 28;
public static final int FLOAT_TYPE_SIZE = 4;
public static final int PIXEL_SIZE = 1;
public static final int MODEL_INPUT_SIZE = FLOAT_TYPE_SIZE * INPUT_IMG_SIZE_WIDTH * INPUT_IMG_SIZE_HEIGHT * PIXEL_SIZE;
public static final List<String> OUTPUT_LABELS = Collections.unmodifiableList(
Arrays.asList("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"));
public static final int MAX_CLASSIFICATION_RESULTS = 3;
View ByteBuffer.java
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(MnistModelConfig.MODEL_INPUT_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
int[] pixels = new int[MnistModelConfig.INPUT_IMG_SIZE_WIDTH * MnistModelConfig.INPUT_IMG_SIZE_HEIGHT];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int pixel : pixels) {
float rChannel = (pixel >> 16) & 0xFF;
float gChannel = (pixel >> 8) & 0xFF;
float bChannel = (pixel) & 0xFF;
float pixelValue = (rChannel + gChannel + bChannel) / 3 / 255.f;
View Classification.java
public List<Classification> recognizeImage(Bitmap bitmap) {
ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
float[][] result = new float[1][MnistModelConfig.OUTPUT_LABELS.size()];
interpreter.run(byteBuffer, result);
return getSortedResult(result);
}
View MnistClassifier.java
public static MnistClassifier classifier(AssetManager assetManager, String modelPath) throws IOException {
ByteBuffer byteBuffer = loadModelFile(assetManager, modelPath);
Interpreter interpreter = new Interpreter(byteBuffer);
return new MnistClassifier(interpreter);
}
private static ByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
View appt.gradle
aaptOptions {
noCompress "tflite"
noCompress "lite"
}
View ImageUtils.java
/**
* Make bitmap appropriate size, greyscale and inverted. MNIST model is originally teached on
* dataset of images 28x28px with white letter written on black background.
*/
public static Bitmap prepareImageForClassification(Bitmap bitmap) {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
colorMatrix.postConcat(BLACKWHITE);
colorMatrix.postConcat(INVERT);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix);
You can’t perform that action at this time.