Spaces:
Runtime error
Runtime error
improve user interaction
Browse files- app.py +130 -177
- assets/box-instructions.mov +0 -3
- assets/point-instructions.mov +0 -3
app.py
CHANGED
|
@@ -49,30 +49,32 @@ predictor = SamPredictor(sam)
|
|
| 49 |
# Description
|
| 50 |
title = "<center><strong><font size='8'>EdgeSAM<font></strong> <a href='https://github.com/chongzhou96/EdgeSAM'><font size='6'>[GitHub]</font></a> </center>"
|
| 51 |
|
| 52 |
-
description_p = """ # Instructions for point mode
|
| 53 |
|
| 54 |
1. Upload an image or click one of the provided examples.
|
| 55 |
2. Select the point type.
|
| 56 |
3. Click once or multiple times on the image to indicate the object of interest.
|
| 57 |
-
4.
|
| 58 |
-
5. The
|
| 59 |
-
6. The Reset button resets both points and the image.
|
| 60 |
|
| 61 |
"""
|
| 62 |
|
| 63 |
-
description_b = """ # Instructions for box mode
|
| 64 |
|
| 65 |
1. Upload an image or click one of the provided examples.
|
| 66 |
2. Click twice on the image (diagonal points of the box).
|
| 67 |
-
3.
|
| 68 |
-
4. The
|
| 69 |
-
5. The Reset button resets both the box and the image.
|
| 70 |
|
| 71 |
"""
|
| 72 |
|
| 73 |
description_e = """ # Everything mode is NOT recommended.
|
| 74 |
|
| 75 |
Since EdgeSAM follows the same encoder-decoder architecture as SAM, the everything mode will infer the decoder 32x32=1024 times, which is inefficient, thus a longer processing time is expected.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
"""
|
| 78 |
|
|
@@ -95,14 +97,13 @@ examples = [
|
|
| 95 |
["assets/16.jpeg"]
|
| 96 |
]
|
| 97 |
|
| 98 |
-
default_example = examples[0]
|
| 99 |
-
|
| 100 |
css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"
|
| 101 |
|
| 102 |
global_points = []
|
| 103 |
global_point_label = []
|
| 104 |
global_box = []
|
| 105 |
global_image = None
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
def reset():
|
|
@@ -110,26 +111,29 @@ def reset():
|
|
| 110 |
global global_point_label
|
| 111 |
global global_box
|
| 112 |
global global_image
|
|
|
|
| 113 |
global_points = []
|
| 114 |
global_point_label = []
|
| 115 |
global_box = []
|
| 116 |
global_image = None
|
| 117 |
-
|
| 118 |
-
|
| 119 |
|
| 120 |
def reset_all():
|
| 121 |
global global_points
|
| 122 |
global global_point_label
|
| 123 |
global global_box
|
| 124 |
global global_image
|
|
|
|
| 125 |
global_points = []
|
| 126 |
global_point_label = []
|
| 127 |
global_box = []
|
| 128 |
global_image = None
|
|
|
|
| 129 |
if args.enable_everything_mode:
|
| 130 |
-
return None, None, None
|
| 131 |
else:
|
| 132 |
-
return None, None
|
| 133 |
|
| 134 |
|
| 135 |
def clear():
|
|
@@ -137,10 +141,12 @@ def clear():
|
|
| 137 |
global global_point_label
|
| 138 |
global global_box
|
| 139 |
global global_image
|
|
|
|
| 140 |
global_points = []
|
| 141 |
global_point_label = []
|
| 142 |
global_box = []
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
def on_image_upload(image, input_size=1024):
|
|
@@ -148,6 +154,7 @@ def on_image_upload(image, input_size=1024):
|
|
| 148 |
global global_point_label
|
| 149 |
global global_box
|
| 150 |
global global_image
|
|
|
|
| 151 |
global_points = []
|
| 152 |
global_point_label = []
|
| 153 |
global_box = []
|
|
@@ -159,11 +166,12 @@ def on_image_upload(image, input_size=1024):
|
|
| 159 |
new_h = int(h * scale)
|
| 160 |
image = image.resize((new_w, new_h))
|
| 161 |
global_image = copy.deepcopy(image)
|
|
|
|
| 162 |
print("Image changed")
|
| 163 |
nd_image = np.array(global_image)
|
| 164 |
predictor.set_image(nd_image)
|
| 165 |
|
| 166 |
-
return image
|
| 167 |
|
| 168 |
|
| 169 |
def convert_box(xyxy):
|
|
@@ -177,85 +185,37 @@ def convert_box(xyxy):
|
|
| 177 |
xyxy[1][1] = max_y
|
| 178 |
return xyxy
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
global global_points
|
| 183 |
global global_point_label
|
| 184 |
-
|
| 185 |
|
| 186 |
x, y = evt.index[0], evt.index[1]
|
| 187 |
-
|
| 188 |
-
# y = int(y * scale)
|
| 189 |
-
point_radius, point_color = 10, (97, 217, 54) if label == "Positive" else (237, 34, 13)
|
| 190 |
global_points.append([x, y])
|
| 191 |
global_point_label.append(1 if label == "Positive" else 0)
|
| 192 |
|
| 193 |
print(f'global_points: {global_points}')
|
| 194 |
print(f'global_point_label: {global_point_label}')
|
| 195 |
|
| 196 |
-
draw = ImageDraw.Draw(
|
| 197 |
-
draw.ellipse(
|
| 198 |
-
[(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],
|
| 199 |
-
fill=point_color,
|
| 200 |
-
)
|
| 201 |
-
return image
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
def get_box_with_draw(image, evt: gr.SelectData):
|
| 205 |
-
global global_box
|
| 206 |
-
# global global_image
|
| 207 |
-
|
| 208 |
-
x, y = evt.index[0], evt.index[1]
|
| 209 |
-
# x = float(x * scale)
|
| 210 |
-
# y = float(y * scale)
|
| 211 |
-
point_radius, point_color, box_outline = 5, (97, 217, 54), 5
|
| 212 |
-
box_color = (0, 255, 0)
|
| 213 |
-
|
| 214 |
-
if len(global_box) == 0:
|
| 215 |
-
global_box.append([x, y])
|
| 216 |
-
elif len(global_box) == 1:
|
| 217 |
-
global_box.append([x, y])
|
| 218 |
-
elif len(global_box) == 2:
|
| 219 |
-
global_box = [[x, y]]
|
| 220 |
-
|
| 221 |
-
print(f'global_box: {global_box}')
|
| 222 |
-
|
| 223 |
-
draw = ImageDraw.Draw(image)
|
| 224 |
draw.ellipse(
|
| 225 |
[(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],
|
| 226 |
fill=point_color,
|
| 227 |
)
|
| 228 |
-
|
| 229 |
-
if len(global_box) == 2:
|
| 230 |
-
global_box = convert_box(global_box)
|
| 231 |
-
xy = (global_box[0][0], global_box[0][1], global_box[1][0], global_box[1][1])
|
| 232 |
-
draw.rectangle(
|
| 233 |
-
xy,
|
| 234 |
-
outline=box_color,
|
| 235 |
-
width=box_outline
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
return image
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
def segment_with_points(
|
| 242 |
-
image,
|
| 243 |
-
input_size=1024,
|
| 244 |
-
better_quality=False,
|
| 245 |
-
withContours=True,
|
| 246 |
-
use_retina=True,
|
| 247 |
-
mask_random_color=False,
|
| 248 |
-
):
|
| 249 |
-
global global_points
|
| 250 |
-
global global_point_label
|
| 251 |
|
| 252 |
global_points_np = np.array(global_points)
|
| 253 |
global_point_label_np = np.array(global_point_label)
|
| 254 |
|
| 255 |
-
if global_points_np.size == 0 and global_point_label_np.size == 0:
|
| 256 |
-
print("No point selected")
|
| 257 |
-
return image, image
|
| 258 |
-
|
| 259 |
num_multimask_outputs = 4
|
| 260 |
|
| 261 |
masks, scores, logits = predictor.predict(
|
|
@@ -286,11 +246,11 @@ def segment_with_points(
|
|
| 286 |
withContours=withContours,
|
| 287 |
)
|
| 288 |
|
| 289 |
-
return
|
| 290 |
|
| 291 |
|
| 292 |
def segment_with_box(
|
| 293 |
-
|
| 294 |
input_size=1024,
|
| 295 |
better_quality=False,
|
| 296 |
withContours=True,
|
|
@@ -298,31 +258,60 @@ def segment_with_box(
|
|
| 298 |
mask_random_color=False,
|
| 299 |
):
|
| 300 |
global global_box
|
| 301 |
-
|
|
|
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
mask_random_color=mask_random_color,
|
| 320 |
-
bbox=None,
|
| 321 |
-
use_retina=use_retina,
|
| 322 |
-
withContours=withContours,
|
| 323 |
)
|
|
|
|
| 324 |
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
|
| 328 |
def segment_everything(
|
|
@@ -351,18 +340,14 @@ def segment_everything(
|
|
| 351 |
return seg
|
| 352 |
|
| 353 |
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
segm_img_p = gr.Image(label="Segmented Image with points", interactive=False, type="pil")
|
| 359 |
-
segm_img_b = gr.Image(label="Segmented Image with box", interactive=False, type="pil")
|
| 360 |
-
segm_img_e = gr.Image(label="Segmented Everything", interactive=False, type="pil")
|
| 361 |
|
| 362 |
if args.enable_everything_mode:
|
| 363 |
-
all_outputs = [
|
| 364 |
else:
|
| 365 |
-
all_outputs = [
|
| 366 |
|
| 367 |
with gr.Blocks(css=css, title="EdgeSAM") as demo:
|
| 368 |
|
|
@@ -375,14 +360,8 @@ with gr.Blocks(css=css, title="EdgeSAM") as demo:
|
|
| 375 |
# Images
|
| 376 |
with gr.Row(variant="panel"):
|
| 377 |
with gr.Column(scale=1):
|
| 378 |
-
|
| 379 |
-
|
| 380 |
with gr.Column(scale=1):
|
| 381 |
-
segm_img_p.render()
|
| 382 |
-
|
| 383 |
-
# Submit & Clear
|
| 384 |
-
with gr.Row():
|
| 385 |
-
with gr.Column():
|
| 386 |
with gr.Row():
|
| 387 |
add_or_remove = gr.Radio(
|
| 388 |
["Positive", "Negative"],
|
|
@@ -391,118 +370,92 @@ with gr.Blocks(css=css, title="EdgeSAM") as demo:
|
|
| 391 |
)
|
| 392 |
|
| 393 |
with gr.Column():
|
| 394 |
-
segment_btn_p = gr.Button(
|
| 395 |
-
"Start", variant="primary"
|
| 396 |
-
)
|
| 397 |
clear_btn_p = gr.Button("Clear", variant="secondary")
|
| 398 |
reset_btn_p = gr.Button("Reset", variant="secondary")
|
|
|
|
|
|
|
| 399 |
|
|
|
|
|
|
|
| 400 |
gr.Markdown("Try some of the examples below ⬇️")
|
| 401 |
gr.Examples(
|
| 402 |
examples=examples,
|
| 403 |
-
inputs=[
|
| 404 |
-
outputs=[
|
| 405 |
-
examples_per_page=
|
| 406 |
fn=on_image_upload,
|
| 407 |
run_on_click=True
|
| 408 |
)
|
| 409 |
|
| 410 |
-
with gr.Column():
|
| 411 |
-
# Description
|
| 412 |
-
gr.Markdown(description_p)
|
| 413 |
-
|
| 414 |
with gr.Tab("Box mode") as tab_b:
|
| 415 |
# Images
|
| 416 |
with gr.Row(variant="panel"):
|
| 417 |
with gr.Column(scale=1):
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
|
|
|
|
|
|
| 422 |
|
| 423 |
-
# Submit & Clear
|
| 424 |
with gr.Row():
|
| 425 |
with gr.Column():
|
| 426 |
-
with gr.Row():
|
| 427 |
-
with gr.Column():
|
| 428 |
-
segment_btn_b = gr.Button(
|
| 429 |
-
"Start", variant="primary"
|
| 430 |
-
)
|
| 431 |
-
clear_btn_b = gr.Button("Clear", variant="secondary")
|
| 432 |
-
reset_btn_b = gr.Button("Reset", variant="secondary")
|
| 433 |
-
|
| 434 |
gr.Markdown("Try some of the examples below ⬇️")
|
| 435 |
gr.Examples(
|
| 436 |
examples=examples,
|
| 437 |
-
inputs=[
|
| 438 |
-
outputs=[
|
| 439 |
-
examples_per_page=
|
| 440 |
fn=on_image_upload,
|
| 441 |
run_on_click=True
|
| 442 |
)
|
| 443 |
|
| 444 |
-
with gr.Column():
|
| 445 |
-
# Description
|
| 446 |
-
gr.Markdown(description_b)
|
| 447 |
-
|
| 448 |
if args.enable_everything_mode:
|
| 449 |
with gr.Tab("Everything mode") as tab_e:
|
| 450 |
# Images
|
| 451 |
with gr.Row(variant="panel"):
|
| 452 |
with gr.Column(scale=1):
|
| 453 |
-
|
| 454 |
-
|
| 455 |
with gr.Column(scale=1):
|
| 456 |
-
segm_img_e.render()
|
| 457 |
-
|
| 458 |
-
# Submit & Clear
|
| 459 |
-
with gr.Row():
|
| 460 |
-
with gr.Column():
|
| 461 |
with gr.Row():
|
| 462 |
with gr.Column():
|
| 463 |
-
segment_btn_e = gr.Button(
|
| 464 |
-
"Start", variant="primary"
|
| 465 |
-
)
|
| 466 |
reset_btn_e = gr.Button("Reset", variant="secondary")
|
|
|
|
| 467 |
|
|
|
|
|
|
|
|
|
|
| 468 |
gr.Markdown("Try some of the examples below ⬇️")
|
| 469 |
gr.Examples(
|
| 470 |
examples=examples,
|
| 471 |
-
inputs=[
|
| 472 |
-
examples_per_page=
|
| 473 |
)
|
| 474 |
|
| 475 |
-
with gr.Column():
|
| 476 |
-
# Description
|
| 477 |
-
gr.Markdown(description_e)
|
| 478 |
-
|
| 479 |
with gr.Row():
|
| 480 |
with gr.Column(scale=1):
|
| 481 |
gr.Markdown("<center><img src='https://visitor-badge.laobi.icu/badge?page_id=chongzhou/edgesam' alt='visitors'></center>")
|
| 482 |
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
)
|
| 488 |
-
clear_btn_p.click(clear, outputs=[cond_img_p, segm_img_p])
|
| 489 |
-
reset_btn_p.click(reset, outputs=[cond_img_p, segm_img_p])
|
| 490 |
tab_p.select(fn=reset_all, outputs=all_outputs)
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
)
|
| 497 |
-
clear_btn_b.click(clear, outputs=[cond_img_b, segm_img_b])
|
| 498 |
-
reset_btn_b.click(reset, outputs=[cond_img_b, segm_img_b])
|
| 499 |
tab_b.select(fn=reset_all, outputs=all_outputs)
|
| 500 |
|
| 501 |
if args.enable_everything_mode:
|
| 502 |
segment_btn_e.click(
|
| 503 |
-
segment_everything, inputs=[
|
| 504 |
)
|
| 505 |
-
reset_btn_e.click(reset, outputs=[
|
| 506 |
tab_e.select(fn=reset_all, outputs=all_outputs)
|
| 507 |
|
| 508 |
demo.queue()
|
|
|
|
| 49 |
# Description
|
| 50 |
title = "<center><strong><font size='8'>EdgeSAM<font></strong> <a href='https://github.com/chongzhou96/EdgeSAM'><font size='6'>[GitHub]</font></a> </center>"
|
| 51 |
|
| 52 |
+
description_p = """ # Instructions for point mode
|
| 53 |
|
| 54 |
1. Upload an image or click one of the provided examples.
|
| 55 |
2. Select the point type.
|
| 56 |
3. Click once or multiple times on the image to indicate the object of interest.
|
| 57 |
+
4. The Clear button clears all the points.
|
| 58 |
+
5. The Reset button resets both points and the image.
|
|
|
|
| 59 |
|
| 60 |
"""
|
| 61 |
|
| 62 |
+
description_b = """ # Instructions for box mode
|
| 63 |
|
| 64 |
1. Upload an image or click one of the provided examples.
|
| 65 |
2. Click twice on the image (diagonal points of the box).
|
| 66 |
+
3. The Clear button clears the box.
|
| 67 |
+
4. The Reset button resets both the box and the image.
|
|
|
|
| 68 |
|
| 69 |
"""
|
| 70 |
|
| 71 |
description_e = """ # Everything mode is NOT recommended.
|
| 72 |
|
| 73 |
Since EdgeSAM follows the same encoder-decoder architecture as SAM, the everything mode will infer the decoder 32x32=1024 times, which is inefficient, thus a longer processing time is expected.
|
| 74 |
+
|
| 75 |
+
1. Upload an image or click one of the provided examples.
|
| 76 |
+
2. Click Start to get the segmentation mask.
|
| 77 |
+
3. The Reset button resets the image and masks.
|
| 78 |
|
| 79 |
"""
|
| 80 |
|
|
|
|
| 97 |
["assets/16.jpeg"]
|
| 98 |
]
|
| 99 |
|
|
|
|
|
|
|
| 100 |
css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"
|
| 101 |
|
| 102 |
global_points = []
|
| 103 |
global_point_label = []
|
| 104 |
global_box = []
|
| 105 |
global_image = None
|
| 106 |
+
global_image_with_prompt = None
|
| 107 |
|
| 108 |
|
| 109 |
def reset():
|
|
|
|
| 111 |
global global_point_label
|
| 112 |
global global_box
|
| 113 |
global global_image
|
| 114 |
+
global global_image_with_prompt
|
| 115 |
global_points = []
|
| 116 |
global_point_label = []
|
| 117 |
global_box = []
|
| 118 |
global_image = None
|
| 119 |
+
global_image_with_prompt = None
|
| 120 |
+
return None
|
| 121 |
|
| 122 |
def reset_all():
|
| 123 |
global global_points
|
| 124 |
global global_point_label
|
| 125 |
global global_box
|
| 126 |
global global_image
|
| 127 |
+
global global_image_with_prompt
|
| 128 |
global_points = []
|
| 129 |
global_point_label = []
|
| 130 |
global_box = []
|
| 131 |
global_image = None
|
| 132 |
+
global_image_with_prompt = None
|
| 133 |
if args.enable_everything_mode:
|
| 134 |
+
return None, None, None
|
| 135 |
else:
|
| 136 |
+
return None, None
|
| 137 |
|
| 138 |
|
| 139 |
def clear():
|
|
|
|
| 141 |
global global_point_label
|
| 142 |
global global_box
|
| 143 |
global global_image
|
| 144 |
+
global global_image_with_prompt
|
| 145 |
global_points = []
|
| 146 |
global_point_label = []
|
| 147 |
global_box = []
|
| 148 |
+
global_image_with_prompt = copy.deepcopy(global_image)
|
| 149 |
+
return global_image
|
| 150 |
|
| 151 |
|
| 152 |
def on_image_upload(image, input_size=1024):
|
|
|
|
| 154 |
global global_point_label
|
| 155 |
global global_box
|
| 156 |
global global_image
|
| 157 |
+
global global_image_with_prompt
|
| 158 |
global_points = []
|
| 159 |
global_point_label = []
|
| 160 |
global_box = []
|
|
|
|
| 166 |
new_h = int(h * scale)
|
| 167 |
image = image.resize((new_w, new_h))
|
| 168 |
global_image = copy.deepcopy(image)
|
| 169 |
+
global_image_with_prompt = copy.deepcopy(image)
|
| 170 |
print("Image changed")
|
| 171 |
nd_image = np.array(global_image)
|
| 172 |
predictor.set_image(nd_image)
|
| 173 |
|
| 174 |
+
return image
|
| 175 |
|
| 176 |
|
| 177 |
def convert_box(xyxy):
|
|
|
|
| 185 |
xyxy[1][1] = max_y
|
| 186 |
return xyxy
|
| 187 |
|
| 188 |
+
def segment_with_points(
|
| 189 |
+
label,
|
| 190 |
+
evt: gr.SelectData,
|
| 191 |
+
input_size=1024,
|
| 192 |
+
better_quality=False,
|
| 193 |
+
withContours=True,
|
| 194 |
+
use_retina=True,
|
| 195 |
+
mask_random_color=False,
|
| 196 |
+
):
|
| 197 |
global global_points
|
| 198 |
global global_point_label
|
| 199 |
+
global global_image_with_prompt
|
| 200 |
|
| 201 |
x, y = evt.index[0], evt.index[1]
|
| 202 |
+
point_radius, point_color = 5, (97, 217, 54) if label == "Positive" else (237, 34, 13)
|
|
|
|
|
|
|
| 203 |
global_points.append([x, y])
|
| 204 |
global_point_label.append(1 if label == "Positive" else 0)
|
| 205 |
|
| 206 |
print(f'global_points: {global_points}')
|
| 207 |
print(f'global_point_label: {global_point_label}')
|
| 208 |
|
| 209 |
+
draw = ImageDraw.Draw(global_image_with_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
draw.ellipse(
|
| 211 |
[(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],
|
| 212 |
fill=point_color,
|
| 213 |
)
|
| 214 |
+
image = global_image_with_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
global_points_np = np.array(global_points)
|
| 217 |
global_point_label_np = np.array(global_point_label)
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
num_multimask_outputs = 4
|
| 220 |
|
| 221 |
masks, scores, logits = predictor.predict(
|
|
|
|
| 246 |
withContours=withContours,
|
| 247 |
)
|
| 248 |
|
| 249 |
+
return seg
|
| 250 |
|
| 251 |
|
| 252 |
def segment_with_box(
|
| 253 |
+
evt: gr.SelectData,
|
| 254 |
input_size=1024,
|
| 255 |
better_quality=False,
|
| 256 |
withContours=True,
|
|
|
|
| 258 |
mask_random_color=False,
|
| 259 |
):
|
| 260 |
global global_box
|
| 261 |
+
global global_image
|
| 262 |
+
global global_image_with_prompt
|
| 263 |
|
| 264 |
+
x, y = evt.index[0], evt.index[1]
|
| 265 |
+
point_radius, point_color, box_outline = 5, (97, 217, 54), 5
|
| 266 |
+
box_color = (0, 255, 0)
|
| 267 |
|
| 268 |
+
if len(global_box) == 0:
|
| 269 |
+
global_box.append([x, y])
|
| 270 |
+
elif len(global_box) == 1:
|
| 271 |
+
global_box.append([x, y])
|
| 272 |
+
elif len(global_box) == 2:
|
| 273 |
+
global_image_with_prompt = copy.deepcopy(global_image)
|
| 274 |
+
global_box = [[x, y]]
|
| 275 |
|
| 276 |
+
print(f'global_box: {global_box}')
|
| 277 |
+
|
| 278 |
+
draw = ImageDraw.Draw(global_image_with_prompt)
|
| 279 |
+
draw.ellipse(
|
| 280 |
+
[(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],
|
| 281 |
+
fill=point_color,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
)
|
| 283 |
+
image = global_image_with_prompt
|
| 284 |
|
| 285 |
+
if len(global_box) == 2:
|
| 286 |
+
global_box = convert_box(global_box)
|
| 287 |
+
xy = (global_box[0][0], global_box[0][1], global_box[1][0], global_box[1][1])
|
| 288 |
+
draw.rectangle(
|
| 289 |
+
xy,
|
| 290 |
+
outline=box_color,
|
| 291 |
+
width=box_outline
|
| 292 |
+
)
|
| 293 |
+
|
| 294 |
+
global_box_np = np.array(global_box)
|
| 295 |
+
|
| 296 |
+
masks, scores, logits = predictor.predict(
|
| 297 |
+
box=global_box_np,
|
| 298 |
+
num_multimask_outputs=1,
|
| 299 |
+
)
|
| 300 |
+
annotations = masks
|
| 301 |
+
|
| 302 |
+
seg = fast_process(
|
| 303 |
+
annotations=annotations,
|
| 304 |
+
image=image,
|
| 305 |
+
device=device,
|
| 306 |
+
scale=(1024 // input_size),
|
| 307 |
+
better_quality=better_quality,
|
| 308 |
+
mask_random_color=mask_random_color,
|
| 309 |
+
bbox=None,
|
| 310 |
+
use_retina=use_retina,
|
| 311 |
+
withContours=withContours,
|
| 312 |
+
)
|
| 313 |
+
return seg
|
| 314 |
+
return image
|
| 315 |
|
| 316 |
|
| 317 |
def segment_everything(
|
|
|
|
| 340 |
return seg
|
| 341 |
|
| 342 |
|
| 343 |
+
img_p = gr.Image(label="Input with points", type="pil")
|
| 344 |
+
img_b = gr.Image(label="Input with box", type="pil")
|
| 345 |
+
img_e = gr.Image(label="Input (everything)", type="pil")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
if args.enable_everything_mode:
|
| 348 |
+
all_outputs = [img_p, img_b, img_e]
|
| 349 |
else:
|
| 350 |
+
all_outputs = [img_p, img_b]
|
| 351 |
|
| 352 |
with gr.Blocks(css=css, title="EdgeSAM") as demo:
|
| 353 |
|
|
|
|
| 360 |
# Images
|
| 361 |
with gr.Row(variant="panel"):
|
| 362 |
with gr.Column(scale=1):
|
| 363 |
+
img_p.render()
|
|
|
|
| 364 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
with gr.Row():
|
| 366 |
add_or_remove = gr.Radio(
|
| 367 |
["Positive", "Negative"],
|
|
|
|
| 370 |
)
|
| 371 |
|
| 372 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
| 373 |
clear_btn_p = gr.Button("Clear", variant="secondary")
|
| 374 |
reset_btn_p = gr.Button("Reset", variant="secondary")
|
| 375 |
+
with gr.Row():
|
| 376 |
+
gr.Markdown(description_p)
|
| 377 |
|
| 378 |
+
with gr.Row():
|
| 379 |
+
with gr.Column():
|
| 380 |
gr.Markdown("Try some of the examples below ⬇️")
|
| 381 |
gr.Examples(
|
| 382 |
examples=examples,
|
| 383 |
+
inputs=[img_p],
|
| 384 |
+
outputs=[img_p],
|
| 385 |
+
examples_per_page=8,
|
| 386 |
fn=on_image_upload,
|
| 387 |
run_on_click=True
|
| 388 |
)
|
| 389 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
with gr.Tab("Box mode") as tab_b:
|
| 391 |
# Images
|
| 392 |
with gr.Row(variant="panel"):
|
| 393 |
with gr.Column(scale=1):
|
| 394 |
+
img_b.render()
|
| 395 |
+
with gr.Row():
|
| 396 |
+
with gr.Column():
|
| 397 |
+
clear_btn_b = gr.Button("Clear", variant="secondary")
|
| 398 |
+
reset_btn_b = gr.Button("Reset", variant="secondary")
|
| 399 |
+
gr.Markdown(description_b)
|
| 400 |
|
|
|
|
| 401 |
with gr.Row():
|
| 402 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
gr.Markdown("Try some of the examples below ⬇️")
|
| 404 |
gr.Examples(
|
| 405 |
examples=examples,
|
| 406 |
+
inputs=[img_b],
|
| 407 |
+
outputs=[img_b],
|
| 408 |
+
examples_per_page=8,
|
| 409 |
fn=on_image_upload,
|
| 410 |
run_on_click=True
|
| 411 |
)
|
| 412 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
if args.enable_everything_mode:
|
| 414 |
with gr.Tab("Everything mode") as tab_e:
|
| 415 |
# Images
|
| 416 |
with gr.Row(variant="panel"):
|
| 417 |
with gr.Column(scale=1):
|
| 418 |
+
img_e.render()
|
|
|
|
| 419 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
with gr.Row():
|
| 421 |
with gr.Column():
|
| 422 |
+
segment_btn_e = gr.Button("Start", variant="primary")
|
|
|
|
|
|
|
| 423 |
reset_btn_e = gr.Button("Reset", variant="secondary")
|
| 424 |
+
gr.Markdown(description_e)
|
| 425 |
|
| 426 |
+
# Submit & Clear
|
| 427 |
+
with gr.Row():
|
| 428 |
+
with gr.Column():
|
| 429 |
gr.Markdown("Try some of the examples below ⬇️")
|
| 430 |
gr.Examples(
|
| 431 |
examples=examples,
|
| 432 |
+
inputs=[img_e],
|
| 433 |
+
examples_per_page=8,
|
| 434 |
)
|
| 435 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
with gr.Row():
|
| 437 |
with gr.Column(scale=1):
|
| 438 |
gr.Markdown("<center><img src='https://visitor-badge.laobi.icu/badge?page_id=chongzhou/edgesam' alt='visitors'></center>")
|
| 439 |
|
| 440 |
+
img_p.upload(on_image_upload, img_p, [img_p])
|
| 441 |
+
img_p.select(segment_with_points, [add_or_remove], img_p)
|
| 442 |
+
|
| 443 |
+
clear_btn_p.click(clear, outputs=[img_p])
|
| 444 |
+
reset_btn_p.click(reset, outputs=[img_p])
|
|
|
|
|
|
|
| 445 |
tab_p.select(fn=reset_all, outputs=all_outputs)
|
| 446 |
|
| 447 |
+
img_b.upload(on_image_upload, img_b, [img_b])
|
| 448 |
+
img_b.select(segment_with_box, outputs=[img_b])
|
| 449 |
+
|
| 450 |
+
clear_btn_b.click(clear, outputs=[img_b])
|
| 451 |
+
reset_btn_b.click(reset, outputs=[img_b])
|
|
|
|
|
|
|
| 452 |
tab_b.select(fn=reset_all, outputs=all_outputs)
|
| 453 |
|
| 454 |
if args.enable_everything_mode:
|
| 455 |
segment_btn_e.click(
|
| 456 |
+
segment_everything, inputs=[img_e], outputs=img_e
|
| 457 |
)
|
| 458 |
+
reset_btn_e.click(reset, outputs=[img_e])
|
| 459 |
tab_e.select(fn=reset_all, outputs=all_outputs)
|
| 460 |
|
| 461 |
demo.queue()
|
assets/box-instructions.mov
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:b6d85a91a61f63f5636f42832fb8751b36cdce92cefefdcef05816f1c931c00a
|
| 3 |
-
size 8007816
|
|
|
|
|
|
|
|
|
|
|
|
assets/point-instructions.mov
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:fbe4b4f30563fa059e600bb0dff599c2666821e7a5e5f799a0ad2d82f6895ebd
|
| 3 |
-
size 20135690
|
|
|
|
|
|
|
|
|
|
|
|