vishwak1 commited on
Commit
5dbeaf8
·
verified ·
1 Parent(s): af78724

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
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 your trained .h5 model
8
  model = load_model("model.h5")
9
 
10
- # Optional: Replace this list with your actual class labels if available
11
- class_labels = [f"Class_{i}" for i in range(model.output_shape[1])]
 
 
 
 
12
 
13
  # Prediction function
14
- def predict_disease(image):
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
- # Predict
22
- prediction = model.predict(img_array)
23
- predicted_class_index = int(np.argmax(prediction, axis=1)[0])
24
- confidence = float(np.max(prediction))
25
 
26
- predicted_label = class_labels[predicted_class_index]
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=predict_disease,
35
  inputs=gr.Image(type="pil"),
36
  outputs="text",
37
  title="Plant Disease Classifier",
38
- description="Upload a plant leaf image to detect disease using a trained ML model (.h5)."
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()