Commit 130bc741 authored by Salika952's avatar Salika952

add diseases UI and logic class

parent efeb0f51
package com.example.eketha;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;
import com.example.eketha.ml.Pestmodel;
public class Pests extends AppCompatActivity {
private Button camera, gallery;
private ImageView imageView;
private TextView result;
private int imageSize = 250;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pests);
camera = findViewById(R.id.btnTakePicturePests);
gallery = findViewById(R.id.btnLaunchGalleryPests);
result = findViewById(R.id.resultPests);
imageView = findViewById(R.id.imageViewPests);
camera.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 3);
} else {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 100);
}
}
});
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(cameraIntent, 1);
}
});
}
public void classifyImage2(Bitmap image){
try {
Pestmodel pModel = Pestmodel.newInstance(getApplicationContext());
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 250, 250, 3}, DataType.FLOAT32);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * imageSize * imageSize * 3);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[imageSize * imageSize];
image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int pixel = 0;
//iterate over each pixel and extract R, G, and B values. Add those values individually to the byte buffer.
for(int i = 0; i < imageSize; i ++){
for(int j = 0; j < imageSize; j++){
int val = intValues[pixel++]; // RGB
byteBuffer.putFloat(((val >> 16) & 0xFF) * (1.f / 1));
byteBuffer.putFloat(((val >> 8) & 0xFF) * (1.f / 1));
byteBuffer.putFloat((val & 0xFF) * (1.f / 1));
}
}
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
Pestmodel.Outputs outputs = pModel.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
float[] confidences = outputFeature0.getFloatArray();
// find the index of the class with the biggest confidence.
int maxPos = 0;
float maxConfidence = 0;
for (int i = 0; i < confidences.length; i++) {
if (confidences[i] > maxConfidence) {
maxConfidence = confidences[i];
maxPos = i;
}
}
String[] clas = {"Caterpillar"," Gryllotalpa", "Pezothrips","Pieris canidia" , "Prostigmata" , "Protaetia cuprea" , "Trombidium" ,"white grubs"};
Log.d("log name",clas[maxPos]);
result.setText(clas[maxPos]);
// Releases model resources if no longer used.
pModel.close();
} catch (IOException e) {
// TODO Handle the exception
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode == RESULT_OK){
if(requestCode == 3){
Bitmap image = (Bitmap) data.getExtras().get("data");
int dimension = Math.min(image.getWidth(), image.getHeight());
image = ThumbnailUtils.extractThumbnail(image, dimension, dimension);
imageView.setImageBitmap(image);
image = Bitmap.createScaledBitmap(image, imageSize, imageSize, false);
classifyImage2(image);
}else{
Uri dat = data.getData();
Bitmap image = null;
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), dat);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(image);
image = Bitmap.createScaledBitmap(image, imageSize, imageSize, false);
classifyImage2(image);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Pests">
<Button
android:id="@+id/btnTakePicturePests"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/btnLaunchGalleryPests"
android:layout_centerInParent="true"
android:background="@drawable/round_bg"
android:text="Take Picture"
android:textAllCaps="false"
android:textSize="21sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnLaunchGalleryPests"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/round_bg"
android:text="Launch Gallery"
android:textAllCaps="false"
android:textSize="21sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageViewPests"
android:layout_width="370sp"
android:layout_height="370sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Classified as pests:"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/classified"
android:layout_below="@+id/imageViewPests"
android:layout_marginTop="10sp"
/>
<TextView
android:id="@+id/resultPests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/classified"
android:layout_centerHorizontal="true"
android:text=""
android:textColor="#C30000"
android:textSize="27sp"
android:textStyle="bold" />
</RelativeLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment