sankalp2606 commited on
Commit
6dce674
Β·
verified Β·
1 Parent(s): c01f171

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -46
app.py CHANGED
@@ -4,70 +4,66 @@ from ultralytics import YOLO
4
  import gradio as gr
5
  from gradio_client import Client, handle_file
6
 
7
- # 🌿 Dictionary containing disease names, descriptions, remedies, and recommended fertilizers
8
  disease_info = {
9
  "bacterial spot": {
10
- "description": "Bacterial spot is a common disease affecting tomato and pepper plants, causing dark, water-soaked spots on leaves and fruits.",
11
  "remedy": "Remove infected plant debris, use copper-based fungicides.",
12
  "fertilizer": "Use potassium-rich fertilizers to strengthen plant immunity."
13
  },
14
  "early blight": {
15
- "description": "Early blight is a fungal disease that causes dark concentric rings on leaves, leading to defoliation.",
16
  "remedy": "Apply fungicides, practice crop rotation.",
17
  "fertilizer": "Use nitrogen-rich fertilizers to improve plant resistance."
18
  },
19
  "healthy": {
20
- "description": "The plant is healthy and does not show any signs of disease.",
21
  "remedy": "No action needed.",
22
- "fertilizer": "Balanced NPK fertilizers can maintain plant health."
23
  },
24
  "late blight": {
25
- "description": "Late blight is a devastating fungal disease that spreads rapidly under wet conditions.",
26
- "remedy": "Remove infected plants, use fungicides.",
27
- "fertilizer": "Calcium-based fertilizers help reduce susceptibility."
28
  },
29
  "leaf miner": {
30
- "description": "Leaf miners create winding tunnels in leaves as larvae feed on plant tissue.",
31
  "remedy": "Use insecticidal sprays, remove affected leaves.",
32
- "fertilizer": "Organic compost or vermicompost can strengthen plants against pests."
33
  },
34
  "leaf mold": {
35
- "description": "Leaf mold appears as a yellowing of leaves with fuzzy mold on the undersides.",
36
- "remedy": "Improve air circulation, use fungicides.",
37
- "fertilizer": "Apply phosphorus-based fertilizers to enhance root health."
38
  },
39
  "mosaic virus": {
40
- "description": "Mosaic virus causes a mottled, yellow-green appearance on leaves, affecting photosynthesis.",
41
  "remedy": "Remove infected plants, control aphids.",
42
- "fertilizer": "Avoid nitrogen excess; use balanced NPK fertilizers."
43
  },
44
  "septoria": {
45
- "description": "Septoria leaf spot appears as numerous small, circular brown spots on lower leaves.",
46
  "remedy": "Remove infected leaves, use fungicides.",
47
- "fertilizer": "Use sulfur-based fertilizers to reduce fungal spread."
48
  },
49
  "spider mites": {
50
- "description": "Spider mites cause stippling and webbing on leaves, leading to leaf drop.",
51
  "remedy": "Use miticides, introduce beneficial insects.",
52
- "fertilizer": "Silicon-based fertilizers improve plant resistance to pests."
53
  },
54
  "yellow leaf curl virus": {
55
- "description": "This virus causes curled, yellowing leaves and stunted growth, transmitted by whiteflies.",
56
  "remedy": "Remove infected plants, control whiteflies.",
57
- "fertilizer": "Zinc and boron-based fertilizers can reduce symptoms."
58
  }
59
  }
60
 
61
  def custom_images(img, target_size=(512, 512)): # Fixed size
62
- """
63
- Resizes the input image to a fixed target size.
64
- """
65
  return cv2.resize(img, target_size)
66
 
67
  def plant_disease_detect(img, model, original_size):
68
- """
69
- Performs plant disease detection using the YOLO model.
70
- """
71
  detect_result = model(img)
72
  detect_img = detect_result[0].plot()
73
  detections = detect_result[0].boxes.data.tolist()
@@ -76,40 +72,48 @@ def plant_disease_detect(img, model, original_size):
76
  return detect_img, classes
77
 
78
  def predict(img):
 
79
  model = YOLO('crop_disease_model.pt')
80
  original_size = (img.shape[1], img.shape[0])
81
- resized_image = custom_images(img) # Automatically resized
82
  detect_img, classes = plant_disease_detect(resized_image, model, original_size)
83
-
 
84
  unique_classes = list(set(classes))
85
- class_table = [[
86
- cls,
87
- disease_info.get(cls.lower(), {}).get("description", "No description available"),
88
- disease_info.get(cls.lower(), {}).get("remedy", "No remedy available"),
89
- disease_info.get(cls.lower(), {}).get("fertilizer", "No fertilizer recommendation available")
90
- ] for cls in unique_classes]
91
-
92
- return detect_img, class_table
 
 
93
 
94
- # 🌱 Gradio Interface
95
  with gr.Blocks() as demo:
96
- gr.Markdown("# 🌿 Crop Disease Diagnosis")
97
- gr.Markdown("Upload an image of a plant to detect diseases and get recommendations.")
98
 
99
  with gr.Row():
100
  with gr.Column():
101
- img_input = gr.Image(type="numpy", label="πŸ“· Upload Image")
102
- submit_btn = gr.Button("πŸ” Detect Disease")
103
  with gr.Column():
104
- img_output = gr.Image(label="πŸ–ΌοΈ Processed Image")
105
- class_table = gr.Dataframe(headers=["Disease", "Description", "Remedy", "Recommended Fertilizer"], label="πŸ“‹ Disease Details")
 
 
 
 
106
 
107
  submit_btn.click(fn=predict, inputs=[img_input], outputs=[img_output, class_table])
108
 
