Astridkraft commited on
Commit
3cca410
·
verified ·
1 Parent(s): db71054

Update sam_module.py

Browse files
Files changed (1) hide show
  1. sam_module.py +18 -2
sam_module.py CHANGED
@@ -62,11 +62,26 @@ def create_sam_mask(self, image, bbox_coords, mode):
62
  print(" Führe Vorhersage durch...")
63
  outputs = self.sam_model(**inputs)
64
  print(f"✅ Vorhersage abgeschlossen")
65
- print(f" Anzahl der Vorhersagemasken: {outputs.pred_masks.shape[2]}")
66
 
67
  num_masks = outputs.pred_masks.shape[2]
68
  print(f" SAM lieferte {num_masks} verschiedene Masken")
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  bbox_center = ((x1 + x2) // 2, (y1 + y2) // 2)
71
  bbox_area = (x2 - x1) * (y2 - y1)
72
  print(f" Erwartetes BBox-Zentrum: {bbox_center}")
@@ -535,4 +550,5 @@ def create_sam_mask(self, image, bbox_coords, mode):
535
  print(f" ⚠️ Fallback-Maske angepasst: {fallback_mask.size} → {original_image.size}")
536
  fallback_mask = fallback_mask.resize(original_image.size, Image.Resampling.NEAREST)
537
 
538
- return fallback_mask, fallback_mask
 
 
62
  print(" Führe Vorhersage durch...")
63
  outputs = self.sam_model(**inputs)
64
  print(f"✅ Vorhersage abgeschlossen")
65
+ print(f" Anzahl der Vorhersagemasken: {outputs.pred_masks.shape[2]}")
66
 
67
  num_masks = outputs.pred_masks.shape[2]
68
  print(f" SAM lieferte {num_masks} verschiedene Masken")
69
 
70
+ #Sammlung der Masken
71
+ all_masks = []
72
+
73
+ for i in range(num_masks):
74
+ single_mask = outputs.pred_masks[:, :, i, :, :]
75
+ resized_mask = F.interpolate(
76
+ single_mask,
77
+ size=(image.height, image.width),
78
+ mode='bilinear',
79
+ align_corners=False
80
+ ).squeeze()
81
+
82
+ mask_np = resized_mask.sigmoid().cpu().numpy()
83
+ all_masks.append(mask_np)
84
+
85
  bbox_center = ((x1 + x2) // 2, (y1 + y2) // 2)
86
  bbox_area = (x2 - x1) * (y2 - y1)
87
  print(f" Erwartetes BBox-Zentrum: {bbox_center}")
 
550
  print(f" ⚠️ Fallback-Maske angepasst: {fallback_mask.size} → {original_image.size}")
551
  fallback_mask = fallback_mask.resize(original_image.size, Image.Resampling.NEAREST)
552
 
553
+ return fallback_mask, fallback_mask
554
+