app
Browse files
app.py
CHANGED
@@ -3,16 +3,35 @@ from ultralytics import YOLO
|
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
6 |
import requests
|
7 |
from io import BytesIO
|
8 |
|
9 |
-
# Load the model
|
10 |
def load_model():
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
model
|
|
|
16 |
|
17 |
def detect_tree_disease(image, conf_threshold=0.25, iou_threshold=0.45):
|
18 |
"""Detect unhealthy trees in the uploaded image"""
|
@@ -32,75 +51,71 @@ def detect_tree_disease(image, conf_threshold=0.25, iou_threshold=0.45):
|
|
32 |
detection = {
|
33 |
'confidence': float(box.conf[0]),
|
34 |
'bbox': box.xyxy[0].tolist(),
|
35 |
-
'class': 'unhealthy'
|
36 |
}
|
37 |
detections.append(detection)
|
38 |
|
39 |
-
#
|
40 |
-
annotated_img =
|
41 |
-
|
42 |
-
|
43 |
-
# Try to use a default font, fall back to PIL default if not available
|
44 |
-
try:
|
45 |
-
font = ImageFont.truetype("arial.ttf", 20)
|
46 |
-
except:
|
47 |
-
font = ImageFont.load_default()
|
48 |
-
|
49 |
-
for det in detections:
|
50 |
-
bbox = det['bbox']
|
51 |
-
conf = det['confidence']
|
52 |
-
|
53 |
-
# Draw bounding box
|
54 |
-
draw.rectangle(bbox, outline="red", width=3)
|
55 |
-
|
56 |
-
# Draw label with confidence
|
57 |
-
label = f"Unhealthy: {conf:.2f}"
|
58 |
-
text_bbox = draw.textbbox((bbox[0], bbox[1] - 25), label, font=font)
|
59 |
-
draw.rectangle(text_bbox, fill="red")
|
60 |
-
draw.text((bbox[0], bbox[1] - 25), label, fill="white", font=font)
|
61 |
|
62 |
# Create detection summary
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
return annotated_img, summary
|
68 |
|
69 |
# Create example images
|
70 |
example_images = [
|
71 |
-
["https://
|
72 |
-
|
73 |
]
|
74 |
|
75 |
# Create Gradio interface
|
76 |
with gr.Blocks(title="Tree Disease Detection") as demo:
|
77 |
-
gr.Markdown("""
|
78 |
# 🌳 Tree Disease Detection with YOLOv8
|
79 |
|
80 |
This model detects unhealthy/diseased trees in aerial UAV imagery.
|
81 |
Upload an image or use one of the examples below to detect diseased trees.
|
82 |
|
83 |
-
**Model**:
|
84 |
""")
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
with gr.Row():
|
87 |
with gr.Column():
|
88 |
input_image = gr.Image(type="pil", label="Upload Image")
|
89 |
conf_threshold = gr.Slider(
|
90 |
-
minimum=0.0,
|
91 |
-
maximum=1.0,
|
92 |
-
value=0.25,
|
93 |
step=0.05,
|
94 |
label="Confidence Threshold"
|
95 |
)
|
96 |
iou_threshold = gr.Slider(
|
97 |
-
minimum=0.0,
|
98 |
-
maximum=1.0,
|
99 |
-
value=0.45,
|
100 |
step=0.05,
|
101 |
label="IoU Threshold"
|
102 |
)
|
103 |
-
detect_button = gr.Button("Detect Tree Disease")
|
104 |
|
105 |
with gr.Column():
|
106 |
output_image = gr.Image(type="pil", label="Detection Results")
|
@@ -119,7 +134,7 @@ with gr.Blocks(title="Tree Disease Detection") as demo:
|
|
119 |
inputs=[input_image, conf_threshold, iou_threshold],
|
120 |
outputs=[output_image, detection_summary],
|
121 |
fn=detect_tree_disease,
|
122 |
-
cache_examples=
|
123 |
)
|
124 |
|
125 |
gr.Markdown("""
|
@@ -143,4 +158,4 @@ with gr.Blocks(title="Tree Disease Detection") as demo:
|
|
143 |
""")
|
144 |
|
145 |
# Launch the app
|
146 |
-
demo.launch()
|
|
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import os
|
7 |
import requests
|
8 |
from io import BytesIO
|
9 |
|
10 |
+
# Load the model with error handling
|
11 |
def load_model():
|
12 |
+
try:
|
13 |
+
# Try to load from HuggingFace first
|
14 |
+
model = YOLO('IsmatS/crop_desease_detection')
|
15 |
+
return model, "Custom Tree Disease Detection Model"
|
16 |
+
except:
|
17 |
+
try:
|
18 |
+
# Try direct URL
|
19 |
+
model = YOLO('https://huggingface.co/IsmatS/crop_desease_detection/resolve/main/best.pt')
|
20 |
+
return model, "Custom Tree Disease Detection Model"
|
21 |
+
except:
|
22 |
+
try:
|
23 |
+
# Try local file if exists
|
24 |
+
if os.path.exists('best.pt'):
|
25 |
+
model = YOLO('best.pt')
|
26 |
+
return model, "Custom Tree Disease Detection Model (Local)"
|
27 |
+
except:
|
28 |
+
# Fallback to standard YOLOv8s
|
29 |
+
print("Loading standard YOLOv8s model as fallback...")
|
30 |
+
model = YOLO('yolov8s.pt')
|
31 |
+
return model, "Standard YOLOv8s Model (Fallback)"
|
32 |
|
33 |
+
# Load model and get status
|
34 |
+
model, model_status = load_model()
|
35 |
|
36 |
def detect_tree_disease(image, conf_threshold=0.25, iou_threshold=0.45):
|
37 |
"""Detect unhealthy trees in the uploaded image"""
|
|
|
51 |
detection = {
|
52 |
'confidence': float(box.conf[0]),
|
53 |
'bbox': box.xyxy[0].tolist(),
|
54 |
+
'class': 'unhealthy' if model_status.startswith("Custom") else 'object'
|
55 |
}
|
56 |
detections.append(detection)
|
57 |
|
58 |
+
# Get annotated image directly from results
|
59 |
+
annotated_img = results[0].plot()
|
60 |
+
annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
61 |
+
annotated_img = Image.fromarray(annotated_img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
# Create detection summary
|
64 |
+
if model_status.startswith("Custom"):
|
65 |
+
summary = f"Detected {len(detections)} unhealthy tree(s)\n\n"
|
66 |
+
for i, det in enumerate(detections, 1):
|
67 |
+
summary += f"Tree {i}: Confidence {det['confidence']:.2f}\n"
|
68 |
+
else:
|
69 |
+
summary = f"Using {model_status}\n"
|
70 |
+
summary += f"Detected {len(detections)} object(s)\n\n"
|
71 |
+
for i, det in enumerate(detections, 1):
|
72 |
+
summary += f"Object {i}: Confidence {det['confidence']:.2f}\n"
|
73 |
+
|
74 |
+
summary += f"\nModel Status: {model_status}"
|
75 |
|
76 |
return annotated_img, summary
|
77 |
|
78 |
# Create example images
|
79 |
example_images = [
|
80 |
+
["https://hips.hearstapps.com/hmg-prod/images/gettyimages-1841066-1691513468.jpg", 0.25, 0.45], # Tree image
|
81 |
+
["https://www.fs.usda.gov/Internet/FSE_MEDIA/fseprd1115588.jpg", 0.25, 0.45], # Another tree
|
82 |
]
|
83 |
|
84 |
# Create Gradio interface
|
85 |
with gr.Blocks(title="Tree Disease Detection") as demo:
|
86 |
+
gr.Markdown(f"""
|
87 |
# 🌳 Tree Disease Detection with YOLOv8
|
88 |
|
89 |
This model detects unhealthy/diseased trees in aerial UAV imagery.
|
90 |
Upload an image or use one of the examples below to detect diseased trees.
|
91 |
|
92 |
+
**Current Model**: {model_status}
|
93 |
""")
|
94 |
|
95 |
+
if not model_status.startswith("Custom"):
|
96 |
+
gr.Markdown("""
|
97 |
+
⚠️ **Note**: Currently using a fallback model. The specialized tree disease model is being updated.
|
98 |
+
Detection will work but won't be specific to tree diseases.
|
99 |
+
""")
|
100 |
+
|
101 |
with gr.Row():
|
102 |
with gr.Column():
|
103 |
input_image = gr.Image(type="pil", label="Upload Image")
|
104 |
conf_threshold = gr.Slider(
|
105 |
+
minimum=0.0,
|
106 |
+
maximum=1.0,
|
107 |
+
value=0.25,
|
108 |
step=0.05,
|
109 |
label="Confidence Threshold"
|
110 |
)
|
111 |
iou_threshold = gr.Slider(
|
112 |
+
minimum=0.0,
|
113 |
+
maximum=1.0,
|
114 |
+
value=0.45,
|
115 |
step=0.05,
|
116 |
label="IoU Threshold"
|
117 |
)
|
118 |
+
detect_button = gr.Button("Detect Tree Disease", variant="primary")
|
119 |
|
120 |
with gr.Column():
|
121 |
output_image = gr.Image(type="pil", label="Detection Results")
|
|
|
134 |
inputs=[input_image, conf_threshold, iou_threshold],
|
135 |
outputs=[output_image, detection_summary],
|
136 |
fn=detect_tree_disease,
|
137 |
+
cache_examples=False, # Disable caching to avoid initialization issues
|
138 |
)
|
139 |
|
140 |
gr.Markdown("""
|
|
|
158 |
""")
|
159 |
|
160 |
# Launch the app
|
161 |
+
demo.launch()
|