Spaces:
Sleeping
Sleeping
Update sam_module.py
Browse files- sam_module.py +72 -16
sam_module.py
CHANGED
|
@@ -547,25 +547,81 @@ def create_sam_mask(self, image, bbox_coords, mode):
|
|
| 547 |
crop_y1 = max(0, crop_y1)
|
| 548 |
crop_x2 = min(original_image.width, crop_x2)
|
| 549 |
crop_y2 = min(original_image.height, crop_y2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
|
|
|
| 554 |
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
|
| 567 |
-
|
| 568 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
|
| 570 |
# Bild ausschneiden- 2,5 mal so groß und quadratisch wie BBox
|
| 571 |
cropped_image = original_image.crop((crop_x1, crop_y1, crop_x2, crop_y2))
|
|
|
|
| 547 |
crop_y1 = max(0, crop_y1)
|
| 548 |
crop_x2 = min(original_image.width, crop_x2)
|
| 549 |
crop_y2 = min(original_image.height, crop_y2)
|
| 550 |
+
|
| 551 |
+
|
| 552 |
+
# ITERATIVE ANPASSUNG für bessere Crop-Größe
|
| 553 |
+
max_iterations = 3
|
| 554 |
+
print(f" 🔄 Iterative Crop-Anpassung (max. {max_iterations} Versuche)")
|
| 555 |
+
|
| 556 |
+
for iteration in range(max_iterations):
|
| 557 |
+
actual_crop_width = crop_x2 - crop_x1
|
| 558 |
+
actual_crop_height = crop_y2 - crop_y1
|
| 559 |
|
| 560 |
+
# Prüfen ob Crop groß genug ist
|
| 561 |
+
if actual_crop_width >= crop_size and actual_crop_height >= crop_size:
|
| 562 |
+
print(f" ✅ Crop-Größe OK nach {iteration} Iteration(en): {actual_crop_width}×{actual_crop_height} px")
|
| 563 |
+
break
|
| 564 |
|
| 565 |
+
print(f" 🔄 Iteration {iteration+1}: Crop zu klein ({actual_crop_width}×{actual_crop_height})")
|
| 566 |
+
|
| 567 |
+
# BREITE anpassen (falls nötig)
|
| 568 |
+
if actual_crop_width < crop_size:
|
| 569 |
+
if crop_x1 == 0: # Am linken Rand
|
| 570 |
+
crop_x2 = min(original_image.width, crop_x1 + crop_size)
|
| 571 |
+
print(f" ← Breite angepasst (linker Rand): crop_x2 = {crop_x2}")
|
| 572 |
+
elif crop_x2 == original_image.width: # Am rechten Rand
|
| 573 |
+
crop_x1 = max(0, crop_x2 - crop_size)
|
| 574 |
+
print(f" → Breite angepasst (rechter Rand): crop_x1 = {crop_x1}")
|
| 575 |
+
else:
|
| 576 |
+
# Nicht am Rand - zentriert erweitern
|
| 577 |
+
missing_width = crop_size - actual_crop_width
|
| 578 |
+
expand_left = missing_width // 2
|
| 579 |
+
expand_right = missing_width - expand_left
|
| 580 |
+
|
| 581 |
+
crop_x1 = max(0, crop_x1 - expand_left)
|
| 582 |
+
crop_x2 = min(original_image.width, crop_x2 + expand_right)
|
| 583 |
+
print(f" ↔ Zentriert erweitert um {missing_width}px")
|
| 584 |
|
| 585 |
+
# HÖHE anpassen (falls nötig)
|
| 586 |
+
if actual_crop_height < crop_size:
|
| 587 |
+
if crop_y1 == 0: # Am oberen Rand
|
| 588 |
+
crop_y2 = min(original_image.height, crop_y1 + crop_size)
|
| 589 |
+
print(f" ↑ Höhe angepasst (oberer Rand): crop_y2 = {crop_y2}")
|
| 590 |
+
elif crop_y2 == original_image.height: # Am unteren Rand
|
| 591 |
+
crop_y1 = max(0, crop_y2 - crop_size)
|
| 592 |
+
print(f" ↓ Höhe angepasst (unterer Rand): crop_y1 = {crop_y1}")
|
| 593 |
+
else:
|
| 594 |
+
# Nicht am Rand - zentriert erweitern
|
| 595 |
+
missing_height = crop_size - actual_crop_height
|
| 596 |
+
expand_top = missing_height // 2
|
| 597 |
+
expand_bottom = missing_height - expand_top
|
| 598 |
+
|
| 599 |
+
crop_y1 = max(0, crop_y1 - expand_top)
|
| 600 |
+
crop_y2 = min(original_image.height, crop_y2 + expand_bottom)
|
| 601 |
+
print(f" ↕ Zentriert erweitert um {missing_height}px")
|
| 602 |
+
|
| 603 |
+
# Sicherstellen, dass innerhalb der Bildgrenzen
|
| 604 |
+
crop_x1 = max(0, crop_x1)
|
| 605 |
+
crop_y1 = max(0, crop_y1)
|
| 606 |
+
crop_x2 = min(original_image.width, crop_x2)
|
| 607 |
+
crop_y2 = min(original_image.height, crop_y2)
|
| 608 |
+
|
| 609 |
+
# Letzte Iteration erreicht?
|
| 610 |
+
if iteration == max_iterations - 1:
|
| 611 |
+
actual_crop_width = crop_x2 - crop_x1
|
| 612 |
+
actual_crop_height = crop_y2 - crop_y1
|
| 613 |
+
print(f" ⚠️ Max. Iterationen erreicht. Finaler Crop: {actual_crop_width}×{actual_crop_height} px")
|
| 614 |
+
|
| 615 |
+
# Warnung wenn immer noch zu klein
|
| 616 |
+
if actual_crop_width < crop_size or actual_crop_height < crop_size:
|
| 617 |
+
min_acceptable = int(bbox_max_dim * 1.8) # Mindestens 1.8× BBox
|
| 618 |
+
if actual_crop_width < min_acceptable or actual_crop_height < min_acceptable:
|
| 619 |
+
print(f" 🚨 KRITISCH: Crop immer noch zu klein ({actual_crop_width}×{actual_crop_height})")
|
| 620 |
+
print(f" 🚨 SAM könnte Probleme haben!")
|
| 621 |
+
|
| 622 |
+
print(f" 🔲 Finaler Crop-Bereich: [{crop_x1}, {crop_y1}, {crop_x2}, {crop_y2}]")
|
| 623 |
+
print(f" 📏 Finale Crop-Größe: {crop_x2-crop_x1} × {crop_y2-crop_y1} px")
|
| 624 |
+
|
| 625 |
|
| 626 |
# Bild ausschneiden- 2,5 mal so groß und quadratisch wie BBox
|
| 627 |
cropped_image = original_image.crop((crop_x1, crop_y1, crop_x2, crop_y2))
|