diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml index b268ef3..93b1bd2 100644 --- a/.idea/deploymentTargetSelector.xml +++ b/.idea/deploymentTargetSelector.xml @@ -4,6 +4,14 @@ diff --git a/app/src/main/java/xyz/r0r5chach/dermy/fragments/CameraFragment.java b/app/src/main/java/xyz/r0r5chach/dermy/fragments/CameraFragment.java index a0de84c..8255ab5 100644 --- a/app/src/main/java/xyz/r0r5chach/dermy/fragments/CameraFragment.java +++ b/app/src/main/java/xyz/r0r5chach/dermy/fragments/CameraFragment.java @@ -5,6 +5,8 @@ import static xyz.r0r5chach.dermy.MainActivity.REQUEST_CAMERA_PERMISSION; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -24,11 +26,15 @@ import androidx.fragment.app.Fragment; import com.google.common.util.concurrent.ListenableFuture; import java.io.File; +import java.io.IOException; +import java.util.Arrays; import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import xyz.r0r5chach.dermy.R; +import xyz.r0r5chach.dermy.models.Model; +import xyz.r0r5chach.dermy.models.binary_classifiers.BinaryMobileNetV2; public class CameraFragment extends Fragment { private PreviewView previewView; @@ -84,8 +90,16 @@ public class CameraFragment extends Fragment { imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(requireContext()), new ImageCapture.OnImageSavedCallback() { @Override public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { - //TODO: Add image path to bundle to pass onto next activity - //TODO: Move to EditEntryActivity + Bitmap photo = BitmapFactory.decodeFile(photoFile.getAbsolutePath()); + try { + //TODO: Change Model based on preference + Model model = new BinaryMobileNetV2(getResources()); + + float[] results = model.runInference(photo, 2); + Toast.makeText(requireContext(), "Results = " + Arrays.toString(results), Toast.LENGTH_LONG).show(); + } catch (IOException e) { + throw new RuntimeException(e); + } } @Override @@ -96,6 +110,8 @@ public class CameraFragment extends Fragment { }); } + + @Override public void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/xyz/r0r5chach/dermy/models/Model.java b/app/src/main/java/xyz/r0r5chach/dermy/models/Model.java index 8c881e1..c64ddd3 100644 --- a/app/src/main/java/xyz/r0r5chach/dermy/models/Model.java +++ b/app/src/main/java/xyz/r0r5chach/dermy/models/Model.java @@ -1,7 +1,7 @@ package xyz.r0r5chach.dermy.models; import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; +import android.content.res.Resources; import android.graphics.Bitmap; import org.tensorflow.lite.Interpreter; @@ -15,13 +15,13 @@ import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; +import xyz.r0r5chach.dermy.R; + public class Model { protected final Interpreter modelInterpreter; - protected final String modelPath; - public Model(AssetManager assetManager, String modelPath) throws IOException { - modelInterpreter = new Interpreter(loadModel(assetManager)); - this.modelPath = modelPath; + public Model(Resources resources, int modelId) throws IOException { + modelInterpreter = new Interpreter(loadModel(resources, modelId)); } public float[] runInference(Bitmap image, int classes) { @@ -32,8 +32,8 @@ public class Model { return output[0]; } - public MappedByteBuffer loadModel(AssetManager assetManager) throws IOException { - AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath); + public MappedByteBuffer loadModel(Resources resources, int modelId) throws IOException { + AssetFileDescriptor fileDescriptor = resources.openRawResourceFd(modelId); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); //FIXME: add try-with-resources FileChannel fileChannel = inputStream.getChannel(); @@ -44,16 +44,16 @@ public class Model { } public static ByteBuffer preprocessImage(Bitmap image) { - ByteBuffer buffer = ByteBuffer.allocateDirect(4 * 224 * 224 * 3); + ByteBuffer buffer = ByteBuffer.allocateDirect(4 * 280 * 280 * 3); buffer.order(ByteOrder.nativeOrder()); - int[] intValues = new int[244 * 244]; + int[] intValues = new int[280 * 280]; image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight()); int pixel = 0; - for (int i = 0; i < 224; ++i) { - for (int j = 0; j < 224; ++j) { + for (int i = 0; i < 280; ++i) { + for (int j = 0; j < 280; ++j) { int val = intValues[pixel++]; buffer.putFloat((((val >> 16) & 0xFF) - 127) / 128.0f); buffer.putFloat((((val >> 8) & 0xFF) - 127) / 128.0f); diff --git a/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite3.java b/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite3.java index 133843e..8a43c67 100644 --- a/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite3.java +++ b/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite3.java @@ -1,15 +1,16 @@ package xyz.r0r5chach.dermy.models.binary_classifiers; import android.content.res.AssetManager; +import android.content.res.Resources; import java.io.IOException; +import xyz.r0r5chach.dermy.R; import xyz.r0r5chach.dermy.models.Model; public class BinaryEfficientNetLite3 extends Model { - private static final String modelPath = ""; //TODO: Add model Path - public BinaryEfficientNetLite3(AssetManager assetManager) throws IOException { - super(assetManager, modelPath); + public BinaryEfficientNetLite3(Resources resources) throws IOException { + super(resources, R.raw.binary_efficientnet_lite3); } } diff --git a/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite4.java b/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite4.java index b658adf..2aacb49 100644 --- a/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite4.java +++ b/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryEfficientNetLite4.java @@ -1,15 +1,16 @@ package xyz.r0r5chach.dermy.models.binary_classifiers; import android.content.res.AssetManager; +import android.content.res.Resources; import java.io.IOException; +import xyz.r0r5chach.dermy.R; import xyz.r0r5chach.dermy.models.Model; public class BinaryEfficientNetLite4 extends Model { - private static final String modelPath = ""; //TODO: Add model path - public BinaryEfficientNetLite4(AssetManager assetManager, String modelPath) throws IOException { - super(assetManager, modelPath); + public BinaryEfficientNetLite4(Resources resources) throws IOException { + super(resources, R.raw.binary_efficientnet_lite4); } } diff --git a/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryMobileNetV2.java b/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryMobileNetV2.java index e27c26f..0df025c 100644 --- a/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryMobileNetV2.java +++ b/app/src/main/java/xyz/r0r5chach/dermy/models/binary_classifiers/BinaryMobileNetV2.java @@ -1,16 +1,17 @@ package xyz.r0r5chach.dermy.models.binary_classifiers; import android.content.res.AssetManager; +import android.content.res.Resources; import java.io.IOException; +import xyz.r0r5chach.dermy.R; import xyz.r0r5chach.dermy.models.Model; public class BinaryMobileNetV2 extends Model { - private static final String modelPath = ""; //TODO: Add model path - public BinaryMobileNetV2(AssetManager assetManager) throws IOException { - super(assetManager, modelPath); + public BinaryMobileNetV2(Resources resources) throws IOException { + super(resources, R.raw.binary_mobilenet_v2); } //TODO: Add runInference method that runs super but transforms output into usable format diff --git a/app/src/main/res/models b/app/src/main/res/models deleted file mode 120000 index 1c60a1f..0000000 --- a/app/src/main/res/models +++ /dev/null @@ -1 +0,0 @@ -../../../../dermy-models/models \ No newline at end of file diff --git a/app/src/main/res/raw/binary_efficientnet_lite3.tflite b/app/src/main/res/raw/binary_efficientnet_lite3.tflite new file mode 100644 index 0000000..7c3a7ca Binary files /dev/null and b/app/src/main/res/raw/binary_efficientnet_lite3.tflite differ diff --git a/app/src/main/res/raw/binary_efficientnet_lite4.tflite b/app/src/main/res/raw/binary_efficientnet_lite4.tflite new file mode 100644 index 0000000..12228ae Binary files /dev/null and b/app/src/main/res/raw/binary_efficientnet_lite4.tflite differ diff --git a/app/src/main/res/raw/binary_mobilenet_v2.tflite b/app/src/main/res/raw/binary_mobilenet_v2.tflite new file mode 100644 index 0000000..4aa48ba Binary files /dev/null and b/app/src/main/res/raw/binary_mobilenet_v2.tflite differ diff --git a/dermy-models b/dermy-models deleted file mode 160000 index ab88613..0000000 --- a/dermy-models +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ab886132c76f98b766f81bf7468b60beb419662c