Skip to content
Snippets Groups Projects
Commit 279233a8 authored by “kamon's avatar “kamon
Browse files

reconnaissance d'image sans cadres ni exceptions implémentée

parent 621dfac4
No related branches found
No related tags found
1 merge request!2merge all branches, model integration
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="CHOOSE_PER_TEST" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="jbr-17" /> <option name="gradleJvm" value="jbr-17" />
<option name="modules"> <option name="modules">
......
...@@ -6,6 +6,7 @@ import android.os.Bundle; ...@@ -6,6 +6,7 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
...@@ -19,10 +20,8 @@ public class MainActivity extends AppCompatActivity { ...@@ -19,10 +20,8 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//Button recognizeButton = findViewById(R.id.buttonRecognize); //setContentView(R.layout.activity_main1);
setContentView(R.layout.activity_main1);
TextView textView = findViewById(R.id.testInference);
/*
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
...@@ -35,7 +34,8 @@ public class MainActivity extends AppCompatActivity { ...@@ -35,7 +34,8 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
finish(); finish();
} }
}, delayMillis); */ }, delayMillis);
} }
......
package com.example.myapplication; package com.example.myapplication;
import android.Manifest; import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
...@@ -13,6 +17,7 @@ import androidx.core.content.ContextCompat; ...@@ -13,6 +17,7 @@ import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -20,10 +25,16 @@ import android.widget.Button; ...@@ -20,10 +25,16 @@ import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import java.io.IOException;
import java.util.List;
public class PhotoPage extends Fragment { public class PhotoPage extends Fragment {
private static final int CAMERA_REQUEST_CODE = 3;
private static final int GALLERY_REQUEST_CODE = 1;
private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
Button cameraButton, folderButton; Button cameraButton, folderButton;
TextView textView,txtView; TextView textView, txtView;
ImageView imageView; ImageView imageView;
int imageSize = 256; int imageSize = 256;
...@@ -47,9 +58,9 @@ public class PhotoPage extends Fragment { ...@@ -47,9 +58,9 @@ public class PhotoPage extends Fragment {
public void onClick(View v) { public void onClick(View v) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 3); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
} else { } else {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 100); requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
} }
} }
}); });
...@@ -58,10 +69,86 @@ public class PhotoPage extends Fragment { ...@@ -58,10 +69,86 @@ public class PhotoPage extends Fragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Intent folderIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Intent folderIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(folderIntent, 1); startActivityForResult(folderIntent, GALLERY_REQUEST_CODE);
} }
}); });
return view; return view;
} }
}
\ No newline at end of file @Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null || resultCode != getActivity().RESULT_OK) {
Log.e("PhotoPage", "Error: Data is null or result not OK");
return;
}
switch (requestCode) {
case CAMERA_REQUEST_CODE:
// Handle photo taken with camera
Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
if (cameraImage != null) {
imageView.setImageBitmap(cameraImage);
processImage(cameraImage); // Pass the image for further processing
}
break;
case GALLERY_REQUEST_CODE:
// Handle image picked from the gallery
Uri selectedImageUri = data.getData();
if (selectedImageUri != null) {
try {
ContentResolver resolver = getContext().getContentResolver();
Bitmap galleryImage = MediaStore.Images.Media.getBitmap(resolver, selectedImageUri);
imageView.setImageBitmap(galleryImage);
processImage(galleryImage); // Pass the image for further processing
} catch (IOException e) {
e.printStackTrace();
Log.e("PhotoPage", "Error loading image from gallery: " + e.getMessage());
}
}
break;
}
}
private void processImage(Bitmap bitmap) {
// Resize and/or preprocess the image for your recognition program
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imageSize, imageSize, true);
// You can pass this `scaledBitmap` to your prediction program
Log.d("PhotoPage", "Bitmap processed for prediction: " + scaledBitmap.toString());
try {
TestModeleTflite testModeleTflite = new TestModeleTflite(this.getContext());
List<String> speciesDetected = testModeleTflite.recognizeSpeciesClass(scaledBitmap);
StringBuilder recognationOutput = new StringBuilder("Espèces détectées : ");
for(String specie:speciesDetected){
recognationOutput.append(specie).append("\t");
}
TextView textView = getView().findViewById(R.id.textView2);
textView.setText(recognationOutput);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
} else {
Log.e("PhotoPage", "Camera permission denied");
textView.setText("Camera permission denied. Please allow it to use this feature.");
}
}
}
}
...@@ -237,6 +237,22 @@ public class TestModeleTflite { ...@@ -237,6 +237,22 @@ public class TestModeleTflite {
} }
} }
public Bitmap recognizeSpecies(Bitmap inputImage){
TensorBuffer outputBuffer = predict(inputImage);
List<BoundingBox> boxes = processOutput(outputBuffer);
return annotateImage(inputImage, boxes);
}
public List<String> recognizeSpeciesClass(Bitmap inputImage){
ArrayList<String> speciesNames = new ArrayList<>();
TensorBuffer outputBuffer = predict(inputImage);
List<BoundingBox> boxes = processOutput(outputBuffer);
for(BoundingBox box:boxes){
if(!speciesNames.contains(box.clsName)) speciesNames.add(box.clsName);
}
return speciesNames;
}
public static void main(String [] args){ public static void main(String [] args){
/* /*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment