Spaces:
Running
on
T4
Running
on
T4
Advanced configuration added
Browse files
app.py
CHANGED
@@ -1,20 +1,23 @@
|
|
1 |
from typing import List
|
2 |
|
3 |
import cv2
|
4 |
-
import torch
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import supervision as sv
|
|
|
8 |
from inference.models import YOLOWorld
|
9 |
|
10 |
from utils.efficient_sam import load, inference_with_box
|
11 |
|
12 |
MARKDOWN = """
|
13 |
-
# YOLO-World
|
14 |
|
15 |
-
This is a demo of zero-shot instance segmentation using
|
|
|
|
|
16 |
|
17 |
-
Powered by Roboflow [Inference](https://github.com/roboflow/inference) and
|
|
|
18 |
"""
|
19 |
|
20 |
EXAMPLES = [
|
@@ -35,19 +38,21 @@ def process_categories(categories: str) -> List[str]:
|
|
35 |
|
36 |
|
37 |
def process_image(
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
) -> np.ndarray:
|
46 |
categories = process_categories(categories)
|
47 |
YOLO_WORLD_MODEL.set_classes(categories)
|
48 |
results = YOLO_WORLD_MODEL.infer(input_image, confidence=confidence_threshold)
|
49 |
detections = sv.Detections.from_inference(results)
|
50 |
-
detections = detections.with_nms(
|
|
|
|
|
51 |
if with_segmentation:
|
52 |
masks = []
|
53 |
for [x_min, y_min, x_max, y_max] in detections.xyxy:
|
@@ -57,7 +62,11 @@ def process_image(
|
|
57 |
detections.mask = np.array(masks)
|
58 |
|
59 |
labels = [
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
for class_id, confidence in
|
62 |
zip(detections.class_id, detections.confidence)
|
63 |
]
|
@@ -70,8 +79,67 @@ def process_image(
|
|
70 |
return output_image
|
71 |
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
with gr.Blocks() as demo:
|
74 |
gr.Markdown(MARKDOWN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
with gr.Row():
|
76 |
input_image_component = gr.Image(
|
77 |
type='numpy',
|
@@ -85,19 +153,39 @@ with gr.Blocks() as demo:
|
|
85 |
categories_text_component = gr.Textbox(
|
86 |
label='Categories',
|
87 |
placeholder='comma separated list of categories',
|
88 |
-
scale=
|
|
|
|
|
|
|
|
|
|
|
89 |
)
|
90 |
-
submit_button_component = gr.Button('Submit', scale=1)
|
91 |
gr.Examples(
|
92 |
fn=process_image,
|
93 |
examples=EXAMPLES,
|
94 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
outputs=output_image_component
|
96 |
)
|
97 |
|
98 |
submit_button_component.click(
|
99 |
fn=process_image,
|
100 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
outputs=output_image_component
|
102 |
)
|
103 |
|
|
|
1 |
from typing import List
|
2 |
|
3 |
import cv2
|
|
|
4 |
import gradio as gr
|
5 |
import numpy as np
|
6 |
import supervision as sv
|
7 |
+
import torch
|
8 |
from inference.models import YOLOWorld
|
9 |
|
10 |
from utils.efficient_sam import load, inference_with_box
|
11 |
|
12 |
MARKDOWN = """
|
13 |
+
# YOLO-World + EfficientSAM 🔥
|
14 |
|
15 |
+
This is a demo of zero-shot instance segmentation using
|
16 |
+
[YOLO-World](https://github.com/AILab-CVC/YOLO-World) and
|
17 |
+
[EfficientSAM](https://github.com/yformer/EfficientSAM).
|
18 |
|
19 |
+
Powered by Roboflow [Inference](https://github.com/roboflow/inference) and
|
20 |
+
[Supervision](https://github.com/roboflow/supervision).
|
21 |
"""
|
22 |
|
23 |
EXAMPLES = [
|
|
|
38 |
|
39 |
|
40 |
def process_image(
|
41 |
+
input_image: np.ndarray,
|
42 |
+
categories: str,
|
43 |
+
confidence_threshold: float = 0.3,
|
44 |
+
iou_threshold: float = 0.5,
|
45 |
+
with_segmentation: bool = True,
|
46 |
+
with_confidence: bool = False,
|
47 |
+
with_class_agnostic_nms: bool = False,
|
48 |
) -> np.ndarray:
|
49 |
categories = process_categories(categories)
|
50 |
YOLO_WORLD_MODEL.set_classes(categories)
|
51 |
results = YOLO_WORLD_MODEL.infer(input_image, confidence=confidence_threshold)
|
52 |
detections = sv.Detections.from_inference(results)
|
53 |
+
detections = detections.with_nms(
|
54 |
+
class_agnostic=with_class_agnostic_nms,
|
55 |
+
threshold=iou_threshold)
|
56 |
if with_segmentation:
|
57 |
masks = []
|
58 |
for [x_min, y_min, x_max, y_max] in detections.xyxy:
|
|
|
62 |
detections.mask = np.array(masks)
|
63 |
|
64 |
labels = [
|
65 |
+
(
|
66 |
+
f"{categories[class_id]}: {confidence:.2f}"
|
67 |
+
if with_confidence
|
68 |
+
else f"{categories[class_id]}"
|
69 |
+
)
|
70 |
for class_id, confidence in
|
71 |
zip(detections.class_id, detections.confidence)
|
72 |
]
|
|
|
79 |
return output_image
|
80 |
|
81 |
|
82 |
+
confidence_threshold_component = gr.Slider(
|
83 |
+
minimum=0,
|
84 |
+
maximum=1.0,
|
85 |
+
value=0.3,
|
86 |
+
step=0.01,
|
87 |
+
label="Confidence Threshold",
|
88 |
+
info=(
|
89 |
+
"The confidence threshold for the YOLO-World model. Lower the threshold to "
|
90 |
+
"reduce false negatives, enhancing the model's sensitivity to detect "
|
91 |
+
"sought-after objects. Conversely, increase the threshold to minimize false "
|
92 |
+
"positives, preventing the model from identifying objects it shouldn't."
|
93 |
+
))
|
94 |
+
|
95 |
+
iou_threshold_component = gr.Slider(
|
96 |
+
minimum=0,
|
97 |
+
maximum=1.0,
|
98 |
+
value=0.5,
|
99 |
+
step=0.01,
|
100 |
+
label="IoU Threshold",
|
101 |
+
info=(
|
102 |
+
"The Intersection over Union (IoU) threshold for non-maximum suppression. "
|
103 |
+
"Decrease the value to lessen the occurrence of overlapping bounding boxes, "
|
104 |
+
"making the detection process stricter. On the other hand, increase the value "
|
105 |
+
"to allow more overlapping bounding boxes, accommodating a broader range of "
|
106 |
+
"detections."
|
107 |
+
))
|
108 |
+
|
109 |
+
with_segmentation_component = gr.Checkbox(
|
110 |
+
value=True,
|
111 |
+
label="With Segmentation",
|
112 |
+
info=(
|
113 |
+
"Whether to run EfficientSAM for instance segmentation."
|
114 |
+
)
|
115 |
+
)
|
116 |
+
|
117 |
+
with_confidence_component = gr.Checkbox(
|
118 |
+
value=False,
|
119 |
+
label="Display Confidence",
|
120 |
+
info=(
|
121 |
+
"Whether to display the confidence of the detected objects."
|
122 |
+
)
|
123 |
+
)
|
124 |
+
|
125 |
+
with_class_agnostic_nms_component = gr.Checkbox(
|
126 |
+
value=False,
|
127 |
+
label="Use Class-Agnostic NMS",
|
128 |
+
info=(
|
129 |
+
"Suppress overlapping bounding boxes across all classes."
|
130 |
+
)
|
131 |
+
)
|
132 |
+
|
133 |
+
|
134 |
with gr.Blocks() as demo:
|
135 |
gr.Markdown(MARKDOWN)
|
136 |
+
with gr.Accordion("Configuration", open=False):
|
137 |
+
confidence_threshold_component.render()
|
138 |
+
iou_threshold_component.render()
|
139 |
+
with gr.Row():
|
140 |
+
with_segmentation_component.render()
|
141 |
+
with_confidence_component.render()
|
142 |
+
with_class_agnostic_nms_component.render()
|
143 |
with gr.Row():
|
144 |
input_image_component = gr.Image(
|
145 |
type='numpy',
|
|
|
153 |
categories_text_component = gr.Textbox(
|
154 |
label='Categories',
|
155 |
placeholder='comma separated list of categories',
|
156 |
+
scale=7
|
157 |
+
)
|
158 |
+
submit_button_component = gr.Button(
|
159 |
+
value='Submit',
|
160 |
+
scale=1,
|
161 |
+
variant='primary'
|
162 |
)
|
|
|
163 |
gr.Examples(
|
164 |
fn=process_image,
|
165 |
examples=EXAMPLES,
|
166 |
+
inputs=[
|
167 |
+
input_image_component,
|
168 |
+
categories_text_component,
|
169 |
+
confidence_threshold_component,
|
170 |
+
iou_threshold_component,
|
171 |
+
with_segmentation_component,
|
172 |
+
with_confidence_component,
|
173 |
+
with_class_agnostic_nms_component
|
174 |
+
],
|
175 |
outputs=output_image_component
|
176 |
)
|
177 |
|
178 |
submit_button_component.click(
|
179 |
fn=process_image,
|
180 |
+
inputs=[
|
181 |
+
input_image_component,
|
182 |
+
categories_text_component,
|
183 |
+
confidence_threshold_component,
|
184 |
+
iou_threshold_component,
|
185 |
+
with_segmentation_component,
|
186 |
+
with_confidence_component,
|
187 |
+
with_class_agnostic_nms_component
|
188 |
+
],
|
189 |
outputs=output_image_component
|
190 |
)
|
191 |
|