onlyshrey98 commited on
Commit
4066d2a
·
verified ·
1 Parent(s): 5db052f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -60
app.py CHANGED
@@ -1,74 +1,77 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
  import numpy as np
 
 
4
  from PIL import Image
5
 
6
- # Load your trained model
7
- model = tf.keras.models.load_model('Model.hdf5')
8
 
9
- # Define the class names based on your image
10
- # I created this list from the dictionary you provided
11
- class_names = [
12
- 'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust', 'Apple___healthy',
13
- 'Blueberry___healthy', 'Cherry_(including_sour)___Powdery_mildew', 'Cherry_(including_sour)___healthy',
14
- 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot', 'Corn_(maize)___Common_rust', 'Corn_(maize)___Northern_Leaf_Blight',
15
- 'Corn_(maize)___healthy', 'Grape___Black_rot', 'Grape___Esca_(Black_Measles)', 'Grape___leaf_blight_(Isariopsis_Leaf_Spot)',
16
- 'Grape___healthy', 'Orange___Haunglongbing_(Citrus_greening)', 'Peach___Bacterial_spot', 'Peach___healthy',
17
- 'Pepper,_bell___Bacterial_spot', 'Pepper,_bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight',
18
- 'Potato___healthy', 'Raspberry___healthy', 'Soybean___healthy', 'Squash___Powdery_mildew', 'Strawberry___Leaf_scorch',
19
- 'Strawberry___healthy', 'Tomato___Bacterial_spot', 'Tomato___Early_blight', 'Tomato___Late_blight',
20
- 'Tomato___Leaf_Mold', 'Tomato___Septoria_leaf_spot', 'Tomato___Spider_mites Two-spotted_spider_mite',
21
- 'Tomato___Target_Spot', 'Tomato___Yellow_Leaf_Curl_Virus', 'Tomato___mosaic_virus', 'Tomato___healthy'
22
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
 
 
24
 
25
  # Prediction function
26
- def predict(image):
27
- """
28
- This function takes an image, preprocesses it, and returns the model's prediction.
29
- """
30
- # --- PREPROCESSING STEP ---
31
- # The input 'image' from Gradio is a NumPy array.
32
- # IMPORTANT: You might need to change the resize dimensions to match your model's input shape.
33
- # Common shapes are (224, 224) or (256, 256). I'll use (256, 256) as a default.
34
- img = Image.fromarray(image).resize((256, 256))
35
- img_array = np.array(img)
36
-
37
- # Normalize the image (if you did this during training)
38
- # Common normalization is to scale pixel values to [0, 1]
39
- img_array = img_array / 255.0
40
-
41
- # Add a batch dimension
42
- img_batch = np.expand_dims(img_array, axis=0)
43
 
44
- # --- PREDICTION STEP ---
45
- prediction = model.predict(img_batch)
46
-
47
- # Get the index of the highest probability
48
- predicted_class_index = np.argmax(prediction[0])
49
-
50
- # Get the predicted class name
51
- predicted_class_name = class_names[predicted_class_index]
52
-
53
- # Get the confidence score
54
- confidence = float(np.max(prediction[0]))
55
-
56
- # Return a dictionary of labels and their confidences
57
- return {predicted_class_name: confidence}
58
 
59
- # --- GRADIO INTERFACE ---
60
- iface = gr.Interface(
 
 
61
  fn=predict,
62
- inputs=gr.Image(label="Upload a photo of a crop leaf 🌿"),
63
- outputs=gr.Label(num_top_classes=1, label="Prediction Result"),
64
  title="Crop Disease Prediction",
65
- description="Upload an image of a crop leaf to predict its disease. This model can identify 38 different conditions.",
66
- examples=[
67
- # You can add paths to example images here if you have any
68
- # ["path/to/your/example1.jpg"],
69
- # ["path/to/your/example2.jpg"]
70
- ]
71
  )
72
 
73
- # Launch the app
74
- iface.launch()
 
1
  import gradio as gr
 
2
  import numpy as np
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing import image
5
  from PIL import Image
6
 
7
+ # Load your model
8
+ model = load_model("Model.hdf5")
9
 
10
+ # Class mapping (from your screenshot)
11
+ class_indices = {
12
+ 'Apple___Apple_scab': 0,
13
+ 'Apple___Black_rot': 1,
14
+ 'Apple___Cedar_apple_rust': 2,
15
+ 'Apple___healthy': 3,
16
+ 'Blueberry___healthy': 4,
17
+ 'Cherry_(including_sour)___Powdery_mildew': 5,
18
+ 'Cherry_(including_sour)___healthy': 6,
19
+ 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot': 7,
20
+ 'Corn_(maize)___Common_rust': 8,
21
+ 'Corn_(maize)___Northern_Leaf_Blight': 9,
22
+ 'Corn_(maize)___healthy': 10,
23
+ 'Grape___Black_rot': 11,
24
+ 'Grape___Esca_(Black_Measles)': 12,
25
+ 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)': 13,
26
+ 'Grape___healthy': 14,
27
+ 'Orange___Haunglongbing_(Citrus_greening)': 15,
28
+ 'Peach___Bacterial_spot': 16,
29
+ 'Peach___healthy': 17,
30
+ 'Pepper,_bell___Bacterial_spot': 18,
31
+ 'Pepper,_bell___healthy': 19,
32
+ 'Potato___Early_blight': 20,
33
+ 'Potato___Late_blight': 21,
34
+ 'Potato___healthy': 22,
35
+ 'Raspberry___healthy': 23,
36
+ 'Soybean___healthy': 24,
37
+ 'Squash___Powdery_mildew': 25,
38
+ 'Strawberry___Leaf_scorch': 26,
39
+ 'Strawberry___healthy': 27,
40
+ 'Tomato___Bacterial_spot': 28,
41
+ 'Tomato___Early_blight': 29,
42
+ 'Tomato___Late_blight': 30,
43
+ 'Tomato___Leaf_Mold': 31,
44
+ 'Tomato___Septoria_leaf_spot': 32,
45
+ 'Tomato___Spider_mites Two-spotted_spider_mite': 33,
46
+ 'Tomato___Target_Spot': 34,
47
+ 'Tomato___Tomato_Yellow_Leaf_Curl_Virus': 35,
48
+ 'Tomato___Tomato_mosaic_virus': 36,
49
+ 'Tomato___healthy': 37
50
+ }
51
 
52
+ # Reverse mapping
53
+ idx_to_class = {v: k for k, v in class_indices.items()}
54
 
55
  # Prediction function
56
+ def predict(img):
57
+ img = img.resize((224, 224)) # adjust if your model uses different input size
58
+ img_array = image.img_to_array(img) / 255.0
59
+ img_array = np.expand_dims(img_array, axis=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ predictions = model.predict(img_array)
62
+ predicted_class = np.argmax(predictions[0])
63
+ confidence = np.max(predictions[0]) * 100
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ return f"{idx_to_class[predicted_class]} ({confidence:.2f}% confidence)"
66
+
67
+ # Gradio interface
68
+ demo = gr.Interface(
69
  fn=predict,
70
+ inputs=gr.Image(type="pil", label="Upload a leaf image"),
71
+ outputs=gr.Textbox(label="Prediction"),
72
  title="Crop Disease Prediction",
73
+ description="Upload an image of a crop leaf to predict its disease."
 
 
 
 
 
74
  )
75
 
76
+ if __name__ == "__main__":
77
+ demo.launch()