Sm0kyWu commited on
Commit
beb20dc
ยท
verified ยท
1 Parent(s): dfe14ad

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -14
app.py CHANGED
@@ -51,7 +51,7 @@ def button_clickable(selected_points):
51
  else:
52
  return gr.Button.update(interactive=False)
53
 
54
- def run_sam(image, predictor, selected_points):
55
  """
56
  ่ฐƒ็”จ SAM ๆจกๅž‹่ฟ›่กŒๅˆ†ๅ‰ฒใ€‚
57
  """
@@ -70,11 +70,10 @@ def run_sam(image, predictor, selected_points):
70
  best_mask = masks[0].astype(np.uint8)
71
  # dilate
72
  if len(input_points) > 1:
73
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
74
  best_mask = cv2.dilate(best_mask, kernel, iterations=1)
75
  best_mask = cv2.erode(best_mask, kernel, iterations=1)
76
- visible_mask = 255 * best_mask
77
- return visible_mask
78
 
79
  def apply_mask_overlay(image, mask):
80
  """
@@ -93,8 +92,8 @@ def segment_and_overlay(image, points, sam_predictor):
93
  """
94
  ่ฐƒ็”จ run_sam ่Žทๅพ— mask๏ผŒ็„ถๅŽๅ ๅŠ ๆ˜พ็คบๅˆ†ๅ‰ฒ็ป“ๆžœใ€‚
95
  """
96
- visible_mask = run_sam(image, sam_predictor, points)
97
- overlaid = apply_mask_overlay(image, visible_mask)
98
  return overlaid, visible_mask
99
 
100
 
@@ -375,15 +374,39 @@ def delete_selected_occlusion(image, visible_points, occlusion_points, selected_
375
  updated_text, vis_dropdown, occ_dropdown = update_all_points(visible_points, occlusion_points)
376
  return updated_image, visible_points, occlusion_points, updated_text, vis_dropdown, occ_dropdown
377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
 
379
  with gr.Blocks(delete_cache=(600, 600)) as demo:
 
 
 
 
 
 
 
 
380
  gr.Markdown("""
381
  ## 3D Amodal Reconstruction with [Amodal3R](https://sm0kywu.github.io/Amodal3R/)
382
- * Upload an image and click "Generate" to create a 3D asset.
383
- * Target object selection. Multiple point prompts are supported until you get the ideal visible area.
384
- * Occluders selection, this can be done by squential point prompts. You can choose "all occ", then all the other areas except the target object will be treated as occluders.
385
- * Different random seeds can be tried in "Generation Settings", if you think the results are not ideal.
386
- * If the reconstruction 3D asset is satisfactory, you can extract the GLB file and download it.
 
 
387
  """)
388
 
389
  # ๅฎšไน‰ๅ„็Šถๆ€ๅ˜้‡
@@ -393,6 +416,8 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
393
  original_image = gr.State(value=None)
394
  visibility_mask = gr.State(value=None)
395
  occlusion_mask = gr.State(value=None)
 
 
396
 
397
 
398
  with gr.Row():
@@ -430,14 +455,14 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
430
  add_vis_mask = gr.Button("Add Mask")
431
  with gr.Row():
432
  render_vis_mask = gr.Button("Render Mask")
433
- undo_vis_mask = gr.Button("Undo Occlusion Mask")
434
- occlusion_mask = gr.Image(label='Occlusion Mask', interactive=False, height=300)
435
  with gr.Row():
436
  gen_occ_mask = gr.Button("Generate Mask")
437
  add_occ_mask = gr.Button("Add Mask")
438
  with gr.Row():
439
  render_occ_mask = gr.Button("Render Mask")
440
- undo_occ_mask = gr.Button("Undo Occlusion Mask")
441
  #
442
 
443
  # ---------------------------
@@ -505,6 +530,31 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
505
  inputs=[original_image, visible_points_state, predictor],
506
  outputs=[visible_mask, visibility_mask]
507
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
 
509
 
510
  # ๅฏๅŠจ Gradio App
 
51
  else:
52
  return gr.Button.update(interactive=False)
53
 
54
+ def run_sam(predictor, selected_points):
55
  """
56
  ่ฐƒ็”จ SAM ๆจกๅž‹่ฟ›่กŒๅˆ†ๅ‰ฒใ€‚
57
  """
 
70
  best_mask = masks[0].astype(np.uint8)
71
  # dilate
72
  if len(input_points) > 1:
73
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
74
  best_mask = cv2.dilate(best_mask, kernel, iterations=1)
75
  best_mask = cv2.erode(best_mask, kernel, iterations=1)
76
+ return best_mask
 
77
 
78
  def apply_mask_overlay(image, mask):
79
  """
 
