Sm0kyWu commited on
Commit
f8e489c
·
verified ·
1 Parent(s): 2ead80c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -20
app.py CHANGED
@@ -129,8 +129,9 @@ def reset_image(predictor, img):
129
  - 返回原图
130
  """
131
  predictor.set_image(img)
132
- # 返回predictor,原始图像
133
- return predictor, img
 
134
 
135
  def button_clickable(selected_points):
136
  if len(selected_points) > 0:
@@ -372,8 +373,8 @@ def draw_points_on_image(image, point, point_type):
372
  """在图像上绘制所有点,points 为 [(x, y, point_type), ...]"""
373
  image_with_points = image.copy()
374
  x, y = point
375
- color = (0, 0, 255) if point_type == "visible" else (0, 255, 0)
376
- cv2.circle(image_with_points, (int(x), int(y)), radius=5, color=color, thickness=-1)
377
  return image_with_points
378
 
379
 
@@ -409,6 +410,70 @@ def delete_point(point_type, visible_points, occlusion_points):
409
  return visible_points, occlusion_points
410
 
411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
  with gr.Blocks(delete_cache=(600, 600)) as demo:
413
  gr.Markdown("""
414
  ## 3D Amodal Reconstruction with [Amodal3R](https://sm0kywu.github.io/Amodal3R/)
@@ -423,6 +488,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
423
  predictor = gr.State(value=get_sam_predictor())
424
  visible_points_state = gr.State(value=[])
425
  occlusion_points_state = gr.State(value=[])
 
426
 
427
 
428
  with gr.Row():
@@ -436,38 +502,83 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
436
  with gr.Row():
437
  see_button = gr.Button("See")
438
  add_button = gr.Button("Add")
439
- delete_button = gr.Button("Delete")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
  with gr.Column():
441
- # 显示 SAM 分割结果(带 overlay)—— 使用 AnnotatedImage 显示更直观
442
- sam_image = gr.Image(label='SAM Generated Mask', interactive=False, height=300)
443
-
444
-
445
- # 会话启动与结束
446
- demo.load(start_session)
447
- demo.unload(end_session)
448
 
449
- # 上传图像时:重置 predictor 并将原图赋值给 original_image、preprocessed_image、selected_points 以及 output_mask
 
 
450
  input_image.upload(
451
  reset_image,
452
  [predictor, input_image],
453
- [predictor, sam_image]
454
  )
455
- # 如果点击see按钮,应该在input图片上生成对应的点,
456
  see_button.click(
457
  see_point,
458
  inputs=[input_image, x_input, y_input, point_type],
459
  outputs=[input_image]
460
  )
461
- # 如果点击add按钮,应该将对应的点添加到visible_points_state中
462
  add_button.click(
463
  add_point,
464
  inputs=[x_input, y_input, point_type, visible_points_state, occlusion_points_state],
465
  outputs=[visible_points_state, occlusion_points_state]
466
  )
467
- delete_button.click(
468
- delete_point,
469
- inputs=[point_type, visible_points_state, occlusion_points_state],
470
- outputs=[visible_points_state, occlusion_points_state]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471
  )
472
 
473
 
 
129
  - 返回原图
