Compare commits
No commits in common. "135dfcc17df3d2c5a2b0d4b25298a76ffd0c323f" and "500c40a12532f90b739c4daa3dd91d251f744ca6" have entirely different histories.
135dfcc17d
...
500c40a125
|
|
@ -4,14 +4,6 @@
|
|||
<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>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
android:theme="@style/Theme.Dermy"
|
||||
tools:targetApi="31" >
|
||||
|
||||
<activity android:name=".MainActivity"
|
||||
<activity android:name=".CameraActivity"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
|
|
|||
|
|
@ -1,19 +1,12 @@
|
|||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
import static xyz.r0r5chach.dermy.MainActivity.REQUEST_CAMERA_PERMISSION;
|
||||
package xyz.r0r5chach.dermy;
|
||||
|
||||
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;
|
||||
|
|
@ -22,45 +15,38 @@ 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;
|
||||
|
||||
import xyz.r0r5chach.dermy.R;
|
||||
import xyz.r0r5chach.dermy.models.Model;
|
||||
import xyz.r0r5chach.dermy.models.binary_classifiers.BinaryMobileNetV2;
|
||||
|
||||
public class CameraFragment extends Fragment {
|
||||
public class CameraActivity extends AppCompatActivity {
|
||||
private static final int REQUEST_CAMERA_PERMISSION = 200;
|
||||
private PreviewView previewView;
|
||||
private ImageCapture imageCapture;
|
||||
private ExecutorService cameraExecutor;
|
||||
|
||||
@Override
|
||||
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);
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
previewView = findViewById(R.id.previewView);
|
||||
Button captureButton = findViewById(R.id.captureButton);
|
||||
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
startCamera();
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
|
||||
}
|
||||
|
||||
previewView.setOnClickListener(v -> takePhoto());
|
||||
captureButton.setOnClickListener(v -> takePhoto());
|
||||
|
||||
cameraExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void startCamera() {
|
||||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext());
|
||||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
|
||||
|
||||
cameraProviderFuture.addListener(() -> {
|
||||
try {
|
||||
|
|
@ -69,7 +55,7 @@ public class CameraFragment extends Fragment {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace(); //TODO: Replace with better logging
|
||||
}
|
||||
}, ContextCompat.getMainExecutor(requireContext()));
|
||||
}, ContextCompat.getMainExecutor(this));
|
||||
}
|
||||
|
||||
private void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
|
||||
|
|
@ -85,36 +71,38 @@ public class CameraFragment extends Fragment {
|
|||
}
|
||||
|
||||
private void takePhoto() {
|
||||
File photoFile = new File(requireContext().getExternalFilesDir(null), System.currentTimeMillis() + ".jpg");
|
||||
File photoFile = new File(getExternalFilesDir(null), System.currentTimeMillis() + ".jpg");
|
||||
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
|
||||
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(requireContext()), new ImageCapture.OnImageSavedCallback() {
|
||||
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
|
||||
@Override
|
||||
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
||||
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);
|
||||
}
|
||||
//TODO: Add image path to bundle to pass onto next activity
|
||||
//TODO: Move to EditEntryActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull ImageCaptureException exception) {
|
||||
exception.printStackTrace(); //TODO: Replace with better logging
|
||||
Toast.makeText(requireContext(), "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(CameraActivity.this, "Error saving photo: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
protected 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
|
||||
public class MapActivity {
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package xyz.r0r5chach.dermy;
|
||||
|
||||
public class NearbyActivity {
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package xyz.r0r5chach.dermy.fragments;
|
||||
package xyz.r0r5chach.dermy;
|
||||
|
||||
public class PreferencesFragment {
|
||||
public class PreferencesActivity {
|
||||
//TODO: Disclaimers
|
||||
//TODO: Useful Links
|
||||
//TODO: Export File
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
public class MapFragment {
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
package xyz.r0r5chach.dermy.fragments;
|
||||
|
||||
public class NearbyFragment {
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
package xyz.r0r5chach.dermy.models;
|
||||
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.AssetManager;
|
||||
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(Resources resources, int modelId) throws IOException {
|
||||
modelInterpreter = new Interpreter(loadModel(resources, modelId));
|
||||
public Model(AssetManager assetManager, String modelPath) throws IOException {
|
||||
modelInterpreter = new Interpreter(loadModel(assetManager));
|
||||
this.modelPath = modelPath;
|
||||
}
|
||||
|
||||
public float[] runInference(Bitmap image, int classes) {
|
||||
|
|
@ -32,8 +32,8 @@ public class Model {
|
|||
return output[0];
|
||||
}
|
||||
|
||||
public MappedByteBuffer loadModel(Resources resources, int modelId) throws IOException {
|
||||
AssetFileDescriptor fileDescriptor = resources.openRawResourceFd(modelId);
|
||||
public MappedByteBuffer loadModel(AssetManager assetManager) throws IOException {
|
||||
AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath);
|
||||
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 * 280 * 280 * 3);
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(4 * 224 * 224 * 3);
|
||||
buffer.order(ByteOrder.nativeOrder());
|
||||
|
||||
int[] intValues = new int[280 * 280];
|
||||
int[] intValues = new int[244 * 244];
|
||||
|
||||
image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
|
||||
|
||||
int pixel = 0;
|
||||
for (int i = 0; i < 280; ++i) {
|
||||
for (int j = 0; j < 280; ++j) {
|
||||
for (int i = 0; i < 224; ++i) {
|
||||
for (int j = 0; j < 224; ++j) {
|
||||
int val = intValues[pixel++];
|
||||
buffer.putFloat((((val >> 16) & 0xFF) - 127) / 128.0f);
|
||||
buffer.putFloat((((val >> 8) & 0xFF) - 127) / 128.0f);
|
||||
|
|
|
|||
|
|
@ -1,16 +1,15 @@
|
|||
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(Resources resources) throws IOException {
|
||||
super(resources, R.raw.binary_efficientnet_lite3);
|
||||
public BinaryEfficientNetLite3(AssetManager assetManager) throws IOException {
|
||||
super(assetManager, modelPath);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,15 @@
|
|||
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(Resources resources) throws IOException {
|
||||
super(resources, R.raw.binary_efficientnet_lite4);
|
||||
public BinaryEfficientNetLite4(AssetManager assetManager, String modelPath) throws IOException {
|
||||
super(assetManager, modelPath);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +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 BinaryMobileNetV2 extends Model {
|
||||
private static final String modelPath = ""; //TODO: Add model path
|
||||
|
||||
public BinaryMobileNetV2(Resources resources) throws IOException {
|
||||
super(resources, R.raw.binary_mobilenet_v2);
|
||||
public BinaryMobileNetV2(AssetManager assetManager) throws IOException {
|
||||
super(assetManager, modelPath);
|
||||
}
|
||||
|
||||
//TODO: Add runInference method that runs super but transforms output into usable format
|
||||
|
|
|
|||
|
|
@ -1,12 +1,18 @@
|
|||
<?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.fragment.app.FragmentContainerView
|
||||
android:id="@+id/fragment_container"
|
||||
android:layout_height="match_parent">
|
||||
<androidx.camera.view.PreviewView
|
||||
android:id="@+id/previewView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:name="xyz.r0r5chach.dermy.fragments.CameraFragment" />
|
||||
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.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
<?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>
|
||||
|
|
@ -0,0 +1 @@
|
|||
../../../../dermy-models/models
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1 @@
|
|||
Subproject commit ab886132c76f98b766f81bf7468b60beb419662c
|
||||
Loading…
Reference in New Issue