92
  """
93
  ่ฐƒ็”จ run_sam ่Žทๅพ— mask๏ผŒ็„ถๅŽๅ ๅŠ ๆ˜พ็คบๅˆ†ๅ‰ฒ็ป“ๆžœใ€‚
94
  """
95
+ visible_mask = run_sam(sam_predictor, points)
96
+ overlaid = apply_mask_overlay(image, visible_mask * 255)
97
  return overlaid, visible_mask
98
 
99
 
 
374
  updated_text, vis_dropdown, occ_dropdown = update_all_points(visible_points, occlusion_points)
375
  return updated_image, visible_points, occlusion_points, updated_text, vis_dropdown, occ_dropdown
376
 
377
+ def add_mask(mask, mask_list):
378
+ mask_list.append(mask)
379
+ return mask_list
380
+
381
+ def vis_mask(image, mask_list):
382
+ updated_image = image.copy()
383
+ # combine all the mask:
384
+ combined_mask = np.zeros_like(updated_image)
385
+ for mask in mask_list:
386
+ combined_mask = cv2.bitwise_or(combined_mask, mask)
387
+ # overlay the mask on the image
388
+ updated_image = apply_mask_overlay(updated_image, combined_mask)
389
+ return updated_image
390
+
391
 
392
  with gr.Blocks(delete_cache=(600, 600)) as demo:
393
+ # gr.Markdown("""
394
+ # ## 3D Amodal Reconstruction with [Amodal3R](https://sm0kywu.github.io/Amodal3R/)
395
+ # * Upload an image and click "Generate" to create a 3D asset.
396
+ # * Target object selection. Multiple point prompts are supported until you get the ideal visible area.
397
+ # * Occluders selection, this can be done by squential point prompts. You can choose "all occ", then all the other areas except the target object will be treated as occluders.
398
+ # * Different random seeds can be tried in "Generation Settings", if you think the results are not ideal.
399
+ # * If the reconstruction 3D asset is satisfactory, you can extract the GLB file and download it.
400
+ # """)
401
  gr.Markdown("""
402
  ## 3D Amodal Reconstruction with [Amodal3R](https://sm0kywu.github.io/Amodal3R/)
403
+ * Step 1 - Generate Visibility Mask and Occlusion Mask.
404
+ * Please wait for a few seconds after uploading the image. The 2D segmenter is getting ready.
405
+ * Add the point prompts to indicate the target object and occluders separately.
406
+ * "Render Point", see the position of the point to be added.
407
+ * "Add Point", the point will be added to the list.
408
+ * "Generate mask", see the segmented area corresponding to current point list.
409
+ * "Add mask", current mask will be added for 3D amodal completion.
410
  """)
411
 
412
  # ๅฎšไน‰ๅ„็Šถๆ€ๅ˜้‡
 
416
  original_image = gr.State(value=None)
417
  visibility_mask = gr.State(value=None)
418
  occlusion_mask = gr.State(value=None)
419
+ visibility_mask_list = gr.State(value=[])
420
+ occlusion_mask_list = gr.State(value=[])
421
 
422
 
423
  with gr.Row():
 
455
  add_vis_mask = gr.Button("Add Mask")
456
  with gr.Row():
457
  render_vis_mask = gr.Button("Render Mask")
458
+ undo_vis_mask = gr.Button("Undo Last Mask")
459
+ occluded_mask = gr.Image(label='Occlusion Mask', interactive=False, height=300)
460
  with gr.Row():
461
  gen_occ_mask = gr.Button("Generate Mask")
462
  add_occ_mask = gr.Button("Add Mask")
463
  with gr.Row():
464
  render_occ_mask = gr.Button("Render Mask")
465
+ undo_occ_mask = gr.Button("Undo Last Mask")
466
  #
467
 
468
  # ---------------------------
 
530
  inputs=[original_image, visible_points_state, predictor],
531
  outputs=[visible_mask, visibility_mask]
532
  )
533
+ add_vis_mask.click(
534
+ add_mask,
535
+ inputs=[visibility_mask, visibility_mask_list],
536
+ outputs=[visibility_mask_list]
537
+ )
538
+ render_vis_mask.click(
539
+ vis_mask,
540
+ inputs=[original_image, visibility_mask_list],
541
+ outputs=[input_image]
542
+ )
543
+ gen_occ_mask.click(
544
+ segment_and_overlay,
545
+ inputs=[original_image, occlusion_points_state, predictor],
546
+ outputs=[occluded_mask, occlusion_mask]
547
+ )
548
+ add_occ_mask.click(
549
+ add_mask,
550
+ inputs=[occlusion_mask, occlusion_mask_list],
551
+ outputs=[occlusion_mask_list]
552
+ )
553
+ render_occ_mask.click(
554
+ vis_mask,
555
+ inputs=[original_image, occlusion_mask_list],
556
+ outputs=[input_image]
557
+ )
558
 
559
 
560
  # ๅฏๅŠจ Gradio App