Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Ultralytics YOLO26 & YOLOE26 Gradio Demo.
|
| 3 |
-
|
| 4 |
-
This script creates an interactive Gradio interface showcasing:
|
| 5 |
-
- Ultralytics YOLO26 models across tasks (Detection, Segmentation, Pose, OBB, Classification)
|
| 6 |
-
- Ultralytics YOLOE26 open-vocabulary segmentation with custom text prompts
|
| 7 |
-
|
| 8 |
-
Docs: https://docs.ultralytics.com/models/yolo26/
|
| 9 |
-
GitHub: https://github.com/ultralytics/ultralytics
|
| 10 |
-
|
| 11 |
-
Usage:
|
| 12 |
-
python examples/app.py
|
| 13 |
-
"""
|
| 14 |
-
|
| 15 |
import gradio as gr
|
| 16 |
from PIL import Image
|
| 17 |
|
|
@@ -27,14 +13,6 @@ if not OBB_IMAGE.exists():
|
|
| 27 |
# Model cache
|
| 28 |
model_cache = {}
|
| 29 |
|
| 30 |
-
# Suffixes for filenames (weights naming) and for repo naming
|
| 31 |
-
TASK_FILE_SUFFIX = {
|
| 32 |
-
"Detection": "",
|
| 33 |
-
"Segmentation": "-seg",
|
| 34 |
-
"Classification": "-cls",
|
| 35 |
-
"Pose": "-pose",
|
| 36 |
-
"OBB": "-obb",
|
| 37 |
-
}
|
| 38 |
TASK_REPO_SUFFIX = {
|
| 39 |
"Detection": "",
|
| 40 |
"Segmentation": "-seg",
|
|
@@ -47,25 +25,26 @@ def _scale_from_label(label: str) -> str:
|
|
| 47 |
# "YOLO26-M" -> "m", "YOLOE-26L" -> "l"
|
| 48 |
return label.strip()[-1].lower()
|
| 49 |
|
| 50 |
-
def _get_model(repo_id: str
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return model_cache[key]
|
| 56 |
|
| 57 |
def predict_yolo26(image, model_name, task, conf, iou, retina):
|
| 58 |
-
|
| 59 |
-
scale = _scale_from_label(model_name) # n/s/m/l/x
|
| 60 |
|
| 61 |
-
#
|
| 62 |
repo_id = f"openvision/yolo26-{scale}{TASK_REPO_SUFFIX[task]}"
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if task == "Classification":
|
| 71 |
top5 = results[0].probs.top5
|
|
@@ -77,111 +56,23 @@ def predict_yolo26(image, model_name, task, conf, iou, retina):
|
|
| 77 |
return Image.fromarray(results[0].plot()[..., ::-1]), None
|
| 78 |
|
| 79 |
def predict_yoloe26(image, model_name, classes_text, conf, retina):
|
| 80 |
-
|
| 81 |
-
scale = _scale_from_label(model_name) # n/s/m/l/x
|
| 82 |
|
| 83 |
-
#
|
| 84 |
repo_id = f"openvision/yoloe26-{scale}-seg"
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
names = [c.strip() for c in classes_text.split(",") if c.strip()] or ["person", "car", "dog", "cat"]
|
| 90 |
model.set_classes(names, model.get_text_pe(names))
|
| 91 |
|
| 92 |
-
results = model.predict(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
button_primary_background_fill_hover="#042AFF",
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
# Build interface
|
| 101 |
-
with gr.Blocks(title="Ultralytics YOLO26 & YOLOE26 Demo") as demo:
|
| 102 |
-
gr.Markdown(
|
| 103 |
-
"# 🚀 Ultralytics YOLO26 & YOLOE26 Demo\n"
|
| 104 |
-
"Showcasing YOLO26 tasks and YOLOE26 open-vocabulary segmentation. "
|
| 105 |
-
"[GitHub](https://github.com/ultralytics/ultralytics) | [Docs](https://docs.ultralytics.com/models/yolo26/)"
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
with gr.Tab("YOLO26 Tasks"):
|
| 110 |
-
gr.Markdown("### Ultralytics YOLO26: Detection, Segmentation, Pose, OBB, Classification")
|
| 111 |
-
with gr.Row():
|
| 112 |
-
with gr.Column():
|
| 113 |
-
y26_image = gr.Image(type="pil", label="Upload Image")
|
| 114 |
-
with gr.Row():
|
| 115 |
-
y26_model = gr.Dropdown(
|
| 116 |
-
["YOLO26-N", "YOLO26-S", "YOLO26-M", "YOLO26-L", "YOLO26-X"],
|
| 117 |
-
label="Model",
|
| 118 |
-
)
|
| 119 |
-
y26_task = gr.Dropdown(list(TASK_FILE_SUFFIX.keys()), label="Task")
|
| 120 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 121 |
-
y26_conf = gr.Slider(0, 1, value=0.25, label="Confidence Threshold")
|
| 122 |
-
y26_iou = gr.Slider(0, 1, value=0.45, label="IoU Threshold")
|
| 123 |
-
y26_retina = gr.Checkbox(label="Retina Masks", value=True, info="Higher quality masks, slower inference")
|
| 124 |
-
y26_btn = gr.Button("Run Inference", variant="primary")
|
| 125 |
-
with gr.Column():
|
| 126 |
-
y26_output = gr.Image(type="pil", label="Result")
|
| 127 |
-
y26_label = gr.Label(label="Classification Results", visible=False)
|
| 128 |
-
|
| 129 |
-
y26_task.change(
|
| 130 |
-
lambda t: (gr.update(visible=t != "Classification"), gr.update(visible=t == "Classification")),
|
| 131 |
-
y26_task,
|
| 132 |
-
[y26_output, y26_label],
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
gr.Examples(
|
| 136 |
-
examples=[
|
| 137 |
-
[str(ASSETS / "bus.jpg"), "YOLO26-M", "Detection", 0.25, 0.45, True],
|
| 138 |
-
[str(ASSETS / "bus.jpg"), "YOLO26-M", "Segmentation", 0.25, 0.45, True],
|
| 139 |
-
[str(ASSETS / "zidane.jpg"), "YOLO26-M", "Pose", 0.25, 0.45, True],
|
| 140 |
-
[str(OBB_IMAGE), "YOLO26-M", "OBB", 0.25, 0.45, True],
|
| 141 |
-
],
|
| 142 |
-
inputs=[y26_image, y26_model, y26_task, y26_conf, y26_iou, y26_retina],
|
| 143 |
-
outputs=[y26_output, y26_label],
|
| 144 |
-
fn=predict_yolo26,
|
| 145 |
-
cache_examples=True,
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
y26_btn.click(
|
| 149 |
-
predict_yolo26,
|
| 150 |
-
[y26_image, y26_model, y26_task, y26_conf, y26_iou, y26_retina],
|
| 151 |
-
[y26_output, y26_label],
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
with gr.Tab("YOLOE26 Open-Vocabulary"):
|
| 155 |
-
gr.Markdown("### Ultralytics YOLOE26: Open-Vocabulary Segmentation - Detect any object by text description")
|
| 156 |
-
with gr.Row():
|
| 157 |
-
with gr.Column():
|
| 158 |
-
ye_image = gr.Image(type="pil", label="Upload Image", value=str(ASSETS / "bus.jpg"))
|
| 159 |
-
with gr.Row():
|
| 160 |
-
ye_model = gr.Dropdown(
|
| 161 |
-
["YOLOE-26N", "YOLOE-26S", "YOLOE-26M", "YOLOE-26L", "YOLOE-26X"],
|
| 162 |
-
value="YOLOE-26L",
|
| 163 |
-
label="Model",
|
| 164 |
-
)
|
| 165 |
-
ye_classes = gr.Textbox(value="person, bus, car", label="Classes", placeholder="person, dog, cat...")
|
| 166 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 167 |
-
ye_conf = gr.Slider(0, 1, value=0.2, label="Confidence Threshold")
|
| 168 |
-
ye_retina = gr.Checkbox(value=True, label="Retina Masks", info="Higher quality masks, slower inference")
|
| 169 |
-
ye_btn = gr.Button("Run Inference", variant="primary")
|
| 170 |
-
with gr.Column():
|
| 171 |
-
ye_output = gr.Image(type="pil", label="Result")
|
| 172 |
-
|
| 173 |
-
gr.Examples(
|
| 174 |
-
examples=[
|
| 175 |
-
[str(ASSETS / "bus.jpg"), "YOLOE-26L", "person, bus, car", 0.2, True],
|
| 176 |
-
[str(ASSETS / "zidane.jpg"), "YOLOE-26L", "person, football, grass", 0.2, True],
|
| 177 |
-
],
|
| 178 |
-
inputs=[ye_image, ye_model, ye_classes, ye_conf, ye_retina],
|
| 179 |
-
outputs=ye_output,
|
| 180 |
-
fn=predict_yoloe26,
|
| 181 |
-
cache_examples=True,
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
ye_btn.click(predict_yoloe26, [ye_image, ye_model, ye_classes, ye_conf, ye_retina], ye_output)
|
| 185 |
-
|
| 186 |
-
if __name__ == "__main__":
|
| 187 |
-
demo.launch(theme=theme, allowed_paths=[str(ASSETS), str(ASSETS.parent)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
|
|
|
|
| 13 |
# Model cache
|
| 14 |
model_cache = {}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
TASK_REPO_SUFFIX = {
|
| 17 |
"Detection": "",
|
| 18 |
"Segmentation": "-seg",
|
|
|
|
| 25 |
# "YOLO26-M" -> "m", "YOLOE-26L" -> "l"
|
| 26 |
return label.strip()[-1].lower()
|
| 27 |
|
| 28 |
+
def _get_model(repo_id: str) -> YOLO:
|
| 29 |
+
if repo_id not in model_cache:
|
| 30 |
+
path = hf_hub_download(repo_id=repo_id, filename="model.pt")
|
| 31 |
+
model_cache[repo_id] = YOLO(path)
|
| 32 |
+
return model_cache[repo_id]
|
|
|
|
| 33 |
|
| 34 |
def predict_yolo26(image, model_name, task, conf, iou, retina):
|
| 35 |
+
scale = _scale_from_label(model_name)
|
|
|
|
| 36 |
|
| 37 |
+
# openvision/yolo26-n, yolo26-n-seg, yolo26-n-pose, etc.
|
| 38 |
repo_id = f"openvision/yolo26-{scale}{TASK_REPO_SUFFIX[task]}"
|
| 39 |
+
model = _get_model(repo_id)
|
| 40 |
+
|
| 41 |
+
results = model.predict(
|
| 42 |
+
source=image,
|
| 43 |
+
conf=conf,
|
| 44 |
+
iou=iou,
|
| 45 |
+
imgsz=640,
|
| 46 |
+
retina_masks=bool(retina and task == "Segmentation"),
|
| 47 |
+
)
|
| 48 |
|
| 49 |
if task == "Classification":
|
| 50 |
top5 = results[0].probs.top5
|
|
|
|
| 56 |
return Image.fromarray(results[0].plot()[..., ::-1]), None
|
| 57 |
|
| 58 |
def predict_yoloe26(image, model_name, classes_text, conf, retina):
|
| 59 |
+
scale = _scale_from_label(model_name)
|
|
|
|
| 60 |
|
| 61 |
+
# openvision/yoloe26-n-seg (open-vocab)
|
| 62 |
repo_id = f"openvision/yoloe26-{scale}-seg"
|
| 63 |
+
model = _get_model(repo_id)
|
| 64 |
|
| 65 |
+
names = [c.strip() for c in classes_text.split(",") if c.strip()]
|
| 66 |
+
if not names:
|
| 67 |
+
names = ["person", "car", "dog", "cat"]
|
| 68 |
|
|
|
|
| 69 |
model.set_classes(names, model.get_text_pe(names))
|
| 70 |
|
| 71 |
+
results = model.predict(
|
| 72 |
+
source=image,
|
| 73 |
+
conf=conf,
|
| 74 |
+
imgsz=640,
|
| 75 |
+
retina_masks=bool(retina),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
return Image.fromarray(results[0].plot()[..., ::-1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|