Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import torch.nn as nn
|
|
4 |
from torchvision import transforms, models
|
5 |
from PIL import Image
|
6 |
import torch.nn.functional as F
|
7 |
-
import
|
8 |
|
9 |
class TomatoLeafDiseaseDetectionApp:
|
10 |
def __init__(self):
|
@@ -42,12 +42,12 @@ class TomatoLeafDiseaseDetectionApp:
|
|
42 |
model.eval() # Set the model to evaluation mode
|
43 |
return model
|
44 |
|
45 |
-
def predict_disease(self,
|
46 |
"""
|
47 |
Predict the tomato leaf disease from the given image.
|
48 |
|
49 |
Args:
|
50 |
-
|
51 |
|
52 |
Returns:
|
53 |
tuple: Predicted disease name and confidence score.
|
@@ -59,7 +59,6 @@ class TomatoLeafDiseaseDetectionApp:
|
|
59 |
transforms.ToTensor(),
|
60 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # Normalize for EfficientNet
|
61 |
])
|
62 |
-
image = Image.open(image_path).convert("RGB")
|
63 |
input_tensor = transform(image).unsqueeze(0).to(self.device)
|
64 |
|
65 |
# Perform prediction
|
@@ -74,44 +73,23 @@ class TomatoLeafDiseaseDetectionApp:
|
|
74 |
except Exception as e:
|
75 |
return f"Error: {str(e)}", 0.0
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
Launch the Gradio interface for tomato leaf disease detection.
|
80 |
-
"""
|
81 |
-
def classify_image(image_path):
|
82 |
-
disease_name, confidence = self.predict_disease(image_path)
|
83 |
-
return disease_name, f"Confidence: {confidence:.2f}"
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
output_label = gr.Label(label="Predicted Disease")
|
92 |
-
confidence_text = gr.Textbox(label="Confidence Score")
|
93 |
-
|
94 |
-
with gr.Row():
|
95 |
-
button = gr.Button(value="Detect Disease")
|
96 |
-
|
97 |
-
button.click(
|
98 |
-
classify_image,
|
99 |
-
inputs=[input_image],
|
100 |
-
outputs=[output_label, confidence_text]
|
101 |
-
)
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
"tomato_earlt_blight.jpg", # Replace with your example paths
|
106 |
-
"yellow_leaf_curl.jpg",
|
107 |
-
],
|
108 |
-
inputs=[input_image],
|
109 |
-
outputs=[output_label, confidence_text],
|
110 |
-
label="Example Images"
|
111 |
-
)
|
112 |
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
-
|
117 |
-
app.gradio_interface()
|
|
|
4 |
from torchvision import transforms, models
|
5 |
from PIL import Image
|
6 |
import torch.nn.functional as F
|
7 |
+
import streamlit as st
|
8 |
|
9 |
class TomatoLeafDiseaseDetectionApp:
|
10 |
def __init__(self):
|
|
|
42 |
model.eval() # Set the model to evaluation mode
|
43 |
return model
|
44 |
|
45 |
+
def predict_disease(self, image):
|
46 |
"""
|
47 |
Predict the tomato leaf disease from the given image.
|
48 |
|
49 |
Args:
|
50 |
+
image (PIL.Image): Input image.
|
51 |
|
52 |
Returns:
|
53 |
tuple: Predicted disease name and confidence score.
|
|
|
59 |
transforms.ToTensor(),
|
60 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # Normalize for EfficientNet
|
61 |
])
|
|
|
62 |
input_tensor = transform(image).unsqueeze(0).to(self.device)
|
63 |
|
64 |
# Perform prediction
|
|
|
73 |
except Exception as e:
|
74 |
return f"Error: {str(e)}", 0.0
|
75 |
|
76 |
+
def main():
|
77 |
+
st.title("Tomato Leaf Disease Detection")
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
# Upload image
|
80 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
81 |
+
if uploaded_file is not None:
|
82 |
+
# Open the image
|
83 |
+
image = Image.open(uploaded_file).convert("RGB")
|
84 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
# Initialize the app
|
87 |
+
app = TomatoLeafDiseaseDetectionApp()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
# Predict disease
|
90 |
+
disease_name, confidence = app.predict_disease(image)
|
91 |
+
st.write(f"Predicted Disease: {disease_name}")
|
92 |
+
st.write(f"Confidence Score: {confidence:.2f}")
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
+
main()
|
|