hmgill commited on
Commit
9402dd3
·
verified ·
1 Parent(s): 869a58c

Update cellemetry/services/sam.py

Browse files
Files changed (1) hide show
  1. cellemetry/services/sam.py +29 -1
cellemetry/services/sam.py CHANGED
@@ -93,7 +93,35 @@ def execute_segmentation(deps: AnalysisDeps, request: ComponentRequest) -> str:
93
  print(f"[Engine] Inference took {time.time() - t_inf:.2f}s")
94
 
95
  # 5. REMOVED: Morphological Filtering (Solidity/Circularity)
96
- # We now trust the SAM model and the Agent's bounding box completely.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  # 6. NMS (Keep this to remove duplicate detections on the same object)
99
  pred_boxes = results["boxes"]
 
93
  print(f"[Engine] Inference took {time.time() - t_inf:.2f}s")
94
 
95
  # 5. REMOVED: Morphological Filtering (Solidity/Circularity)
96
+
97
+ # Morphology filtering
98
+ keep_indices_morph = []
99
+ for i, mask_tensor in enumerate(results["masks"]):
100
+ mask_np = mask_tensor.cpu().numpy()
101
+ mask_np = np.squeeze(mask_np).astype(int)
102
+
103
+ if mask_np.ndim != 2:
104
+ keep_indices_morph.append(False)
105
+ continue
106
+
107
+ props = regionprops(mask_np)
108
+ if not props:
109
+ keep_indices_morph.append(False)
110
+ continue
111
+
112
+ prop = props[0]
113
+ perimeter = prop.perimeter
114
+ circularity = (4 * np.pi * prop.area) / (perimeter ** 2) if perimeter > 0 else 0
115
+
116
+ is_solid = prop.solidity > MIN_SOLIDITY
117
+ is_round_enough = circularity > MIN_CIRCULARITY
118
+ keep_indices_morph.append(is_solid and is_round_enough)
119
+
120
+ if any(keep_indices_morph):
121
+ keep_indices_tensor = torch.tensor(keep_indices_morph, device=results["masks"].device)
122
+ before_count = len(results["masks"])
123
+ results = _filter_results(results, keep_indices_tensor)
124
+ print(f"[Filter] Morphology: Dropped {before_count - len(results['masks'])} debris-like objects.")
125
 
126
  # 6. NMS (Keep this to remove duplicate detections on the same object)
127
  pred_boxes = results["boxes"]