109
- # Enable API Mode
110
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
111
 
112
- # Hugging Face API Access
113
  client = Client("sankalp2606/Plant-Disease_diagnosis")
114
  print("HELLO")
115
  print(client.endpoints)
 
4
  import gradio as gr
5
  from gradio_client import Client, handle_file
6
 
7
+ # 🌿 Dictionary containing disease details
8
  disease_info = {
9
  "bacterial spot": {
10
+ "description": "Causes dark, water-soaked spots on leaves and fruits.",
11
  "remedy": "Remove infected plant debris, use copper-based fungicides.",
12
  "fertilizer": "Use potassium-rich fertilizers to strengthen plant immunity."
13
  },
14
  "early blight": {
15
+ "description": "Causes dark concentric rings on leaves, leading to defoliation.",
16
  "remedy": "Apply fungicides, practice crop rotation.",
17
  "fertilizer": "Use nitrogen-rich fertilizers to improve plant resistance."
18
  },
19
  "healthy": {
20
+ "description": "No signs of disease detected.",
21
  "remedy": "No action needed.",
22
+ "fertilizer": "Balanced NPK fertilizers to maintain health."
23
  },
24
  "late blight": {
25
+ "description": "Rapidly spreading fungal disease, common in wet conditions.",
26
+ "remedy": "Remove infected plants, apply fungicides.",
27
+ "fertilizer": "Calcium-based fertilizers reduce susceptibility."
28
  },
29
  "leaf miner": {
30
+ "description": "Larvae create winding tunnels in leaves.",
31
  "remedy": "Use insecticidal sprays, remove affected leaves.",
32
+ "fertilizer": "Organic compost or vermicompost to strengthen plants."
33
  },
34
  "leaf mold": {
35
+ "description": "Yellowing leaves with fuzzy mold on undersides.",
36
+ "remedy": "Improve air circulation, apply fungicides.",
37
+ "fertilizer": "Phosphorus-based fertilizers to enhance root health."
38
  },
39
  "mosaic virus": {
40
+ "description": "Causes mottled, yellow-green appearance on leaves.",
41
  "remedy": "Remove infected plants, control aphids.",
42
+ "fertilizer": "Balanced NPK fertilizers, avoid excess nitrogen."
43
  },
44
  "septoria": {
45
+ "description": "Small, circular brown spots on lower leaves.",
46
  "remedy": "Remove infected leaves, use fungicides.",
47
+ "fertilizer": "Sulfur-based fertilizers to reduce fungal spread."
48
  },
49
  "spider mites": {
50
+ "description": "Causes stippling and webbing on leaves.",
51
  "remedy": "Use miticides, introduce beneficial insects.",
52
+ "fertilizer": "Silicon-based fertilizers improve resistance to pests."
53
  },
54
  "yellow leaf curl virus": {
55
+ "description": "Curled, yellowing leaves and stunted growth.",
56
  "remedy": "Remove infected plants, control whiteflies.",
57
+ "fertilizer": "Zinc and boron-based fertilizers to reduce symptoms."
58
  }
59
  }
60
 
61
  def custom_images(img, target_size=(512, 512)): # Fixed size
62
+ """Resizes the input image to a fixed target size."""
 
 
63
  return cv2.resize(img, target_size)
64
 
65
  def plant_disease_detect(img, model, original_size):
66
+ """Runs YOLO disease detection on the image."""
 
 
67
  detect_result = model(img)
68
  detect_img = detect_result[0].plot()
69
  detections = detect_result[0].boxes.data.tolist()
 
72
  return detect_img, classes
73
 
74
  def predict(img):
75
+ """Processes image and returns detections with disease info."""
76
  model = YOLO('crop_disease_model.pt')
77
  original_size = (img.shape[1], img.shape[0])
78
+ resized_image = custom_images(img)
79
  detect_img, classes = plant_disease_detect(resized_image, model, original_size)
80
+
81
+ # 🟒 **Format table data for better readability**
82
  unique_classes = list(set(classes))
83
+ table_data = []
84
+ for cls in unique_classes:
85
+ disease = cls
86
+ details = disease_info.get(cls.lower(), {})
87
+ description = details.get("description", "No description available")
88
+ remedy = details.get("remedy", "No remedy available")
89
+ fertilizer = details.get("fertilizer", "No recommendation available")
90
+ table_data.append([disease, description, remedy, fertilizer])
91
+
92
+ return detect_img, table_data # Returns detection image + structured table
93
 
94
+ # 🌱 **Improved Gradio Interface**
95
  with gr.Blocks() as demo:
96
+ gr.Markdown("# 🌿 **Crop Disease Diagnosis**")
97
+ gr.Markdown("Upload a plant image to detect diseases and get expert recommendations.")
98
 
99
  with gr.Row():
100
  with gr.Column():
101
+ img_input = gr.Image(type="numpy", label="Upload Image")
102
+ submit_btn = gr.Button("Detect Disease")
103
  with gr.Column():
104
+ img_output = gr.Image(label="Processed Image")
105
+ class_table = gr.Dataframe(
106
+ headers=["Disease", "Description", "Remedy", "Recommended Fertilizer"],
107
+ datatype=["str", "str", "str", "str"], # βœ… Defines column types
108
+ label="Disease Details"
109
+ )
110
 
111
  submit_btn.click(fn=predict, inputs=[img_input], outputs=[img_output, class_table])
112
 
113
+ # 🌎 **Enable API Mode for External Requests**
114
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
115
 
116
+ # πŸ›°οΈ **Hugging Face API Access**
117
  client = Client("sankalp2606/Plant-Disease_diagnosis")
118
  print("HELLO")
119
  print(client.endpoints)