project21 commited on
Commit
28762aa
·
verified ·
1 Parent(s): a76ca21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -73
app.py CHANGED
@@ -33,33 +33,47 @@ def predict_disease(image_file, model, all_labels, target_language):
33
 
34
  # Predict the class
35
  predictions = model.predict(img_array)
36
- # Validate predictions
37
- confidence_threshold = 0.98 # Require at least 98% confidence
38
  confidence_scores = predictions[0]
39
  max_confidence = np.max(confidence_scores)
 
40
 
 
41
  if max_confidence < confidence_threshold:
42
- print(f"Prediction confidence ({max_confidence:.2f}) is too low.")
43
- return f"""
44
- <h3 style="color:red; text-align:center;">
45
-
46
- Please upload a clearer image of the plant.
47
- </h3>
48
- """
49
- predicted_class = np.argmax(predictions[0])
50
-
 
 
51
  # Get the predicted class label
52
  predicted_label = all_labels[predicted_class]
53
-
54
- # Translate the predicted label to the selected language
 
 
 
 
 
 
 
 
 
 
55
  translated_label = GoogleTranslator(source='en', target=target_language).translate(predicted_label)
56
-
57
  # Provide pesticide information based on the predicted label
 
 
 
58
  if predicted_label == 'Sugarcane Yellow':
59
  pesticide_info = """
60
  <h2><center><b>Sugarcane Yellow</b></center></h2>
61
  <h4>PESTICIDES TO BE USED:</h4><br>
62
-
63
  <ul style="font-size:17px;margin-left:40px;">
64
  <li>1. Insecticidal Soap</li>
65
  <li>2. Pyrethroids</li>
@@ -70,74 +84,20 @@ def predict_disease(image_file, model, all_labels, target_language):
70
  <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
71
  <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
72
  """
73
- elif predicted_label == 'Sugarcane Rust':
74
- pesticide_info = """
75
- <h2><center><b>Sugarcane Rust</b></center></h2>
76
- <h4>PESTICIDES TO BE USED:</h4><br>
77
-
78
- <ul style="font-size:17px;margin-left:40px;">
79
- <li>1. Triadimefon</li>
80
- <li>2. Chlorothalonil</li>
81
- <li>3. Tebuconazole</li>
82
- <li>4. Propiconazole</li>
83
- </ul><br>
84
- <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
85
- <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
86
- """
87
- elif predicted_label == 'Sugarcane RedRot':
88
- pesticide_info = """
89
- <h2><center><b>Sugarcane RedRot</b></center></h2>
90
- <h4>PESTICIDES TO BE USED:</h4><br>
91
-
92
- <ul style="font-size:17px;margin-left:40px;">
93
- <li>1. Mancozeb</li>
94
- <li>2. Chlorothalonil</li>
95
- <li>3. Tebuconazole</li>
96
- <li>4. Carbendazim</li>
97
- </ul><br>
98
- <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
99
- <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
100
- """
101
- elif predicted_label == 'Sugarcane Mosaic':
102
- pesticide_info = """
103
- <h2><center><b>Sugarcane Mosaic</b></center></h2>
104
- <h4>PESTICIDES TO BE USED:</h4><br>
105
-
106
- <ul style="font-size:17px;margin-left:40px;">
107
- <li>1. Horticultural Oil</li>
108
- <li>2. Spinosad</li>
109
- <li>3. Pyrethrin </li>
110
- <li>4. Neem Oil</li>
111
- <li>5. Imidacloprid</li>
112
- </ul><br>
113
- <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
114
- <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
115
- """
116
-
117
- elif predicted_label == 'Sugarcane Healthy':
118
- pesticide_info = """<h2><center><b>Sugarcane Healthy</b></center></h2>
119
- <h5> No pesticides needed"""
120
-
121
  else:
122
  pesticide_info = 'No pesticide information available.'
123
 
124
- print(f"Pesticide Info (Before Translation): {pesticide_info}")
125
-
126
- # Translate the pesticide information to the selected language
127
  translated_pesticide_info = GoogleTranslator(source='en', target=target_language).translate(pesticide_info)
128
- print(f"Translated Pesticide Info: {translated_pesticide_info}")
129
 
130
- # Return translated label and pesticide information with associated styling
131
- predicted_label_html = f"""
132
-
133
- {translated_pesticide_info}
134
- """
135
- return predicted_label_html
136
 
137
  except Exception as e:
138
  print(f"Error during prediction: {e}")
139
  return f"<h3>Error: {e}</h3>"
140
 
 
141
  # List of class labels
142
  all_labels = [
143
  'Sugarcane Yellow',
 
33
 
34
  # Predict the class
35
  predictions = model.predict(img_array)
 
 
36
  confidence_scores = predictions[0]
37
  max_confidence = np.max(confidence_scores)
38
+ confidence_threshold = 0.98 # Require at least 98% confidence
39
 
40
+ # Check if the confidence is too low
41
  if max_confidence < confidence_threshold:
42
+ print(f"Prediction confidence ({max_confidence:.2f}) is too low.")
43
+ return f"""
44
+ <h3 style="color:red; text-align:center;">
45
+ The uploaded image does not appear to be a sugarcane plant or has low clarity.
46
+ Please upload a clearer image of a sugarcane plant.
47
+ </h3>
48
+ """
49
+
50
+ # Get the predicted class index
51
+ predicted_class = np.argmax(confidence_scores)
52
+
53
  # Get the predicted class label
54
  predicted_label = all_labels[predicted_class]
55
+
56
+ # Check for irrelevant or non-sugarcane-related predictions
57
+ if predicted_label not in all_labels:
58
+ print("The image does not match sugarcane diseases.")
59
+ return f"""
60
+ <h3 style="color:red; text-align:center;">
61
+ The uploaded image does not match any sugarcane diseases.
62
+ Please ensure you upload a sugarcane plant image.
63
+ </h3>
64
+ """
65
+
66
+ # Translate the predicted label
67
  translated_label = GoogleTranslator(source='en', target=target_language).translate(predicted_label)
68
+
69
  # Provide pesticide information based on the predicted label
70
+ # (Use your existing logic here for each disease case)
71
+
72
+ # Example for a matched class:
73
  if predicted_label == 'Sugarcane Yellow':
74
  pesticide_info = """
75
  <h2><center><b>Sugarcane Yellow</b></center></h2>
76
  <h4>PESTICIDES TO BE USED:</h4><br>
 
77
  <ul style="font-size:17px;margin-left:40px;">
78
  <li>1. Insecticidal Soap</li>
79
  <li>2. Pyrethroids</li>
 
84
  <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
85
  <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
86
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  else:
88
  pesticide_info = 'No pesticide information available.'
89
 
90
+ # Translate pesticide info to the selected language
 
 
91
  translated_pesticide_info = GoogleTranslator(source='en', target=target_language).translate(pesticide_info)
 
92
 
93
+ # Return translated label and pesticide information
94
+ return f"{translated_pesticide_info}"
 
 
 
 
95
 
96
  except Exception as e:
97
  print(f"Error during prediction: {e}")
98
  return f"<h3>Error: {e}</h3>"
99
 
100
+
101
  # List of class labels
102
  all_labels = [
103
  'Sugarcane Yellow',