Update sam_module.py
Browse files- sam_module.py +6 -4
sam_module.py
CHANGED
|
@@ -437,7 +437,7 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 437 |
dynamic_threshold = max(0.15, mask_max * 0.3) # Moderater Threshold
|
| 438 |
print(f" ✅ Gute Maske. Threshold={dynamic_threshold:.3f}")
|
| 439 |
|
| 440 |
-
# Binärmaske erstellen (
|
| 441 |
mask_array = (mask_np > dynamic_threshold).astype(np.uint8) * 255
|
| 442 |
|
| 443 |
# Fallback bei leerer Maske, der höchste Wert ist 0 also schwarz
|
|
@@ -451,7 +451,7 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 451 |
fb_y1 = int(y1 * scale_y)
|
| 452 |
fb_x2 = int(x2 * scale_x)
|
| 453 |
fb_y2 = int(y2 * scale_y)
|
| 454 |
-
cv2.rectangle(mask_array, (fb_x1, fb_y1), (fb_x2, fb_y2), 255, -1)
|
| 455 |
|
| 456 |
# Damit wird die Rohmaske für die UI-Anzeige gespeichert
|
| 457 |
raw_mask_array = mask_array.copy()
|
|
@@ -463,7 +463,7 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 463 |
print(f" mask_array - Shape: {mask_array.shape}")
|
| 464 |
print(f" mask_array - dtype: {mask_array.dtype}")
|
| 465 |
|
| 466 |
-
# 1.
|
| 467 |
labeled_array, num_features = ndimage.label(mask_array)
|
| 468 |
if num_features > 1:
|
| 469 |
sizes = ndimage.sum(mask_array, labeled_array, range(1, num_features + 1))
|
|
@@ -507,12 +507,14 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 507 |
|
| 508 |
# ============================================================
|
| 509 |
# SCHRITT 1: Originalbild sichern
|
|
|
|
|
|
|
| 510 |
# ============================================================
|
| 511 |
original_image = image
|
| 512 |
print(f"💾 Originalbild gesichert: {original_image.size}")
|
| 513 |
|
| 514 |
# ============================================================
|
| 515 |
-
# SCHRITT 2: Crop = BBox × 2.5 (ERHÖHT für mehr Kontext)
|
| 516 |
# ============================================================
|
| 517 |
print("✂️ SCHRITT 2: ERSTELLE QUADRATISCHEN AUSSCHNITT (BBox × 2.5)")
|
| 518 |
|
|
|
|
| 437 |
dynamic_threshold = max(0.15, mask_max * 0.3) # Moderater Threshold
|
| 438 |
print(f" ✅ Gute Maske. Threshold={dynamic_threshold:.3f}")
|
| 439 |
|
| 440 |
+
# Binärmaske erstellen (512x512)
|
| 441 |
mask_array = (mask_np > dynamic_threshold).astype(np.uint8) * 255
|
| 442 |
|
| 443 |
# Fallback bei leerer Maske, der höchste Wert ist 0 also schwarz
|
|
|
|
| 451 |
fb_y1 = int(y1 * scale_y)
|
| 452 |
fb_x2 = int(x2 * scale_x)
|
| 453 |
fb_y2 = int(y2 * scale_y)
|
| 454 |
+
cv2.rectangle(mask_array, (fb_x1, fb_y1), (fb_x2, fb_y2), 255, -1) #weiße Rechteckbox
|
| 455 |
|
| 456 |
# Damit wird die Rohmaske für die UI-Anzeige gespeichert
|
| 457 |
raw_mask_array = mask_array.copy()
|
|
|
|
| 463 |
print(f" mask_array - Shape: {mask_array.shape}")
|
| 464 |
print(f" mask_array - dtype: {mask_array.dtype}")
|
| 465 |
|
| 466 |
+
# 1. Findet und behält nur die größte zusammenhängende Komponente der Maske
|
| 467 |
labeled_array, num_features = ndimage.label(mask_array)
|
| 468 |
if num_features > 1:
|
| 469 |
sizes = ndimage.sum(mask_array, labeled_array, range(1, num_features + 1))
|
|
|
|
| 507 |
|
| 508 |
# ============================================================
|
| 509 |
# SCHRITT 1: Originalbild sichern
|
| 510 |
+
# Andere Vorgehensweise da SAM bei kleinen Köpfen sonst keine Chance hat!
|
| 511 |
+
# Bild ausschneiden auf eine vergrößerte quadratische Box - Crops
|
| 512 |
# ============================================================
|
| 513 |
original_image = image
|
| 514 |
print(f"💾 Originalbild gesichert: {original_image.size}")
|
| 515 |
|
| 516 |
# ============================================================
|
| 517 |
+
# SCHRITT 2: Crop = BBox × 2.5 (ERHÖHT für mehr Kontext)
|
| 518 |
# ============================================================
|
| 519 |
print("✂️ SCHRITT 2: ERSTELLE QUADRATISCHEN AUSSCHNITT (BBox × 2.5)")
|
| 520 |
|