Add application file
Browse files- app.py +146 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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 |
+
# Replace with your model path on HuggingFace
|
12 |
+
model = YOLO('https://huggingface.co/IsmatS/crop_desease_detection/resolve/main/best.pt')
|
13 |
+
return model
|
14 |
+
|
15 |
+
model = load_model()
|
16 |
+
|
17 |
+
def detect_tree_disease(image, conf_threshold=0.25, iou_threshold=0.45):
|
18 |
+
"""Detect unhealthy trees in the uploaded image"""
|
19 |
+
|
20 |
+
# Convert PIL image to numpy array
|
21 |
+
image_np = np.array(image)
|
22 |
+
|
23 |
+
# Run inference
|
24 |
+
results = model(image_np, conf=conf_threshold, iou=iou_threshold)
|
25 |
+
|
26 |
+
# Extract detections
|
27 |
+
detections = []
|
28 |
+
for result in results:
|
29 |
+
boxes = result.boxes
|
30 |
+
if boxes is not None:
|
31 |
+
for box in boxes:
|
32 |
+
detection = {
|
33 |
+
'confidence': float(box.conf[0]),
|
34 |
+
'bbox': box.xyxy[0].tolist(),
|
35 |
+
'class': 'unhealthy'
|
36 |
+
}
|
37 |
+
detections.append(detection)
|
38 |
+
|
39 |
+
# Draw annotations on the image
|
40 |
+
annotated_img = image.copy()
|
41 |
+
draw = ImageDraw.Draw(annotated_img)
|
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 |
+
summary = f"Detected {len(detections)} unhealthy tree(s)\n\n"
|
64 |
+
for i, det in enumerate(detections, 1):
|
65 |
+
summary += f"Tree {i}: Confidence {det['confidence']:.2f}\n"
|
66 |
+
|
67 |
+
return annotated_img, summary
|
68 |
+
|
69 |
+
# Create example images
|
70 |
+
example_images = [
|
71 |
+
["https://raw.githubusercontent.com/ultralytics/yolov5/master/data/images/zidane.jpg", 0.25, 0.45],
|
72 |
+
# Add your own example images here
|
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**: YOLOv8s trained on PDT (Pests and Diseases Tree) dataset
|
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")
|
107 |
+
detection_summary = gr.Textbox(label="Detection Summary", lines=10)
|
108 |
+
|
109 |
+
# Set up event handler
|
110 |
+
detect_button.click(
|
111 |
+
fn=detect_tree_disease,
|
112 |
+
inputs=[input_image, conf_threshold, iou_threshold],
|
113 |
+
outputs=[output_image, detection_summary]
|
114 |
+
)
|
115 |
+
|
116 |
+
# Add examples
|
117 |
+
gr.Examples(
|
118 |
+
examples=example_images,
|
119 |
+
inputs=[input_image, conf_threshold, iou_threshold],
|
120 |
+
outputs=[output_image, detection_summary],
|
121 |
+
fn=detect_tree_disease,
|
122 |
+
cache_examples=True,
|
123 |
+
)
|
124 |
+
|
125 |
+
gr.Markdown("""
|
126 |
+
## About this Model
|
127 |
+
|
128 |
+
- **Architecture**: YOLOv8s
|
129 |
+
- **Dataset**: [PDT Dataset](https://huggingface.co/datasets/qwer0213/PDT_dataset)
|
130 |
+
- **mAP50**: 0.933
|
131 |
+
- **mAP50-95**: 0.659
|
132 |
+
- **Classes**: 1 (unhealthy trees)
|
133 |
+
|
134 |
+
## Usage Tips
|
135 |
+
|
136 |
+
- This model works best with aerial/UAV imagery
|
137 |
+
- Optimal input resolution: 640x640 pixels
|
138 |
+
- Adjust confidence threshold to filter detections
|
139 |
+
- Lower IoU threshold for overlapping trees
|
140 |
+
|
141 |
+
[Model Card](https://huggingface.co/IsmatS/crop_desease_detection) |
|
142 |
+
[Dataset](https://huggingface.co/datasets/qwer0213/PDT_dataset)
|
143 |
+
""")
|
144 |
+
|
145 |
+
# Launch the app
|
146 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
ultralytics
|
3 |
+
opencv-python-headless
|
4 |
+
pillow
|
5 |
+
numpy
|