130
  """
131
  predictor.set_image(img)
132
+ original_img = img.copy()
133
+ # 返回predictor,visible occlusion mask初始化, 原始图像
134
+ return predictor, img, img, original_img
135
 
136
  def button_clickable(selected_points):
137
  if len(selected_points) > 0:
 
373
  """在图像上绘制所有点,points 为 [(x, y, point_type), ...]"""
374
  image_with_points = image.copy()
375
  x, y = point
376
+ color = (255, 0, 0) if point_type == "visible" else (0, 255, 0)
377
+ cv2.circle(image_with_points, (int(x), int(y)), radius=10, color=color, thickness=-1)
378
  return image_with_points
379
 
380
 
 
410
  return visible_points, occlusion_points
411
 
412
 
413
+ def clear_all_points(image):
414
+ """
415
+ 清除所有点:返回原图、空的 visible 和 occlusion 列表,
416
+ 以及更新后的点文本信息和空下拉菜单列表。
417
+ """
418
+ updated_image = image.copy()
419
+ empty_list = []
420
+ text = "Visible Points: []\nOcclusion Points: []"
421
+ return updated_image, empty_list, empty_list, text, [], []
422
+
423
+ def see_visible_points(image, visible_points):
424
+ """
425
+ 在图像上绘制所有 visible 点(红色)。
426
+ """
427
+ updated_image = image.copy()
428
+ for p in visible_points:
429
+ cv2.circle(updated_image, (int(p[0]), int(p[1])), radius=10, color=(255, 0, 0), thickness=-1)
430
+ return updated_image
431
+
432
+ def see_occlusion_points(image, occlusion_points):
433
+ """
434
+ 在图像上绘制所有 occlusion 点(绿色)。
435
+ """
436
+ updated_image = image.copy()
437
+ for p in occlusion_points:
438
+ cv2.circle(updated_image, (int(p[0]), int(p[1])), radius=10, color=(0, 255, 0), thickness=-1)
439
+ return updated_image
440
+
441
+ def update_all_points(visible_points, occlusion_points):
442
+ """
443
+ 返回一个文本描述当前两个点列表,以及对应的下拉菜单选项(字符串格式)。
444
+ """
445
+ text = f"Visible Points: {visible_points}\nOcclusion Points: {occlusion_points}"
446
+ visible_dropdown = [f"({p[0]}, {p[1]})" for p in visible_points]
447
+ occlusion_dropdown = [f"({p[0]}, {p[1]})" for p in occlusion_points]
448
+ return text, visible_dropdown, occlusion_dropdown
449
+
450
+ def delete_selected_visible(image, visible_points, occlusion_points, selected_index):
451
+ """
452
+ 删除 visible 点列表中指定索引的点,并更新图像和显示信息。
453
+ """
454
+ if selected_index is not None and 0 <= selected_index < len(visible_points):
455
+ visible_points.pop(selected_index)
456
+ updated_image = image.copy()
457
+ # 重新绘制所有 visible 点(红色)
458
+ for p in visible_points:
459
+ cv2.circle(updated_image, (int(p[0]), int(p[1])), radius=10, color=(255, 0, 0), thickness=-1)
460
+ updated_text, vis_dropdown, occ_dropdown = update_all_points(visible_points, occlusion_points)
461
+ return updated_image, visible_points, occlusion_points, updated_text, vis_dropdown, occ_dropdown
462
+
463
+ def delete_selected_occlusion(image, visible_points, occlusion_points, selected_index):
464
+ """
465
+ 删除 occlusion 点列表中指定索引的点,并更新图像和显示信息。
466
+ """
467
+ if selected_index is not None and 0 <= selected_index < len(occlusion_points):
468
+ occlusion_points.pop(selected_index)
469
+ updated_image = image.copy()
470
+ # 重新绘制所有 occlusion 点(绿色)
471
+ for p in occlusion_points:
472
+ cv2.circle(updated_image, (int(p[0]), int(p[1])), radius=10, color=(0, 255, 0), thickness=-1)
473
+ updated_text, vis_dropdown, occ_dropdown = update_all_points(visible_points, occlusion_points)
474
+ return updated_image, visible_points, occlusion_points, updated_text, vis_dropdown, occ_dropdown
475
+
476
+
477
  with gr.Blocks(delete_cache=(600, 600)) as demo:
478
  gr.Markdown("""
479
  ## 3D Amodal Reconstruction with [Amodal3R](https://sm0kywu.github.io/Amodal3R/)
 
488
  predictor = gr.State(value=get_sam_predictor())
489
  visible_points_state = gr.State(value=[])
490
  occlusion_points_state = gr.State(value=[])
491
+ original_image = gr.State(value=None)
492
 
493
 
494
  with gr.Row():
 
502
  with gr.Row():
