V4ldeLund commited on
Commit
1be8e93
·
verified ·
1 Parent(s): 50e1346

Always return mask image; apply mask to anomaly map to reduce background bleed

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -93,17 +93,21 @@ def infer(ref_files, test_file, use_sam3, sam_prompt):
93
  model.fit(ref_paths, n_ref=len(ref_paths))
94
  image, amap, score = model.predict(test_path)
95
 
96
- fg_mask_img = None
 
97
  if segmenter is not None:
98
  # Compute full-resolution mask for visualization and for suppressing background
99
  fg_mask = segmenter.get_object_mask(image)
 
 
100
  if fg_mask.shape != amap.shape:
101
- fg_mask = cv2.resize(
102
  fg_mask.astype(np.float32),
103
  (amap.shape[1], amap.shape[0]),
104
  interpolation=cv2.INTER_NEAREST,
105
  ) > 0.5
106
- amap = amap * fg_mask # zero-out background to reduce false positives
 
107
  fg_mask_img = Image.fromarray((fg_mask.astype(np.uint8) * 255))
108
 
109
  overlay = _make_overlay(image, amap)
 
93
  model.fit(ref_paths, n_ref=len(ref_paths))
94
  image, amap, score = model.predict(test_path)
95
 
96
+ # Default empty mask for UI (in case SAM3 is off or fails)
97
+ fg_mask_img = Image.fromarray(np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8))
98
  if segmenter is not None:
99
  # Compute full-resolution mask for visualization and for suppressing background
100
  fg_mask = segmenter.get_object_mask(image)
101
+ # Resize mask to anomaly map resolution
102
+ fg_mask_resized = fg_mask
103
  if fg_mask.shape != amap.shape:
104
+ fg_mask_resized = cv2.resize(
105
  fg_mask.astype(np.float32),
106
  (amap.shape[1], amap.shape[0]),
107
  interpolation=cv2.INTER_NEAREST,
108
  ) > 0.5
109
+ # Apply mask to anomaly map to suppress background leakage
110
+ amap = amap * fg_mask_resized
111
  fg_mask_img = Image.fromarray((fg_mask.astype(np.uint8) * 255))
112
 
113
  overlay = _make_overlay(image, amap)