Spaces:
Runtime error
Runtime error
Commit
·
b5045f8
1
Parent(s):
894a37b
Upload 2 files
Browse files- app.py +46 -0
- detection.py +52 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from detection import ObjectDetection
|
3 |
+
|
4 |
+
examples = [
|
5 |
+
['test-images/plant1.jpeg', 0.31],
|
6 |
+
['test-images/plant1.jpeg', 0.51],
|
7 |
+
['test-images/plant1.jpeg', 0.39],
|
8 |
+
]
|
9 |
+
|
10 |
+
def get_predictions(img, threshold, box_color, text_color):
|
11 |
+
v8_results = yolov8_detector.v8_score_frame(img)
|
12 |
+
v8_frame = yolov8_detector.plot_bboxes(v8_results, img, float(threshold), box_color, text_color)
|
13 |
+
return v8_frame
|
14 |
+
|
15 |
+
with gr.Blocks(title="Leaf Disease Detection", theme=gr.themes.Monochrome()) as interface:
|
16 |
+
gr.Markdown("# Leaf Disease Detection")
|
17 |
+
with gr.Row():
|
18 |
+
with gr.Column():
|
19 |
+
image = gr.Image(shape=(416,416), label="Input Image")
|
20 |
+
with gr.Column():
|
21 |
+
with gr.Row():
|
22 |
+
with gr.Column():
|
23 |
+
box_color = gr.ColorPicker(label="Box Color", value="#0000ff")
|
24 |
+
with gr.Column():
|
25 |
+
text_color = gr.ColorPicker(label="Prediction Color", value="#ff0000")
|
26 |
+
|
27 |
+
confidence = gr.Slider(maximum=1, step=0.01, value=0.4, label="Confidence Threshold", interactive=True)
|
28 |
+
btn = gr.Button("Detect")
|
29 |
+
|
30 |
+
with gr.Row():
|
31 |
+
with gr.Box():
|
32 |
+
v8_prediction = gr.Image(shape=(416,416), label="YOLOv8")
|
33 |
+
|
34 |
+
btn.click(
|
35 |
+
get_predictions,
|
36 |
+
[image, confidence, box_color, text_color],
|
37 |
+
[v8_prediction]
|
38 |
+
)
|
39 |
+
|
40 |
+
with gr.Row():
|
41 |
+
gr.Examples(examples=examples, inputs=[image, confidence])
|
42 |
+
|
43 |
+
|
44 |
+
yolov8_detector = ObjectDetection('yolov8')
|
45 |
+
|
46 |
+
interface.launch()
|
detection.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import IPython
|
3 |
+
from PIL import ImageColor
|
4 |
+
from ultralytics import YOLO
|
5 |
+
|
6 |
+
class ObjectDetection:
|
7 |
+
def __init__(self, model_name='yolov8'):
|
8 |
+
self.model_name = model_name
|
9 |
+
self.model = self.load_model()
|
10 |
+
self.classes = self.model.names
|
11 |
+
self.device = 'cpu'
|
12 |
+
|
13 |
+
def load_model(self):
|
14 |
+
model = YOLO(f"weights/{self.model_name}_best.pt")
|
15 |
+
return model
|
16 |
+
|
17 |
+
def v8_score_frame(self, frame):
|
18 |
+
results = self.model(frame)
|
19 |
+
|
20 |
+
labels = results.names[results.pred[..., -1].argmax(-1)] # Get class labels
|
21 |
+
confidences = results.pred[..., -2].max(-1) # Get confidences
|
22 |
+
coords = results.pred[..., :-2] # Get coordinates
|
23 |
+
|
24 |
+
return labels, confidences, coords
|
25 |
+
|
26 |
+
def get_coords(self, frame, row):
|
27 |
+
return int(row[0]), int(row[1]), int(row[2]), int(row[3])
|
28 |
+
|
29 |
+
def class_to_label(self, x):
|
30 |
+
return self.classes[int(x)]
|
31 |
+
|
32 |
+
def get_color(self, code):
|
33 |
+
rgb = ImageColor.getcolor(code, "RGB")
|
34 |
+
return rgb
|
35 |
+
|
36 |
+
def plot_bboxes(self, results, frame, threshold=0.5, box_color='red', text_color='white'):
|
37 |
+
labels, conf, coord = results
|
38 |
+
|
39 |
+
frame = frame.copy()
|
40 |
+
box_color = self.get_color(box_color)
|
41 |
+
text_color = self.get_color(text_color)
|
42 |
+
|
43 |
+
for i in range(len(labels)):
|
44 |
+
if conf[i] >= threshold:
|
45 |
+
x1, y1, x2, y2 = self.get_coords(frame, coord[i])
|
46 |
+
class_name = self.class_to_label(labels[i])
|
47 |
+
|
48 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), box_color, 2)
|
49 |
+
cv2.putText(frame, f"{class_name} - {conf[i]*100:.2f}%", (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.5, text_color)
|
50 |
+
|
51 |
+
return frame
|
52 |
+
|