package com.example.sg;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.sg.ml.ModelUnquant;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Disease extends AppCompatActivity {
private TextView textViewResult;
private ImageView imageView;
private Button buttonUploadImage;
private Button buttonGenerateResult;
private Button whatsApp;
private EditText editTextName, clinicalManagement;
private Bitmap selectedImage;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
Uri imageUri = data.getData();
try {
selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(),
imageUri);
imageView.setImageBitmap(selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disease);
textViewResult = findViewById(R.id.textViewResult);
imageView = findViewById(R.id.imageView);
buttonUploadImage = findViewById(R.id.buttonUploadImage);
buttonGenerateResult = findViewById(R.id.buttonGenerateResult);
editTextName = findViewById(R.id.editTextName);
whatsApp = findViewById(R.id.whatsAppBtn);
clinicalManagement = findViewById(R.id.clinicalId);
whatsApp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedImage != null) {
ByteBuffer byteBuffer = convertBitmapToByteBuffer(selectedImage);
float[] result = doInference(byteBuffer);
// Display the result
String diseaseType = getDiseaseType(result);
Uri imageUri = getImageUri(Disease.this, selectedImage);
shareToWhatsApp(diseaseType, imageUri);
} else {
Toast.makeText(Disease.this,
"Please select an image", Toast.LENGTH_SHORT).show();
}
}
});
buttonUploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
});
buttonGenerateResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedImage != null) {
processImage();
} else {
Toast.makeText(Disease.this,
"Please select an image", Toast.LENGTH_SHORT).show();
}
}
});
}
private void processImage() {
// if (selectedImage != null) {
// ByteBuffer byteBuffer = convertBitmapToByteBuffer(selectedImage);
// float[] result = doInference(byteBuffer);
//
// // Display the result
// String diseaseType = getDiseaseType(result);
// textViewResult.setText("Disease Type: " + diseaseType);
// } else {
// Toast.makeText(Disease.this,
// "Please select an image", Toast.LENGTH_SHORT).show();
// }
if (selectedImage != null) {
ByteBuffer byteBuffer = convertBitmapToByteBuffer(selectedImage);
float[] result = doInference(byteBuffer);
// Display the result
String diseaseType = getDiseaseType(result);
textViewResult.setText("Disease Type: " + diseaseType);
} else {
Toast.makeText(Disease.this,
"Please select an image", Toast.LENGTH_SHORT).show();
}
}
private void shareToWhatsApp(String diseaseType, Uri imageUri) {
String name = editTextName.getText().toString();
String clinic = clinicalManagement.getText().toString();
String message = "Name: " + name + "\nDisease Type: "
+ diseaseType + "\nTreatment: " + clinic;
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setPackage("com.whatsapp");
try {
startActivity(sendIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Disease.this,
"WhatsApp not installed.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("ShareToWhatsApp", "Error sharing to WhatsApp: " + e.getMessage());
Toast.makeText(Disease.this, "Error sharing to WhatsApp", Toast.LENGTH_SHORT).show();
}
}
private Uri getImageUri(Disease disease, Bitmap bitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(disease.getContentResolver(),
bitmap, "Title", null);
return Uri.parse(path);
}
private float[] doInference(ByteBuffer byteBuffer) {
float[] result = null;
try {
ModelUnquant model = ModelUnquant.newInstance(Disease.this);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.FLOAT32);
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
ModelUnquant.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
// Extract the result from the output tensor buffer
result = outputFeature0.getFloatArray();
// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
Log.e("Disease", "Error during inference: " + e.getMessage());
}
return result;
}
private String getDiseaseType(float[] result) {
if (result == null) {
return "Unknown"; // or handle the null case appropriately
}
String[] classes = {"Bacterial", "fungal", "healthy"};
int maxIdx = 0;
for (int i = 1; i < result.length; i++) {
if (result[i] > result[maxIdx]) {
maxIdx = i;
}
}
return classes[maxIdx];
//
// if (result == null || result.length != 3) {
// return "Unknown"; // Handle null or unexpected output
// }
// String[] classes = {"Bacterial", "Fungal", "Healthy"};
// int maxIdx = 0;
// float maxProbability = result[0];
//
// // Find the index of the highest probability
// for (int i = 1; i < result.length; i++) {
// if (result[i] > maxProbability) {
// maxIdx = i;
// maxProbability = result[i];
// }
// }
//
// // Define a confidence threshold (e.g., 0.7 or 70%)
// float confidenceThreshold = 0.7f;
//
// // If the highest probability is below the threshold, classify as "Unknown"
// if (maxProbability < confidenceThreshold) {
// return "Unknown";
// }
//
// return classes[maxIdx];
}
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
int width = 224;
int height = 224;
int channels = 3;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * width * height * channels);
byteBuffer.order(ByteOrder.nativeOrder());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pixel = 0;
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
int pixelValue = pixels[pixel++];
byteBuffer.putFloat(Color.red(pixelValue) / 255.0f);
byteBuffer.putFloat(Color.green(pixelValue) / 255.0f);
byteBuffer.putFloat(Color.blue(pixelValue) / 255.0f);
}
}
return byteBuffer;
}
}
//XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/disease"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="@drawable/background_gradient"
android:padding="20sp">
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32sp"
android:text="Disease Detection"
android:textColor="@color/black"
android:textSize="32sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameView"
android:layout_marginTop="15dp"
android:hint="Plant Name"
android:inputType="text"
android:textColorHint="@color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/textViewUploadImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Upload Plant Image:"
android:textColor="@color/black"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/textViewUploadImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/buttonUploadImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:backgroundTint="@color/black"
android:text="Upload" />
<Button
android:id="@+id/buttonGenerateResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonUploadImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:backgroundTint="@color/black"
android:text="Generate Result" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonUploadImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Result:"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/whatsAppBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/clinicalId"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:backgroundTint="@color/black"
android:text="WhatsApp" />
<EditText
android:id="@+id/clinicalId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textViewResult"
android:layout_marginTop="8dp"
android:hint="Treatment"
android:inputType="text"
android:textColorHint="@color/black" />
</RelativeLayout>
package com.example.imageprediction;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.imageprediction.ml.ModelUnquant;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class MainActivity extends AppCompatActivity {
// Widget Declaration
private TextView textViewResult;
private ImageView imageView;
private Button buttonUploadImage;
private Button buttonGenerateResult;
private Button whatsApp;
private EditText editTextName, clinicalManagement;
private Bitmap selectedImage;
//Open My Gallery getting my image
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
Uri imageUri = data.getData();
try {
selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(),
imageUri);
imageView.setImageBitmap(selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Wiring
textViewResult = findViewById(R.id.textViewResult);
imageView = findViewById(R.id.imageView);
buttonUploadImage = findViewById(R.id.buttonUploadImage);
buttonGenerateResult = findViewById(R.id.buttonGenerateResult);
editTextName = findViewById(R.id.editTextName);
whatsApp = findViewById(R.id.whatsAppBtn);
clinicalManagement = findViewById(R.id.clinicalId);
//Upload Button
buttonUploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
});
buttonGenerateResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedImage != null) {
processImage();
} else {
Toast.makeText(MainActivity.this,
"Please select an image", Toast.LENGTH_SHORT).show();
}
}
});
}
private void processImage(){
if (selectedImage != null) {
ByteBuffer byteBuffer = convertBitmapToByteBuffer(selectedImage);
float[] result = doInference(byteBuffer);
// Display the result
String diseaseType = getDiseaseType(result);
textViewResult.setText("Disease Type: " + diseaseType);
} else {
Toast.makeText(MainActivity.this,
"Please select an image", Toast.LENGTH_SHORT).show();
}
}
private String getDiseaseType(float[] result) {
if (result == null) {
return "Unknown"; // or handle the null case appropriately
}
String[] classes = {"Apple", "banana", "beetroot","carrot"};
int maxIdx = 0;
for (int i = 1; i < result.length; i++) {
if (result[i] > result[maxIdx]) {
maxIdx = i;
}
}
return classes[maxIdx];
}
private float[] doInference(ByteBuffer byteBuffer) {
float[] result = null;
try {
ModelUnquant model = ModelUnquant.newInstance(this);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.FLOAT32);
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
ModelUnquant.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
}
return result;
}
private ByteBuffer convertBitmapToByteBuffer(Bitmap selectedImage) {
int width = 224;
int height = 224;
int channels = 3;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * width * height * channels);
byteBuffer.order(ByteOrder.nativeOrder());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pixel = 0;
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
int pixelValue = pixels[pixel++];
byteBuffer.putFloat(Color.red(pixelValue) / 255.0f);
byteBuffer.putFloat(Color.green(pixelValue) / 255.0f);
byteBuffer.putFloat(Color.blue(pixelValue) / 255.0f);
}
}
return byteBuffer;
}
}