hanszhu commited on
Commit
e5964c3
·
1 Parent(s): 4771e7c

feat(medsam): UI shows polygons only; optional raw masks via hidden flag to avoid UI crashes

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -643,7 +643,7 @@ def analyze(image):
643
  return {"error": "Internal error in analyze"}
644
 
645
 
646
- def analyze_with_medsam(base_result, image):
647
  try:
648
  log("analyze_with_medsam: start")
649
  if not isinstance(base_result, dict):
@@ -674,11 +674,15 @@ def analyze_with_medsam(base_result, image):
674
  m = _medsam.segment_with_box(bbox)
675
  if m is None or not isinstance(m.get('mask'), np.ndarray):
676
  continue
677
- segmentations.append({
678
- "mask": m['mask'].astype(np.uint8).tolist(),
679
  "confidence": float(m.get('confidence', 1.0)),
680
- "method": m.get("method", "medsam_box_auto")
681
- })
 
 
 
 
682
  masks_for_overlay.append(m)
683
 
684
  W, H = pil_img.size
@@ -740,6 +744,7 @@ with gr.Blocks(
740
  height=400,
741
  elem_id="image-input"
742
  )
 
743
 
744
  # Analyze button (single)
745
  analyze_btn = gr.Button(
@@ -774,7 +779,7 @@ with gr.Blocks(
774
  # Automatic overlay generation step for medical images
775
  analyze_event.then(
776
  fn=analyze_with_medsam,
777
- inputs=[result_output, image_input],
778
  outputs=[result_output, overlay_output],
779
  )
780
 
 
643
  return {"error": "Internal error in analyze"}
644
 
645
 
646
+ def analyze_with_medsam(base_result, image, include_raw_masks=False):
647
  try:
648
  log("analyze_with_medsam: start")
649
  if not isinstance(base_result, dict):
 
674
  m = _medsam.segment_with_box(bbox)
675
  if m is None or not isinstance(m.get('mask'), np.ndarray):
676
  continue
677
+ mask_np = m['mask'].astype(np.uint8)
678
+ seg_entry = {
679
  "confidence": float(m.get('confidence', 1.0)),
680
+ "method": m.get("method", "medsam_box_auto"),
681
+ "polygons": _mask_to_polygons(mask_np)
682
+ }
683
+ if include_raw_masks:
684
+ seg_entry["mask"] = mask_np.tolist()
685
+ segmentations.append(seg_entry)
686
  masks_for_overlay.append(m)
687
 
688
  W, H = pil_img.size
 
744
  height=400,
745
  elem_id="image-input"
746
  )
747
+ include_raw_masks_cb = gr.Checkbox(value=False, visible=False, elem_id="include-raw-masks")
748
 
749
  # Analyze button (single)
750
  analyze_btn = gr.Button(
 
779
  # Automatic overlay generation step for medical images
780
  analyze_event.then(
781
  fn=analyze_with_medsam,
782
+ inputs=[result_output, image_input, include_raw_masks_cb],
783
  outputs=[result_output, overlay_output],
784
  )
785