Update app.py
Browse files
app.py
CHANGED
|
@@ -67,19 +67,25 @@ def generate_gemini_response(disease_list, user_context="", conversation_history
|
|
| 67 |
return f"Error connecting to Gemini API: {str(e)}"
|
| 68 |
|
| 69 |
# Performing inference using YOLO
|
| 70 |
-
def inference(image):
|
| 71 |
-
"""Detect crop diseases in the given image."""
|
| 72 |
-
results = yolo_model(image, conf=0.4)
|
| 73 |
infer = np.zeros(image.shape, dtype=np.uint8)
|
| 74 |
detected_classes = []
|
| 75 |
class_names = {}
|
|
|
|
| 76 |
|
| 77 |
for r in results:
|
| 78 |
infer = r.plot()
|
| 79 |
class_names = r.names
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
return infer, detected_classes, class_names
|
| 83 |
|
| 84 |
# Converting text to chosen language speech
|
| 85 |
def text_to_speech(text, language="en"):
|
|
@@ -171,8 +177,18 @@ if uploaded_file:
|
|
| 171 |
st.image(processed_image, caption="π Detected Diseases", use_column_width=True)
|
| 172 |
|
| 173 |
if detected_classes:
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# AI-generated diagnosis from Gemini
|
| 178 |
st.subheader("π AI Diagnosis")
|
|
|
|
| 67 |
return f"Error connecting to Gemini API: {str(e)}"
|
| 68 |
|
| 69 |
# Performing inference using YOLO
|
| 70 |
+
def inference(image, conf_threshold=0.5):
|
| 71 |
+
"""Detect crop diseases in the given image with confidence filtering."""
|
| 72 |
+
results = yolo_model(image, conf=0.4) # Adjusted confidence threshold for detection
|
| 73 |
infer = np.zeros(image.shape, dtype=np.uint8)
|
| 74 |
detected_classes = []
|
| 75 |
class_names = {}
|
| 76 |
+
confidence_scores = []
|
| 77 |
|
| 78 |
for r in results:
|
| 79 |
infer = r.plot()
|
| 80 |
class_names = r.names
|
| 81 |
+
for i, cls in enumerate(r.boxes.cls.tolist()):
|
| 82 |
+
confidence = r.boxes.conf[i].item() # Get confidence score
|
| 83 |
+
if confidence >= conf_threshold: # Only consider high-confidence detections
|
| 84 |
+
detected_classes.append(cls)
|
| 85 |
+
confidence_scores.append(confidence)
|
| 86 |
+
|
| 87 |
+
return infer, detected_classes, class_names, confidence_scores
|
| 88 |
|
|
|
|
| 89 |
|
| 90 |
# Converting text to chosen language speech
|
| 91 |
def text_to_speech(text, language="en"):
|
|
|
|
| 177 |
st.image(processed_image, caption="π Detected Diseases", use_column_width=True)
|
| 178 |
|
| 179 |
if detected_classes:
|
| 180 |
+
# Convert detected class indexes to names
|
| 181 |
+
detected_disease_names = [
|
| 182 |
+
f"{class_names[cls]} ({confidence_scores[i]:.2f})"
|
| 183 |
+
for i, cls in enumerate(detected_classes)
|
| 184 |
+
]
|
| 185 |
+
|
| 186 |
+
# Show only the most confident detections
|
| 187 |
+
if detected_disease_names:
|
| 188 |
+
st.write(f"β
**High Confidence Diseases Detected:** {', '.join(detected_disease_names)}")
|
| 189 |
+
else:
|
| 190 |
+
st.write("β
No high-confidence diseases detected.")
|
| 191 |
+
|
| 192 |
|
| 193 |
# AI-generated diagnosis from Gemini
|
| 194 |
st.subheader("π AI Diagnosis")
|