Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ from torchvision import models, transforms
|
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
|
6 |
-
|
7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
|
9 |
# Load the trained model
|
@@ -27,7 +27,26 @@ transform = transforms.Compose([
|
|
27 |
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
28 |
])
|
29 |
|
30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def predict(image):
|
32 |
# Ensure image is in RGB
|
33 |
image = image.convert("RGB")
|
@@ -47,21 +66,27 @@ def predict(image):
|
|
47 |
probabilities = torch.nn.functional.softmax(outputs, dim=1)[0]
|
48 |
confidence = probabilities[predicted_class.item()].item()
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
-
#
|
56 |
def launch_interface():
|
57 |
# Create a Gradio interface
|
58 |
iface = gr.Interface(
|
59 |
-
theme=
|
|
|
|
|
|
|
60 |
fn=predict,
|
61 |
inputs=gr.Image(type="pil", label="Upload Rice Leaf Image"),
|
62 |
-
outputs=gr.
|
63 |
-
title="Rice Disease Classification",
|
64 |
-
description="Upload a rice leaf image to detect
|
65 |
examples=[
|
66 |
["https://doa.gov.lk/wp-content/uploads/2020/06/brownspot3-1024x683.jpg"],
|
67 |
["https://arkansascrops.uada.edu/posts/crops/rice/images/Fig%206%20Rice%20leaf%20blast%20coalesced%20lesions.png"],
|
@@ -72,7 +97,10 @@ def launch_interface():
|
|
72 |
|
73 |
return iface
|
74 |
|
|
|
|
|
|
|
75 |
# Launch the interface
|
76 |
if __name__ == "__main__":
|
77 |
interface = launch_interface()
|
78 |
-
interface.launch(share=True)
|
|
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Define the device
|
7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
|
9 |
# Load the trained model
|
|
|
27 |
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
28 |
])
|
29 |
|
30 |
+
# Color mapping for labels
|
31 |
+
label_colors = {
|
32 |
+
"Brown Spot": "#b2ff00",
|
33 |
+
"Healthy": "#2ecc71",
|
34 |
+
"Leaf Blast": "#ff00d4",
|
35 |
+
"Neck Blast": "#ffd100"
|
36 |
+
}
|
37 |
+
|
38 |
+
# Function to get color based on confidence
|
39 |
+
def get_confidence_color(confidence):
|
40 |
+
if confidence < 0.25:
|
41 |
+
return "#e74c3c" # Red
|
42 |
+
elif confidence < 0.50:
|
43 |
+
return "#f39c12"
|
44 |
+
elif confidence < 0.75:
|
45 |
+
return "#00b9ff" # Yellow
|
46 |
+
else:
|
47 |
+
return "#13ff00" # Green
|
48 |
+
|
49 |
+
# Updated Prediction Function
|
50 |
def predict(image):
|
51 |
# Ensure image is in RGB
|
52 |
image = image.convert("RGB")
|
|
|
66 |
probabilities = torch.nn.functional.softmax(outputs, dim=1)[0]
|
67 |
confidence = probabilities[predicted_class.item()].item()
|
68 |
|
69 |
+
# Generate styled output
|
70 |
+
label_color = label_colors.get(predicted_label, "#FFFFFF") # Default White if not found
|
71 |
+
confidence_color = get_confidence_color(confidence)
|
72 |
+
|
73 |
+
result = f"<div style='color:{label_color}; font-size:30px; font-weight:bold;'>{predicted_label}</div>"
|
74 |
+
result += f"<div style='color:{confidence_color}; font-size:25px; font-weight:bold;'>Confidence: {confidence*100:.2f}%</div>"
|
75 |
+
return result
|
76 |
|
77 |
+
# Updated Gradio Interface
|
78 |
def launch_interface():
|
79 |
# Create a Gradio interface
|
80 |
iface = gr.Interface(
|
81 |
+
theme=gr.themes.Citrus(
|
82 |
+
primary_hue="emerald",
|
83 |
+
neutral_hue="slate"
|
84 |
+
),
|
85 |
fn=predict,
|
86 |
inputs=gr.Image(type="pil", label="Upload Rice Leaf Image"),
|
87 |
+
outputs=gr.HTML(label="Prediction Results"),
|
88 |
+
title="<span style='color: #00fff7; font-size:40px; font-weight: bold;'>Rice Disease Classification</span>",
|
89 |
+
description="<span style='color: lightblue; font-size:26px;'>Upload a rice leaf image to detect its condition (Brown Spot, Healthy, Leaf Blast, or Neck Blast)</span>",
|
90 |
examples=[
|
91 |
["https://doa.gov.lk/wp-content/uploads/2020/06/brownspot3-1024x683.jpg"],
|
92 |
["https://arkansascrops.uada.edu/posts/crops/rice/images/Fig%206%20Rice%20leaf%20blast%20coalesced%20lesions.png"],
|
|
|
97 |
|
98 |
return iface
|
99 |
|
100 |
+
# Load the model globally
|
101 |
+
model = load_model()
|
102 |
+
|
103 |
# Launch the interface
|
104 |
if __name__ == "__main__":
|
105 |
interface = launch_interface()
|
106 |
+
interface.launch(share=True)
|