Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -35,22 +35,19 @@ def setup_cfg(config_file):
|
|
35 |
class IMGState:
|
36 |
def __init__(self):
|
37 |
self.img = None
|
38 |
-
self.img_feat = None
|
39 |
self.selected_points = []
|
40 |
self.selected_points_labels = []
|
41 |
self.selected_bboxes = []
|
42 |
|
43 |
self.available_to_set = True
|
44 |
|
45 |
-
def set_img(self, img
|
46 |
self.img = img
|
47 |
-
self.img_feat = img_feat
|
48 |
|
49 |
self.available_to_set = False
|
50 |
|
51 |
def clear(self):
|
52 |
self.img = None
|
53 |
-
self.img_feat = None
|
54 |
self.selected_points = []
|
55 |
self.selected_points_labels = []
|
56 |
self.selected_bboxes = []
|
@@ -62,13 +59,6 @@ class IMGState:
|
|
62 |
self.selected_points_labels = []
|
63 |
self.selected_bboxes = []
|
64 |
|
65 |
-
def to_device(self, device=torch.device("cuda")):
|
66 |
-
if self.img_feat is not None:
|
67 |
-
for k in self.img_feat:
|
68 |
-
if isinstance(self.img_feat[k], torch.Tensor):
|
69 |
-
self.img_feat[k] = self.img_feat[k].to(device)
|
70 |
-
elif isinstance(self.img_feat[k], tuple):
|
71 |
-
self.img_feat[k] = tuple(v.to(device) for v in self.img_feat[k])
|
72 |
|
73 |
@property
|
74 |
def available(self):
|
@@ -160,15 +150,27 @@ def get_points_with_draw(image, img_state, evt: gr.SelectData):
|
|
160 |
|
161 |
img_state.selected_points.append([x, y])
|
162 |
img_state.selected_points_labels.append(1 if label == "Add Mask" else 0)
|
163 |
-
img_state.
|
|
|
|
|
164 |
draw = ImageDraw.Draw(image)
|
165 |
-
draw.
|
166 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
fill=point_color,
|
168 |
)
|
169 |
return img_state, image
|
170 |
|
171 |
def get_bbox_with_draw(image, img_state, evt: gr.SelectData):
|
|
|
172 |
x, y = evt.index[0], evt.index[1]
|
173 |
point_radius, point_color, box_outline = 5, (237, 34, 13), 2
|
174 |
box_color = (237, 34, 13)
|
@@ -180,7 +182,9 @@ def get_bbox_with_draw(image, img_state, evt: gr.SelectData):
|
|
180 |
image = Image.fromarray(img_state.img)
|
181 |
else:
|
182 |
raise ValueError(f"Cannot be {len(img_state.selected_bboxes)}")
|
183 |
-
|
|
|
|
|
184 |
|
185 |
draw = ImageDraw.Draw(image)
|
186 |
draw.ellipse(
|
@@ -250,9 +254,10 @@ examples = [
|
|
250 |
|
251 |
examples_point = [
|
252 |
['./demo/images/ADE_val_00000739.jpg'],
|
253 |
-
['./demo/images/
|
254 |
-
['./demo/images/
|
255 |
['./demo/images/ADE_val_00000001.jpg'],
|
|
|
256 |
['./demo/images/000000000785.jpg'],
|
257 |
]
|
258 |
|
@@ -274,8 +279,8 @@ Mask-Adapter effectively extends to SAM or SAM-2 without additional training, ac
|
|
274 |
"""
|
275 |
|
276 |
with gr.Blocks() as demo:
|
277 |
-
gr.Markdown(title)
|
278 |
-
gr.Markdown(description)
|
279 |
|
280 |
with gr.Tabs():
|
281 |
with gr.TabItem("Automatic Mode"):
|
@@ -303,6 +308,8 @@ with gr.Blocks() as demo:
|
|
303 |
input_image = gr.Image( label="Input Image", type="pil")
|
304 |
with gr.Column(scale=1): # 第二列:分割图输出
|
305 |
output_image_box = gr.Image(type="pil", label='Segmentation Map',interactive=False) # 输出分割图
|
|
|
|
|
306 |
|
307 |
input_image.select(
|
308 |
get_bbox_with_draw,
|
@@ -325,9 +332,24 @@ with gr.Blocks() as demo:
|
|
325 |
inputs=[img_state_bbox],
|
326 |
outputs=[img_state_bbox, input_image, output_image_box]
|
327 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
|
329 |
-
|
330 |
-
|
|
|
|
|
|
|
|
|
|
|
331 |
|
332 |
with gr.TabItem("Point Mode"):
|
333 |
img_state_points = gr.State(value=IMGState())
|
@@ -358,9 +380,22 @@ with gr.Blocks() as demo:
|
|
358 |
inputs=[img_state_points],
|
359 |
outputs=[img_state_points, input_image, output_image_point]
|
360 |
)
|
361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
with gr.Row():
|
363 |
-
gr.Examples(examples=examples_point, inputs=input_image, outputs=output_image_point,examples_per_page=5)
|
|
|
364 |
|
365 |
|
366 |
|
|
|
35 |
class IMGState:
|
36 |
def __init__(self):
|
37 |
self.img = None
|
|
|
38 |
self.selected_points = []
|
39 |
self.selected_points_labels = []
|
40 |
self.selected_bboxes = []
|
41 |
|
42 |
self.available_to_set = True
|
43 |
|
44 |
+
def set_img(self, img):
|
45 |
self.img = img
|
|
|
46 |
|
47 |
self.available_to_set = False
|
48 |
|
49 |
def clear(self):
|
50 |
self.img = None
|
|
|
51 |
self.selected_points = []
|
52 |
self.selected_points_labels = []
|
53 |
self.selected_bboxes = []
|
|
|
59 |
self.selected_points_labels = []
|
60 |
self.selected_bboxes = []
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
@property
|
64 |
def available(self):
|
|
|
150 |
|
151 |
img_state.selected_points.append([x, y])
|
152 |
img_state.selected_points_labels.append(1 if label == "Add Mask" else 0)
|
153 |
+
if img_state.img is None:
|
154 |
+
img_state.set_img(np.array(image))
|
155 |
+
|
156 |
draw = ImageDraw.Draw(image)
|
157 |
+
draw.polygon(
|
158 |
+
[
|
159 |
+
(x, y - point_radius),
|
160 |
+
(x + point_radius * 0.25, y - point_radius * 0.25),
|
161 |
+
(x + point_radius, y),
|
162 |
+
(x + point_radius * 0.25, y + point_radius * 0.25),
|
163 |
+
(x, y + point_radius),
|
164 |
+
(x - point_radius * 0.25, y + point_radius * 0.25),
|
165 |
+
(x - point_radius, y),
|
166 |
+
(x - point_radius * 0.25, y - point_radius * 0.25)
|
167 |
+
],
|
168 |
fill=point_color,
|
169 |
)
|
170 |
return img_state, image
|
171 |
|
172 |
def get_bbox_with_draw(image, img_state, evt: gr.SelectData):
|
173 |
+
|
174 |
x, y = evt.index[0], evt.index[1]
|
175 |
point_radius, point_color, box_outline = 5, (237, 34, 13), 2
|
176 |
box_color = (237, 34, 13)
|
|
|
182 |
image = Image.fromarray(img_state.img)
|
183 |
else:
|
184 |
raise ValueError(f"Cannot be {len(img_state.selected_bboxes)}")
|
185 |
+
|
186 |
+
if img_state.img is None:
|
187 |
+
img_state.set_img(np.array(image))
|
188 |
|
189 |
draw = ImageDraw.Draw(image)
|
190 |
draw.ellipse(
|
|
|
254 |
|
255 |
examples_point = [
|
256 |
['./demo/images/ADE_val_00000739.jpg'],
|
257 |
+
['./demo/images/000000052462.jpg'],
|
258 |
+
['./demo/images/000000081766.jpg'],
|
259 |
['./demo/images/ADE_val_00000001.jpg'],
|
260 |
+
['./demo/images/000000033707.jpg'],
|
261 |
['./demo/images/000000000785.jpg'],
|
262 |
]
|
263 |
|
|
|
279 |
"""
|
280 |
|
281 |
with gr.Blocks() as demo:
|
282 |
+
gr.Markdown(title)
|
283 |
+
gr.Markdown(description)
|
284 |
|
285 |
with gr.Tabs():
|
286 |
with gr.TabItem("Automatic Mode"):
|
|
|
308 |
input_image = gr.Image( label="Input Image", type="pil")
|
309 |
with gr.Column(scale=1): # 第二列:分割图输出
|
310 |
output_image_box = gr.Image(type="pil", label='Segmentation Map',interactive=False) # 输出分割图
|
311 |
+
|
312 |
+
gr.Markdown("Click the top-left and bottom-right corners of the image to select a rectangular area")
|
313 |
|
314 |
input_image.select(
|
315 |
get_bbox_with_draw,
|
|
|
332 |
inputs=[img_state_bbox],
|
333 |
outputs=[img_state_bbox, input_image, output_image_box]
|
334 |
)
|
335 |
+
input_image.clear(
|
336 |
+
clear_everything,
|
337 |
+
inputs=[img_state_bbox],
|
338 |
+
outputs=[img_state_bbox, input_image, output_image_box]
|
339 |
+
)
|
340 |
+
output_image_box.clear(
|
341 |
+
clear_everything,
|
342 |
+
inputs=[img_state_bbox],
|
343 |
+
outputs=[img_state_bbox, input_image, output_image_box]
|
344 |
+
)
|
345 |
|
346 |
+
|
347 |
+
gr.Examples(
|
348 |
+
examples=examples_point,
|
349 |
+
inputs=input_image,
|
350 |
+
outputs=output_image_box,
|
351 |
+
examples_per_page=5,
|
352 |
+
)
|
353 |
|
354 |
with gr.TabItem("Point Mode"):
|
355 |
img_state_points = gr.State(value=IMGState())
|
|
|
380 |
inputs=[img_state_points],
|
381 |
outputs=[img_state_points, input_image, output_image_point]
|
382 |
)
|
383 |
+
input_image.clear(
|
384 |
+
clear_everything,
|
385 |
+
inputs=[img_state_points],
|
386 |
+
outputs=[img_state_points, input_image, output_image_point]
|
387 |
+
)
|
388 |
+
output_image_point.clear(
|
389 |
+
clear_everything,
|
390 |
+
inputs=[img_state_points],
|
391 |
+
outputs=[img_state_points, input_image, output_image_point]
|
392 |
+
)
|
393 |
+
def clear_and_set_example_point(example):
|
394 |
+
clear_everything(img_state_points)
|
395 |
+
return example
|
396 |
with gr.Row():
|
397 |
+
gr.Examples(examples=examples_point, inputs=input_image, outputs=output_image_point,examples_per_page=5, preprocess=clear_and_set_example_point)
|
398 |
+
|
399 |
|
400 |
|
401 |
|