Update sam_module.py
Browse files- sam_module.py +9 -4
sam_module.py
CHANGED
|
@@ -208,7 +208,7 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 208 |
mask_np = resized_mask.sigmoid().cpu().numpy()
|
| 209 |
print(f" 🔄 Beste Maske skaliert auf 512×512 für ControlNet")
|
| 210 |
|
| 211 |
-
|
| 212 |
# ============================================================
|
| 213 |
# DYNAMISCHER THRESHOLD
|
| 214 |
# SAM gibt nur Wahrscheinlichkeiten aus!
|
|
@@ -225,8 +225,9 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 225 |
dynamic_threshold = max(0.15, mask_max * 0.3) # Moderater Threshold
|
| 226 |
print(f" ✅ Gute Maske. Threshold={dynamic_threshold:.3f}")
|
| 227 |
|
| 228 |
-
# Binärmaske erstellen
|
| 229 |
mask_array = (mask_np > dynamic_threshold).astype(np.uint8) * 255
|
|
|
|
| 230 |
|
| 231 |
# Fallback bei leerer Maske, der höchste Wert ist 0 also schwarz
|
| 232 |
if mask_array.max() == 0:
|
|
@@ -240,6 +241,9 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 240 |
fb_x2 = int(x2 * scale_x)
|
| 241 |
fb_y2 = int(y2 * scale_y)
|
| 242 |
cv2.rectangle(mask_array, (fb_x1, fb_y1), (fb_x2, fb_y2), 255, -1)
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
# FOCUS_CHANGE POSTPROCESSING (angepasst für 512x512)
|
| 245 |
print("🔧 FOCUS_CHANGE POSTPROCESSING (auf 512×512)")
|
|
@@ -263,7 +267,7 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 263 |
kernel_dilate = np.ones((15, 15), np.uint8)
|
| 264 |
mask_array = cv2.dilate(mask_array, kernel_dilate, iterations=1)
|
| 265 |
|
| 266 |
-
# 3. Weiche Übergänge
|
| 267 |
mask_array = cv2.GaussianBlur(mask_array, (9, 9), 2.0)
|
| 268 |
|
| 269 |
# 4. Gamma-Korrektur
|
|
@@ -274,7 +278,8 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 274 |
|
| 275 |
# 5. Auf Originalgröße für Rückgabe (falls benötigt)
|
| 276 |
mask_512 = Image.fromarray(mask_array).convert("L")
|
| 277 |
-
raw_mask = mask_512.copy() # Rohmaske = finale Maske bei focus_change
|
|
|
|
| 278 |
|
| 279 |
# Finale Maske für ControlNet ist 512x512
|
| 280 |
mask = mask_512
|
|
|
|
| 208 |
mask_np = resized_mask.sigmoid().cpu().numpy()
|
| 209 |
print(f" 🔄 Beste Maske skaliert auf 512×512 für ControlNet")
|
| 210 |
|
| 211 |
+
|
| 212 |
# ============================================================
|
| 213 |
# DYNAMISCHER THRESHOLD
|
| 214 |
# SAM gibt nur Wahrscheinlichkeiten aus!
|
|
|
|
| 225 |
dynamic_threshold = max(0.15, mask_max * 0.3) # Moderater Threshold
|
| 226 |
print(f" ✅ Gute Maske. Threshold={dynamic_threshold:.3f}")
|
| 227 |
|
| 228 |
+
# Binärmaske erstellen (256x256)
|
| 229 |
mask_array = (mask_np > dynamic_threshold).astype(np.uint8) * 255
|
| 230 |
+
|
| 231 |
|
| 232 |
# Fallback bei leerer Maske, der höchste Wert ist 0 also schwarz
|
| 233 |
if mask_array.max() == 0:
|
|
|
|
| 241 |
fb_x2 = int(x2 * scale_x)
|
| 242 |
fb_y2 = int(y2 * scale_y)
|
| 243 |
cv2.rectangle(mask_array, (fb_x1, fb_y1), (fb_x2, fb_y2), 255, -1)
|
| 244 |
+
|
| 245 |
+
# Damit wird die Rohmaske für die UI-Anzeige gespeichert
|
| 246 |
+
raw_mask_array = mask_array.copy()
|
| 247 |
|
| 248 |
# FOCUS_CHANGE POSTPROCESSING (angepasst für 512x512)
|
| 249 |
print("🔧 FOCUS_CHANGE POSTPROCESSING (auf 512×512)")
|
|
|
|
| 267 |
kernel_dilate = np.ones((15, 15), np.uint8)
|
| 268 |
mask_array = cv2.dilate(mask_array, kernel_dilate, iterations=1)
|
| 269 |
|
| 270 |
+
# 3. Weiche Übergänge mittlerer Blur für natürliche Übergänge
|
| 271 |
mask_array = cv2.GaussianBlur(mask_array, (9, 9), 2.0)
|
| 272 |
|
| 273 |
# 4. Gamma-Korrektur
|
|
|
|
| 278 |
|
| 279 |
# 5. Auf Originalgröße für Rückgabe (falls benötigt)
|
| 280 |
mask_512 = Image.fromarray(mask_array).convert("L")
|
| 281 |
+
#raw_mask = mask_512.copy() # Rohmaske = finale Maske bei focus_change
|
| 282 |
+
raw_mask = Image.fromarray(raw_mask_array).convert("L")
|
| 283 |
|
| 284 |
# Finale Maske für ControlNet ist 512x512
|
| 285 |
mask = mask_512
|