Update app.py
Browse files
app.py
CHANGED
|
@@ -4,38 +4,38 @@ from tensorflow.keras.preprocessing.image import img_to_array
|
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
model = load_model("model.h5")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Prediction function
|
| 14 |
-
def
|
| 15 |
try:
|
| 16 |
-
# Resize and preprocess image
|
| 17 |
image = image.resize((225, 225))
|
| 18 |
img_array = img_to_array(image) / 255.0
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
confidence = float(np.max(prediction))
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return f"Prediction: {predicted_label} (Confidence: {confidence:.2f})"
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
# Gradio UI
|
| 33 |
interface = gr.Interface(
|
| 34 |
-
fn=
|
| 35 |
inputs=gr.Image(type="pil"),
|
| 36 |
outputs="text",
|
| 37 |
title="Plant Disease Classifier",
|
| 38 |
-
description="Upload a plant
|
| 39 |
)
|
| 40 |
|
| 41 |
interface.launch()
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
+
# Load the .h5 model
|
| 8 |
model = load_model("model.h5")
|
| 9 |
|
| 10 |
+
# Class index to label map
|
| 11 |
+
label_map = {
|
| 12 |
+
0: "Healthy",
|
| 13 |
+
1: "Powdery",
|
| 14 |
+
2: "Rust"
|
| 15 |
+
}
|
| 16 |
|
| 17 |
# Prediction function
|
| 18 |
+
def predict(image):
|
| 19 |
try:
|
|
|
|
| 20 |
image = image.resize((225, 225))
|
| 21 |
img_array = img_to_array(image) / 255.0
|
| 22 |
img_array = np.expand_dims(img_array, axis=0)
|
| 23 |
|
| 24 |
+
predictions = model.predict(img_array)
|
| 25 |
+
predicted_index = int(np.argmax(predictions, axis=1)[0])
|
| 26 |
+
predicted_class = label_map.get(predicted_index, "Unknown")
|
|
|
|
| 27 |
|
| 28 |
+
return predicted_class
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
# Gradio UI
|
| 33 |
interface = gr.Interface(
|
| 34 |
+
fn=predict,
|
| 35 |
inputs=gr.Image(type="pil"),
|
| 36 |
outputs="text",
|
| 37 |
title="Plant Disease Classifier",
|
| 38 |
+
description="Upload a plant image to detect whether it is Healthy, Powdery, or Rust."
|
| 39 |
)
|
| 40 |
|
| 41 |
interface.launch()
|