Compare commits

..

2 Commits

Author SHA1 Message Date
Joshua Perry 135dfcc17d updates 2024-06-08 18:55:13 +01:00
Joshua Perry b881af0bcf converted views into fragments 2024-06-04 18:24:35 +01:00
21 changed files with 152 additions and 78 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

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -16,7 +16,7 @@
android:theme="@style/Theme.Dermy"
tools:targetApi="31" >
<activity android:name=".CameraActivity"
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -0,0 +1,47 @@
package xyz.r0r5chach.dermy;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import xyz.r0r5chach.dermy.fragments.CameraFragment;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CAMERA_PERMISSION = 200;
public MainActivity() {
super(R.layout.activity_main);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
startCamera();
}
}
private void startCamera() {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.fragment_container, CameraFragment.class, null)
.commit();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { //FIXME: deprecated listener fir request permissions
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show();
}
}
}
}

View File

@ -1,4 +0,0 @@
package xyz.r0r5chach.dermy;
public class MapActivity {
}

View File

@ -1,4 +0,0 @@
package xyz.r0r5chach.dermy;
public class NearbyActivity {
}

View File

@ -1,12 +1,19 @@
package xyz.r0r5chach.dermy;
package xyz.r0r5chach.dermy.fragments;
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;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
@ -15,38 +22,45 @@ import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
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;
public class CameraActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 200;
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;
private ImageCapture imageCapture;
private ExecutorService cameraExecutor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
previewView = view.findViewById(R.id.camera_preview);
previewView = findViewById(R.id.previewView);
Button captureButton = findViewById(R.id.captureButton);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
captureButton.setOnClickListener(v -> takePhoto());
previewView.setOnClickListener(v -> takePhoto());
cameraExecutor = Executors.newSingleThreadExecutor();
return view;
}
private void startCamera() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext());
cameraProviderFuture.addListener(() -> {
try {
@ -55,7 +69,7 @@ public class CameraActivity extends AppCompatActivity {
} catch (Exception e) {
e.printStackTrace(); //TODO: Replace with better logging
}
}, ContextCompat.getMainExecutor(this));
}, ContextCompat.getMainExecutor(requireContext()));
}
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
@ -71,38 +85,36 @@ public class CameraActivity extends AppCompatActivity {
}
private void takePhoto() {
File photoFile = new File(getExternalFilesDir(null), System.currentTimeMillis() + ".jpg");
File photoFile = new File(requireContext().getExternalFilesDir(null), System.currentTimeMillis() + ".jpg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
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
public void onError(@NonNull ImageCaptureException exception) {
exception.printStackTrace(); //TODO: Replace with better logging
Toast.makeText(CameraActivity.this, "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(requireContext(), "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
public void onDestroy() {
super.onDestroy();
cameraExecutor.shutdown();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show();
}
}
}
}

View File

@ -0,0 +1,4 @@
package xyz.r0r5chach.dermy.fragments;
public class MapFragment {
}

View File

@ -0,0 +1,4 @@
package xyz.r0r5chach.dermy.fragments;
public class NearbyFragment {
}

View File

@ -1,6 +1,6 @@
package xyz.r0r5chach.dermy;
package xyz.r0r5chach.dermy.fragments;
public class PreferencesActivity {
public class PreferencesFragment {
//TODO: Disclaimers
//TODO: Useful Links
//TODO: Export File

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,18 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<Button
android:id="@+id/captureButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:text="@string/image_capture_button_text" />
</androidx.camera.view.PreviewView>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="xyz.r0r5chach.dermy.fragments.CameraFragment" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</androidx.camera.view.PreviewView>
</androidx.constraintlayout.widget.ConstraintLayout>

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