This commit is contained in:
Joshua Perry 2024-06-08 18:55:13 +01:00
parent b881af0bcf
commit 135dfcc17d
11 changed files with 49 additions and 24 deletions

View File

@ -4,6 +4,14 @@
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-06-04T18:01:16.272682016Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/home/r0r5chach/.config/.android/avd/Pixel_8_Pro_API_30.avd" />
</handle>
</Target>
</DropdownSelection>
<DialogSelection />
</SelectionState>
</selectionStates>
</component>

View File

@ -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();

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -1 +0,0 @@
../../../../dermy-models/models

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1 +0,0 @@
Subproject commit ab886132c76f98b766f81bf7468b60beb419662c