Commit a3a9304b authored by Salika Madhushanka W.J's avatar Salika Madhushanka W.J

Merge branch 'IT19101620_2' into 'master'

added pests and diseases changes

See merge request !8
parents 710e9f1b 6ba3ebb8
...@@ -24,6 +24,15 @@ ...@@ -24,6 +24,15 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.EKetha"> android:theme="@style/Theme.EKetha">
<activity
android:name=".Pests"
android:exported="false" />
<activity
android:name=".Diseases"
android:exported="false" />
<activity
android:name=".pdHome"
android:exported="false" />
<activity <activity
android:name=".WeedMain" android:name=".WeedMain"
android:exported="false"> android:exported="false">
......
package com.example.eketha;
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.Model;
public class Diseases extends AppCompatActivity {
private Button camera, gallery;
private ImageView imageView;
private TextView result;
private int imageSize = 250;
private TextView solutionDiseases;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diseases);
camera = findViewById(R.id.btnTakePicture);
gallery = findViewById(R.id.btnLaunchGallery);
result = findViewById(R.id.result1);
imageView = findViewById(R.id.imageView1);
solutionDiseases=findViewById(R.id.solutionDeseases);
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 classifyImage1(Bitmap image){
try {
//Model model = Model.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.
Model.Outputs outputs = model.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[] classes = {"Bacterialblight", "Blast", "Brownspot", "Tungro"};
System.out.println("Log diseases"+classes[maxPos]);
result.setText(classes[maxPos]);
if(classes[maxPos].equals("Bacterialblight")){
solutionDiseases.setText("Bacterial blight is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}else if(classes[maxPos].equals("Blast")){
solutionDiseases.setText("Monitoring and Treatment Decisions Monitor to determine the need for treatments. Throughout the season, examine plants in several locations throughout the field for the presence of leaf lesions; intensify monitoring as plants approach the boot stage. If blast lesions are present and increasing just before the boot stage, a treatment may be justified. When making a treatment decision, consider disease progress, crop growth stage");
}else if(classes[maxPos].equals("Brownspot")){
solutionDiseases.setText("Monitoring and Treatment Decisions Monitor to determine the need for treatments. Throughout the season, examine plants in several locations throughout the field for the presence of leaf lesions; intensify monitoring as plants approach the boot stage. If blast lesions are present and increasing just before the boot stage, a treatment may be justified. When making a treatment decision, consider disease progress, crop growth stage");
}else if(classes[maxPos].equals("Tungro")){
solutionDiseases.setText("Tungro is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}
// Releases model resources if no longer used.
model.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);
classifyImage1(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);
classifyImage1(image);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
\ No newline at end of file
...@@ -35,14 +35,14 @@ public class MainActivity extends AppCompatActivity { ...@@ -35,14 +35,14 @@ public class MainActivity extends AppCompatActivity {
finish(); finish();
} }
}); });
// cardView1.setOnClickListener(new View.OnClickListener() { cardView1.setOnClickListener(new View.OnClickListener() {
// @Override @Override
// public void onClick(View view) { public void onClick(View view) {
// Intent intent = new Intent(getApplicationContext(),pdHome.class); Intent intent = new Intent(getApplicationContext(),pdHome.class);
// startActivity(intent); startActivity(intent);
// } }
// }); });
//
cardView2.setOnClickListener(new View.OnClickListener() { cardView2.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
......
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;
private TextView solutionPests;
@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);
solutionPests=findViewById(R.id.solutionPests);
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]);
if(clas[maxPos].equals("Caterpillar")){
solutionPests.setText("Caterpillar blight is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}else if(clas[maxPos].equals("Gryllotalpa")){
solutionPests.setText("Monitoring and Treatment Decisions Monitor to determine the need for treatments. Throughout the season, examine plants in several locations throughout the field for the presence of leaf lesions; intensify monitoring as plants approach the boot stage. If blast lesions are present and increasing just before the boot stage, a treatment may be justified. When making a treatment decision, consider disease progress, crop growth stage");
}else if(clas[maxPos].equals("Pezothrips")){
solutionPests.setText("Monitoring and Treatment Decisions Monitor to determine the need for treatments. Throughout the season, examine plants in several locations throughout the field for the presence of leaf lesions; intensify monitoring as plants approach the boot stage. If blast lesions are present and increasing just before the boot stage, a treatment may be justified. When making a treatment decision, consider disease progress, crop growth stage");
}else if(clas[maxPos].equals("Pieris canidia")){
solutionPests.setText("Tungro is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}
else if(clas[maxPos].equals("Prostigmata")){
solutionPests.setText("Tungro is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}
else if(clas[maxPos].equals("Protaetia cuprea")){
solutionPests.setText("Tungro is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}
else if(clas[maxPos].equals("white grubs")){
solutionPests.setText("Tungro is caused by the bacterium Pseudomonas syringae pv. syringae (Pss), which survives in diseased stem tissue (cankers), plant debris, and soil. Pss can be spread by insects and on pruning tools, but is more commonly spread by wind and rain");
}
// 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
package com.example.eketha;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class pdHome extends AppCompatActivity {
private LinearLayout layer1, layer2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pd_home);
layer1 = findViewById(R.id.layerPestsDP);
layer2 = findViewById(R.id.layerDiseasesPD);
// layer1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Intent intent = new Intent(getApplicationContext(), Diseases.class);
// startActivity(intent);
// }
// });
// layer2.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Intent intent = new Intent(getApplicationContext(), Pests.class);
// startActivity(intent);
// }
// });
}
}
\ 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=".Diseases">
<Button
android:id="@+id/btnTakePicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/btnLaunchGallery"
android:layout_centerInParent="true"
android:background="@drawable/round_bg"
android:text="Take Picture"
android:textAllCaps="false"
android:textSize="21sp"
android:textStyle="bold" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/btnLaunchGallery"
android:background="@drawable/round_bg"
android:text="Launch Gallery"
android:textAllCaps="false"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:textSize="21sp"
android:textStyle="bold"
/>
<ImageView
android:layout_width="370sp"
android:layout_height="370sp"
android:layout_centerHorizontal="true"
android:id="@+id/imageView1"
android:layout_marginTop="10sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Classified Diseases :"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/classified"
android:layout_below="@+id/imageView1"
android:layout_marginTop="10sp"
/>
<TextView
android:id="@+id/result1"
android:layout_width="150dp"
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" />
<TextView
android:id="@+id/solutionDeseases"
android:layout_width="wrap_content"
android:layout_marginTop="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/result1"
android:layout_centerHorizontal="true"
android:text=""
android:textColor="#C30000"
android:textSize="10sp"
android:textStyle="bold" />
</RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".pdHome">
<LinearLayout
android:id="@+id/layer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="120dp"
android:layout_marginTop="30dp"
app:srcCompat="@drawable/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Pest and Diseases Home"
android:textColor="@color/black"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:textSize="30dp"/>
</LinearLayout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
android:layout_marginRight="30dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp">
<LinearLayout
android:id="@+id/layerDiseasesPD"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:srcCompat="@drawable/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="36dp"
android:textColor="@color/white"
android:text="Pests"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
android:layout_marginRight="30dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp">
<LinearLayout
android:id="@+id/layerPestsDP"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:srcCompat="@drawable/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="36dp"
android:textColor="@color/white"
android:text="Diseases"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
\ 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" />
<TextView
android:id="@+id/solutionPests"
android:layout_width="wrap_content"
android:layout_marginTop="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/classified"
android:layout_centerHorizontal="true"
android:text=""
android:textColor="#C30000"
android:textSize="10sp"
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