Spaces:
Sleeping
Sleeping
MuhammmadRizwanRizwan
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
import PIL.Image
|
5 |
+
|
6 |
+
# Class names for prediction
|
7 |
+
CLASS_NAMES = ['Alphida', 'Army worm', 'Bacterial blight', 'Healthy leaf', 'Leaf spot', 'Powdery Mildew']
|
8 |
+
|
9 |
+
# Load the pre-trained model
|
10 |
+
def load_model():
|
11 |
+
try:
|
12 |
+
model = tf.keras.models.load_model('cotton_disease_model.keras')
|
13 |
+
return model
|
14 |
+
except Exception as e:
|
15 |
+
print(f"Error loading model: {e}")
|
16 |
+
return None
|
17 |
+
|
18 |
+
# Prepare the image for prediction
|
19 |
+
def prepare_image(image):
|
20 |
+
# Resize the image
|
21 |
+
img = image.resize((180, 180))
|
22 |
+
# Convert to numpy array
|
23 |
+
img_array = np.array(img)
|
24 |
+
# Expand dimensions to create a batch
|
25 |
+
img_array = np.expand_dims(img_array, axis=0)
|
26 |
+
return img_array
|
27 |
+
|
28 |
+
# Prediction function
|
29 |
+
def predict_disease(model, image):
|
30 |
+
# Prepare the image
|
31 |
+
processed_image = prepare_image(image)
|
32 |
+
|
33 |
+
# Make prediction
|
34 |
+
predictions = model.predict(processed_image)
|
35 |
+
score = tf.nn.softmax(predictions[0])
|
36 |
+
|
37 |
+
# Get the predicted class and confidence
|
38 |
+
predicted_class_index = np.argmax(score)
|
39 |
+
predicted_class = CLASS_NAMES[predicted_class_index]
|
40 |
+
confidence = 100 * np.max(score)
|
41 |
+
|
42 |
+
return predicted_class, confidence
|
43 |
+
|
44 |
+
# Detailed disease information
|
45 |
+
DISEASE_INFO = {
|
46 |
+
"Alphida": {
|
47 |
+
"description": "Aphids are small insects that can damage cotton plants by sucking sap and spreading viruses.",
|
48 |
+
"impact": "Low to moderate crop damage",
|
49 |
+
"treatment": "Use insecticidal soaps, neem oil, or introduce natural predators like ladybugs"
|
50 |
+
},
|
51 |
+
"Army worm": {
|
52 |
+
"description": "Army worms can cause significant damage by consuming leaf tissue, potentially reducing crop yield.",
|
53 |
+
"impact": "High crop damage potential",
|
54 |
+
"treatment": "Apply appropriate insecticides, practice crop rotation, maintain field hygiene"
|
55 |
+
},
|
56 |
+
"Bacterial blight": {
|
57 |
+
"description": "A bacterial disease that causes lesions and wilting in cotton plants.",
|
58 |
+
"impact": "Moderate to severe crop damage",
|
59 |
+
"treatment": "Use copper-based bactericides, remove infected plants, practice crop rotation"
|
60 |
+
},
|
61 |
+
"Leaf spot": {
|
62 |
+
"description": "A fungal disease that creates spots on leaves, potentially affecting plant health and productivity.",
|
63 |
+
"impact": "Moderate crop damage",
|
64 |
+
"treatment": "Apply fungicides, ensure proper plant spacing, avoid overhead irrigation"
|
65 |
+
},
|
66 |
+
"Powdery Mildew": {
|
67 |
+
"description": "A fungal disease that appears as a white powdery substance on leaf surfaces.",
|
68 |
+
"impact": "Moderate crop damage",
|
69 |
+
"treatment": "Use sulfur-based fungicides, improve air circulation, avoid overhead watering"
|
70 |
+
},
|
71 |
+
"Healthy leaf": {
|
72 |
+
"description": "The cotton plant leaf is in good health with no visible diseases or pest damage.",
|
73 |
+
"impact": "No negative impact",
|
74 |
+
"treatment": "Continue regular plant care and monitoring"
|
75 |
+
}
|
76 |
+
}
|
77 |
+
|
78 |
+
# Main prediction interface
|
79 |
+
def cotton_disease_prediction(input_image):
|
80 |
+
# Load the model (you might want to load this once globally)
|
81 |
+
model = load_model()
|
82 |
+
|
83 |
+
if model is None:
|
84 |
+
return "Error: Model could not be loaded", None, None
|
85 |
+
|
86 |
+
if input_image is None:
|
87 |
+
return "Please upload an image", None, None
|
88 |
+
|
89 |
+
try:
|
90 |
+
# Convert to PIL Image if it's not already
|
91 |
+
if not isinstance(input_image, PIL.Image.Image):
|
92 |
+
input_image = PIL.Image.fromarray(input_image)
|
93 |
+
|
94 |
+
# Predict disease
|
95 |
+
predicted_class, confidence = predict_disease(model, input_image)
|
96 |
+
|
97 |
+
# Get detailed information
|
98 |
+
disease_details = DISEASE_INFO.get(predicted_class, {})
|
99 |
+
|
100 |
+
# Format result
|
101 |
+
result_text = f"Predicted Disease: {predicted_class}\n"
|
102 |
+
result_text += f"Confidence: {confidence:.2f}%\n\n"
|
103 |
+
result_text += f"Description: {disease_details.get('description', 'No additional information')}\n"
|
104 |
+
result_text += f"Impact: {disease_details.get('impact', 'Not specified')}\n"
|
105 |
+
result_text += f"Treatment: {disease_details.get('treatment', 'Consult an agricultural expert')}"
|
106 |
+
|
107 |
+
return result_text, predicted_class, confidence
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
return f"Error in prediction: {str(e)}", None, None
|
111 |
+
|
112 |
+
# Create Gradio Interface
|
113 |
+
def create_gradio_interface():
|
114 |
+
# Define input and output components
|
115 |
+
image_input = gr.Image(type="pil", label="Upload Cotton Leaf Image")
|
116 |
+
text_output = gr.Textbox(label="Prediction Results")
|
117 |
+
disease_label = gr.Label(label="Detected Disease")
|
118 |
+
confidence_number = gr.Number(label="Confidence Score")
|
119 |
+
|
120 |
+
# Create the Gradio interface
|
121 |
+
demo = gr.Interface(
|
122 |
+
fn=cotton_disease_prediction,
|
123 |
+
inputs=image_input,
|
124 |
+
outputs=[text_output, disease_label, confidence_number],
|
125 |
+
title="Cotton Plant Disease Detector",
|
126 |
+
description="Upload a cotton plant leaf image to detect potential diseases or assess health status.",
|
127 |
+
theme="huggingface",
|
128 |
+
examples=[
|
129 |
+
["example_healthy_leaf.jpg"],
|
130 |
+
["example_diseased_leaf.jpg"]
|
131 |
+
]
|
132 |
+
)
|
133 |
+
|
134 |
+
return demo
|
135 |
+
|
136 |
+
# Launch the app
|
137 |
+
if __name__ == "__main__":
|
138 |
+
demo = create_gradio_interface()
|
139 |
+
demo.launch()
|