Skip to content

Instantly share code, notes, and snippets.

🤓
Did you do good today?

Mirosław Stanek frogermcs

Block or report user

Report or block frogermcs

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View SimpleObserver.java
public class SimpleObserver<T> implements Observer<T> {
///...
@Override
public void onError(Throwable e) {
view.showGeneralErrorPopup();
loggingManager.logException(e); // <-- !!!!!
}
}
View PaymentErrorListener.java
@AutoHandler
public interface PaymentErrorListener {
@ErrorCode(CODE_UNSUPPORTED_PREPAID_CARD)
void onUnsupportedPrepaidCardError();
@ErrorCode(CODE_UNSUPPORTED_CONSUMER_CARD)
void onUnsupportedConsumerCardError();
@ErrorCode(CODE_INSUFFICIENT_FUNDS)
View SimpleObserver.java
public class SimpleObserver<T> implements Observer<T> {
///...
@Override
public void onError(Throwable e) {
view.showGeneralErrorPopup();
}
}
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);
}
You can’t perform that action at this time.