503
  see_button = gr.Button("See")
504
  add_button = gr.Button("Add")
505
+ with gr.Row():
506
+ # 新增按钮:Clear、分别查看 visible/occlusion
507
+ clear_button = gr.Button("Clear Points")
508
+ see_visible_button = gr.Button("See Visible Points")
509
+ see_occlusion_button = gr.Button("See Occlusion Points")
510
+ with gr.Row():
511
+ # 新增文本框实时显示点列表
512
+ points_text = gr.Textbox(label="Points List", interactive=False)
513
+ with gr.Row():
514
+ # 新增下拉菜单,用户可选择需要删除的点
515
+ visible_points_dropdown = gr.Dropdown(label="Select Visible Point to Delete", choices=[], interactive=True)
516
+ occlusion_points_dropdown = gr.Dropdown(label="Select Occlusion Point to Delete", choices=[], interactive=True)
517
+ with gr.Row():
518
+ delete_visible_button = gr.Button("Delete Selected Visible")
519
+ delete_occlusion_button = gr.Button("Delete Selected Occlusion")
520
  with gr.Column():
521
+ # 用于显示 SAM 分割结果
522
+ visible_mask = gr.Image(label='Visible Mask', interactive=False, height=300)
523
+ occlusion_mask = gr.Image(label='Occlusion Mask', interactive=False, height=300)
 
 
 
 
524
 
525
+ # ---------------------------
526
+ # 原有交互逻辑(略)
527
+ # ---------------------------
528
  input_image.upload(
529
  reset_image,
530
  [predictor, input_image],
531
+ [predictor, visible_mask, occlusion_mask, original_image]
532
  )
 
533
  see_button.click(
534
  see_point,
535
  inputs=[input_image, x_input, y_input, point_type],
536
  outputs=[input_image]
537
  )
 
538
  add_button.click(
539
  add_point,
540
  inputs=[x_input, y_input, point_type, visible_points_state, occlusion_points_state],
541
  outputs=[visible_points_state, occlusion_points_state]
542
  )
543
+
544
+ # ---------------------------
545
+ # 新增的交互逻辑
546
+ # ---------------------------
547
+ clear_button.click(
548
+ clear_all_points,
549
+ inputs=[original_image],
550
+ outputs=[input_image, visible_points_state, occlusion_points_state, points_text, visible_points_dropdown, occlusion_points_dropdown]
551
+ )
552
+ see_visible_button.click(
553
+ see_visible_points,
554
+ inputs=[input_image, visible_points_state],
555
+ outputs=input_image
556
+ )
557
+ see_occlusion_button.click(
558
+ see_occlusion_points,
559
+ inputs=[input_image, occlusion_points_state],
560
+ outputs=input_image
561
+ )
562
+ # 当 visible_points_state 或 occlusion_points_state 变化时,更新文本框和下拉菜单
563
+ visible_points_state.change(
564
+ update_all_points,
565
+ inputs=[visible_points_state, occlusion_points_state],
566
+ outputs=[points_text, visible_points_dropdown, occlusion_points_dropdown]
567
+ )
568
+ occlusion_points_state.change(
569
+ update_all_points,
570
+ inputs=[visible_points_state, occlusion_points_state],
571
+ outputs=[points_text, visible_points_dropdown, occlusion_points_dropdown]
572
+ )
573
+ delete_visible_button.click(
574
+ delete_selected_visible,
575
+ inputs=[input_image, visible_points_state, occlusion_points_state, visible_points_dropdown],
576
+ outputs=[input_image, visible_points_state, occlusion_points_state, points_text, visible_points_dropdown, occlusion_points_dropdown]
577
+ )
578
+ delete_occlusion_button.click(
579
+ delete_selected_occlusion,
580
+ inputs=[input_image, visible_points_state, occlusion_points_state, occlusion_points_dropdown],
581
+ outputs=[input_image, visible_points_state, occlusion_points_state, points_text, visible_points_dropdown, occlusion_points_dropdown]
582
  )
583
 
584