copilot-swe-agent[bot] matheus-rech commited on
Commit
b364546
·
1 Parent(s): 5b3bea2

Lower SAM3 detection thresholds to fix empty masks issue

Browse files

Co-authored-by: matheus-rech <71096630+matheus-rech@users.noreply.github.com>

Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -98,15 +98,15 @@ else:
98
  processor = None
99
 
100
  @spaces.GPU(duration=60)
101
- def run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5):
102
  """
103
  Run SAM 3 inference - matching official implementation from akhaliq/sam3.
104
 
105
  Args:
106
  pil_image: PIL Image to segment
107
  prompt_text: Text prompt for segmentation
108
- threshold: Detection threshold (higher = fewer detections)
109
- mask_threshold: Mask threshold (higher = sharper masks)
110
 
111
  Returns:
112
  results dict with 'masks' and 'scores' as numpy arrays or lists, or None if failed
@@ -444,7 +444,8 @@ def process_medical_image(image_file, prompt_text, modality, window_type, return
444
  pil_image = Image.fromarray(img_uint8.astype(np.uint8))
445
 
446
  # Run SAM 3 Inference - using helper function matching official implementation
447
- results = run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5)
 
448
 
449
  if results is None:
450
  return None
@@ -639,7 +640,8 @@ def process_medical_image_enhanced(image_file, prompt_text, modality, window_typ
639
  pil_image = enhancer.enhance(contrast)
640
 
641
  # Run SAM 3 Inference - using helper function matching official implementation
642
- results = run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5)
 
643
 
644
  if results is None:
645
  return None
@@ -966,7 +968,8 @@ def process_with_point_prompt(image_file, point_x, point_y, modality, window_typ
966
  prompt_text = f"segment region at point"
967
 
968
  # Process with SAM 3 - using helper function
969
- results = run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5)
 
970
 
971
  final_mask = None
972
  if results and 'masks' in results and results['masks'] is not None:
@@ -1062,7 +1065,8 @@ def process_with_box_prompt(image_file, x1, y1, x2, y2, modality, window_type, c
1062
  prompt_text = "segment region in bounding box"
1063
 
1064
  # Process with SAM 3 - using helper function
1065
- results = run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5)
 
1066
 
1067
  final_mask = None
1068
  if results and 'masks' in results and results['masks'] is not None:
@@ -1146,7 +1150,8 @@ def process_multi_mask(image_file, prompt_text, modality, window_type, num_masks
1146
  prompt_text = "brain"
1147
 
1148
  # Process with SAM 3 - using helper function
1149
- sam_results = run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5)
 
1150
 
1151
  results = []
1152
  mask_info = []
@@ -1553,7 +1558,8 @@ def automatic_mask_generator(image_file, modality, window_type,
1553
 
1554
  try:
1555
  # Process with SAM 3 - using helper function
1556
- sam_results = run_sam3_inference(pil_image, prompt, threshold=0.5, mask_threshold=0.5)
 
1557
 
1558
  if sam_results and 'masks' in sam_results and sam_results['masks'] is not None:
1559
  masks = sam_results['masks'] # List of mask tensors
@@ -1732,7 +1738,8 @@ def process_with_advanced_transforms(image_file, prompt_text, modality, window_t
1732
  prompt_text = "brain"
1733
 
1734
  # Process with SAM 3 - using helper function
1735
- results = run_sam3_inference(pil_image, prompt_text, threshold=0.5, mask_threshold=0.5)
 
1736
 
1737
  final_mask = None
1738
  if results and 'masks' in results and results['masks'] is not None:
 
98
  processor = None
99
 
100
  @spaces.GPU(duration=60)
101
+ def run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0):
102
  """
103
  Run SAM 3 inference - matching official implementation from akhaliq/sam3.
104
 
105
  Args:
106
  pil_image: PIL Image to segment
107
  prompt_text: Text prompt for segmentation
108
+ threshold: Detection threshold (higher = fewer detections, default 0.1 for medical images)
109
+ mask_threshold: Mask threshold (higher = sharper masks, default 0.0 for medical images)
110
 
111
  Returns:
112
  results dict with 'masks' and 'scores' as numpy arrays or lists, or None if failed
 
444
  pil_image = Image.fromarray(img_uint8.astype(np.uint8))
445
 
446
  # Run SAM 3 Inference - using helper function matching official implementation
447
+ # Lower thresholds for medical images to ensure detections are not filtered out
448
+ results = run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0)
449
 
450
  if results is None:
451
  return None
 
640
  pil_image = enhancer.enhance(contrast)
641
 
642
  # Run SAM 3 Inference - using helper function matching official implementation
643
+ # Lower thresholds for medical images to ensure detections are not filtered out
644
+ results = run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0)
645
 
646
  if results is None:
647
  return None
 
968
  prompt_text = f"segment region at point"
969
 
970
  # Process with SAM 3 - using helper function
971
+ # Lower thresholds for medical images to ensure detections are not filtered out
972
+ results = run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0)
973
 
974
  final_mask = None
975
  if results and 'masks' in results and results['masks'] is not None:
 
1065
  prompt_text = "segment region in bounding box"
1066
 
1067
  # Process with SAM 3 - using helper function
1068
+ # Lower thresholds for medical images to ensure detections are not filtered out
1069
+ results = run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0)
1070
 
1071
  final_mask = None
1072
  if results and 'masks' in results and results['masks'] is not None:
 
1150
  prompt_text = "brain"
1151
 
1152
  # Process with SAM 3 - using helper function
1153
+ # Lower thresholds for medical images to ensure detections are not filtered out
1154
+ sam_results = run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0)
1155
 
1156
  results = []
1157
  mask_info = []
 
1558
 
1559
  try:
1560
  # Process with SAM 3 - using helper function
1561
+ # Lower thresholds for medical images to ensure detections are not filtered out
1562
+ sam_results = run_sam3_inference(pil_image, prompt, threshold=0.1, mask_threshold=0.0)
1563
 
1564
  if sam_results and 'masks' in sam_results and sam_results['masks'] is not None:
1565
  masks = sam_results['masks'] # List of mask tensors
 
1738
  prompt_text = "brain"
1739
 
1740
  # Process with SAM 3 - using helper function
1741
+ # Lower thresholds for medical images to ensure detections are not filtered out
1742
+ results = run_sam3_inference(pil_image, prompt_text, threshold=0.1, mask_threshold=0.0)
1743
 
1744
  final_mask = None
1745
  if results and 'masks' in results and results['masks'] is not None: