HashamUllah commited on
Commit
347d632
1 Parent(s): d3c66f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -10
app.py CHANGED
@@ -2,18 +2,16 @@ import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
- import io
6
  import json
7
 
8
  # Load the TensorFlow model
9
- model = tf.keras.models.load_model('plant_disease_detection.h5')
10
 
11
  # Load categories
12
  with open('categories.json') as f:
13
  categories = json.load(f)
14
 
15
  def preprocess_image(image):
16
- # Convert the image to a NumPy array
17
  image = image.resize((224, 224)) # Adjust size as needed
18
  image_array = np.array(image) / 255.0 # Normalize to [0, 1]
19
  image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
@@ -21,17 +19,11 @@ def preprocess_image(image):
21
 
22
  def predict(image):
23
  image_array = preprocess_image(image)
24
-
25
- # Make prediction
26
  predictions = model.predict(image_array)
27
  predicted_class = np.argmax(predictions, axis=1)[0]
28
-
29
- # Map to category names
30
  predicted_label = categories.get(str(predicted_class), 'Unknown')
31
-
32
  return predicted_label, float(predictions[0][predicted_class])
33
 
34
- # Create a Gradio interface
35
  iface = gr.Interface(
36
  fn=predict,
37
  inputs=gr.Image(type="pil"),
@@ -40,6 +32,5 @@ iface = gr.Interface(
40
  description="Upload an image of a plant leaf to detect if it has any diseases."
41
  )
42
 
43
- # Launch the interface
44
  if __name__ == "__main__":
45
  iface.launch()
 
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
 
5
  import json
6
 
7
  # Load the TensorFlow model
8
+ model = tf.keras.models.load_model('plant_disease_detection_compatible.h5')
9
 
10
  # Load categories
11
  with open('categories.json') as f:
12
  categories = json.load(f)
13
 
14
  def preprocess_image(image):
 
15
  image = image.resize((224, 224)) # Adjust size as needed
16
  image_array = np.array(image) / 255.0 # Normalize to [0, 1]
17
  image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
 
19
 
20
  def predict(image):
21
  image_array = preprocess_image(image)
 
 
22
  predictions = model.predict(image_array)
23
  predicted_class = np.argmax(predictions, axis=1)[0]
 
 
24
  predicted_label = categories.get(str(predicted_class), 'Unknown')
 
25
  return predicted_label, float(predictions[0][predicted_class])
26
 
 
27
  iface = gr.Interface(
28
  fn=predict,
29
  inputs=gr.Image(type="pil"),
 
32
  description="Upload an image of a plant leaf to detect if it has any diseases."
33
  )
34
 
 
35
  if __name__ == "__main__":
36
  iface.launch()