Baskar2005 commited on
Commit
1957ca3
·
verified ·
1 Parent(s): fdff8ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -40
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 gradio as gr
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, image_path):
46
  """
47
  Predict the tomato leaf disease from the given image.
48
 
49
  Args:
50
- image_path (str): Path to the input image.
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
- def gradio_interface(self):
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
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
- gr.HTML("<center><h1>Tomato Leaf Disease Detection</h1></center>")
87
-
88
- with gr.Row():
89
- input_image = gr.Image(type="filepath", label="Upload Leaf Image")
90
- with gr.Column():
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
- gr.Examples(
104
- examples=[
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
- demo.launch(debug=True)
 
 
 
114
 
115
  if __name__ == "__main__":
116
- app = TomatoLeafDiseaseDetectionApp()
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()