Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,165 +1,51 @@
|
|
1 |
-
import
|
2 |
-
import supervision as sv
|
3 |
import PIL.Image as Image
|
4 |
from ultralytics import YOLO
|
5 |
-
import gradio as gr
|
6 |
-
import torch
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
model_filenames = [
|
11 |
-
"yolo11n.pt",
|
12 |
-
"yolo11s.pt",
|
13 |
-
"yolo11m.pt",
|
14 |
-
"yolo11l.pt",
|
15 |
-
"yolo11x.pt"
|
16 |
-
]
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
box_annotator = sv.BoxAnnotator()
|
21 |
-
|
22 |
-
category_dict = {
|
23 |
-
0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
|
24 |
-
6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
|
25 |
-
11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
|
26 |
-
16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
|
27 |
-
22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
|
28 |
-
27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
|
29 |
-
32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
|
30 |
-
36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
|
31 |
-
40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
|
32 |
-
46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
|
33 |
-
51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
|
34 |
-
56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
|
35 |
-
61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
|
36 |
-
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
|
37 |
-
72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
|
38 |
-
77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
|
39 |
-
}
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
def yolo_inference(image, model_id, conf_threshold, iou_threshold, max_detection):
|
45 |
-
|
46 |
-
|
47 |
model = YOLO(model_id)
|
48 |
-
results = model(
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
image = gr.Image(type="pil", label="Image", interactive=True)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
with gr.Column():
|
95 |
-
output_image = gr.Image(type="pil", label="Annotated Image", interactive=False)
|
96 |
-
|
97 |
-
yolov_infer.click(
|
98 |
-
fn=yolo_inference,
|
99 |
-
inputs=[
|
100 |
-
image,
|
101 |
-
model_id,
|
102 |
-
conf_threshold,
|
103 |
-
iou_threshold,
|
104 |
-
max_detection,
|
105 |
-
],
|
106 |
-
outputs=[output_image],
|
107 |
-
)
|
108 |
-
|
109 |
-
gr.Examples(
|
110 |
-
examples=[
|
111 |
-
[
|
112 |
-
"zidane.jpg",
|
113 |
-
"yolo11s.pt",
|
114 |
-
0.25,
|
115 |
-
0.45,
|
116 |
-
300,
|
117 |
-
],
|
118 |
-
|
119 |
-
[
|
120 |
-
"bus.jpg",
|
121 |
-
"yolo11m.pt",
|
122 |
-
0.25,
|
123 |
-
0.45,
|
124 |
-
300,
|
125 |
-
],
|
126 |
-
[
|
127 |
-
"yolo_vision.jpg",
|
128 |
-
"yolo11x.pt",
|
129 |
-
0.25,
|
130 |
-
0.45,
|
131 |
-
300,
|
132 |
-
],
|
133 |
-
],
|
134 |
-
fn=yolo_inference,
|
135 |
-
inputs=[
|
136 |
-
image,
|
137 |
-
model_id,
|
138 |
-
conf_threshold,
|
139 |
-
iou_threshold,
|
140 |
-
max_detection,
|
141 |
-
],
|
142 |
-
outputs=[output_image],
|
143 |
-
cache_examples=True,
|
144 |
-
)
|
145 |
-
|
146 |
-
gradio_app = gr.Blocks()
|
147 |
-
with gradio_app:
|
148 |
-
gr.HTML(
|
149 |
-
"""
|
150 |
-
<h1 style='text-align: center'>
|
151 |
-
Yolo11: Object Detection
|
152 |
-
</h1>
|
153 |
-
<a>
|
154 |
-
""")
|
155 |
-
gr.HTML(
|
156 |
-
"""
|
157 |
-
<p style='text-align: center'>
|
158 |
-
Latest ultralytics yolo11 object detection models. Upload an image to run inference.
|
159 |
-
</p>
|
160 |
-
""")
|
161 |
-
with gr.Row():
|
162 |
-
with gr.Column():
|
163 |
-
app()
|
164 |
-
|
165 |
-
gradio_app.launch()
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import PIL.Image as Image
|
3 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
def yolo_inference(images, model_id, conf_threshold, iou_threshold, max_detection):
|
|
|
|
|
|
|
6 |
model = YOLO(model_id)
|
7 |
+
results = model.predict(
|
8 |
+
source=images,
|
9 |
+
conf=conf_threshold,
|
10 |
+
iou=iou_threshold,
|
11 |
+
imgsz=640,
|
12 |
+
max_det=max_detection,
|
13 |
+
show_labels=True,
|
14 |
+
show_conf=True,
|
15 |
+
)
|
16 |
+
|
17 |
+
annotated_images = []
|
18 |
+
for result in results:
|
19 |
+
im_array = result.plot()
|
20 |
+
im = Image.fromarray(im_array)
|
21 |
+
annotated_images.append(im)
|
|
|
22 |
|
23 |
+
return annotated_images
|
24 |
+
|
25 |
+
interface = gr.Interface(
|
26 |
+
fn=yolo_inference,
|
27 |
+
inputs=[
|
28 |
+
gr.Image(type="pil", label="Upload Image(s)", tool="editor", source="upload", multiple=True),
|
29 |
+
gr.Dropdown(
|
30 |
+
choices=['yolo11n.pt', 'yolo11s.pt', 'yolo11m.pt', 'yolo11l.pt', 'yolo11x.pt',
|
31 |
+
'yolo11n-seg.pt', 'yolo11s-seg.pt', 'yolo11m-seg.pt', 'yolo11l-seg.pt', 'yolo11x-seg.pt',
|
32 |
+
'yolo11n-pose.pt', 'yolo11s-pose.pt', 'yolo11m-pose.pt', 'yolo11l-pose.pt', 'yolo11x-pose.pt',
|
33 |
+
'yolo11n-obb.pt', 'yolo11s-obb.pt', 'yolo11m-obb.pt', 'yolo11l-obb.pt', 'yolo11x-obb.pt',
|
34 |
+
'yolo11n-cls.pt', 'yolo11s-cls.pt', 'yolo11m-cls.pt', 'yolo11l-cls.pt', 'yolo11x-cls.pt'],
|
35 |
+
label="Model Name",
|
36 |
+
value="yolo11n.pt",
|
37 |
+
),
|
38 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence Threshold"),
|
39 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU Threshold"),
|
40 |
+
gr.Slider(minimum=1, maximum=300, step=1, value=300, label="Max Detection"),
|
41 |
+
],
|
42 |
+
outputs=gr.Gallery(label="Annotated Image"),
|
43 |
+
title="Yolo11: Object Detection",
|
44 |
+
description="Upload image(s) for inference using the latest Ultralytics YOLO11 models.",
|
45 |
+
examples=[
|
46 |
+
[["zidane.jpg"], "yolo11s.pt", 0.25, 0.45, 300],
|
47 |
+
[["bus.jpg"], "yolo11m.pt", 0.25, 0.45, 300],
|
48 |
+
[["yolo_vision.jpg"], "yolo11x.pt", 0.25, 0.45, 300],
|
49 |
+
],
|
50 |
+
)
|
51 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|