Astridkraft commited on
Commit
d916b1d
·
verified ·
1 Parent(s): 07ebae2

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +27 -9
controlnet_module.py CHANGED
@@ -644,12 +644,14 @@ class ControlNetProcessor:
644
  print(f" 🔄 Konvertiert zu Float32 im Bereich [0, 1]. Neuer min/max: {mask_array.min():.3f}/{mask_array.max():.3f}")
645
  print(f" ⚙️ Gamma-Korrektur (0.85) angewendet. Neuer min/max: {mask_array.min():.3f}/{mask_array.max():.3f}")
646
  print(f" ✅ Zurück zu uint8 [0, 255]. Final min/max: {mask_array.min()}/{mask_array.max()}")
647
-
 
648
  elif mode == "environment_change":
649
  print("🌳 ENVIRONMENT-CHANGE POSTPROCESSING")
650
 
651
  # Für environment_change: Originalbildgröße beibehalten
652
  if image.size != original_image.size:
 
653
  temp_mask = Image.fromarray(mask_array).convert("L")
654
  temp_mask = temp_mask.resize(original_image.size, Image.Resampling.NEAREST)
655
  mask_array = np.array(temp_mask)
@@ -657,16 +659,32 @@ class ControlNetProcessor:
657
  mask_array = 255 - mask_array # Invertiere Maske
658
  print(" ✅ Maske invertiert (Person schwarz, Hintergrund weiß)")
659
 
 
 
 
 
 
660
  # Morphologische Operationen für saubere Umgebung
661
- kernel = np.ones((5,5), np.uint8)
662
- mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_CLOSE, kernel)
663
  print(" ✅ MORPH_CLOSE für zusammenhängende Umgebung")
664
-
665
- # QUALITÄTSKONTROLLE
666
- white_pixels = np.sum(mask_array > 127)
667
- total_pixels = mask_array.size
668
- white_ratio = white_pixels / total_pixels * 100
669
-
 
 
 
 
 
 
 
 
 
 
 
670
  print("-" * 60)
671
  print("📊 MASKEN-STATISTIK (FINAL)")
672
  print(f" Weiße Pixel (Veränderungsbereich): {white_pixels:,} ({white_ratio:.1f}%)")
 
644
  print(f" 🔄 Konvertiert zu Float32 im Bereich [0, 1]. Neuer min/max: {mask_array.min():.3f}/{mask_array.max():.3f}")
645
  print(f" ⚙️ Gamma-Korrektur (0.85) angewendet. Neuer min/max: {mask_array.min():.3f}/{mask_array.max():.3f}")
646
  print(f" ✅ Zurück zu uint8 [0, 255]. Final min/max: {mask_array.min()}/{mask_array.max()}")
647
+
648
+
649
  elif mode == "environment_change":
650
  print("🌳 ENVIRONMENT-CHANGE POSTPROCESSING")
651
 
652
  # Für environment_change: Originalbildgröße beibehalten
653
  if image.size != original_image.size:
654
+ print(f" ⚠️ Bildgröße angepasst: {image.size} → {original_image.size}")
655
  temp_mask = Image.fromarray(mask_array).convert("L")
656
  temp_mask = temp_mask.resize(original_image.size, Image.Resampling.NEAREST)
657
  mask_array = np.array(temp_mask)
 
659
  mask_array = 255 - mask_array # Invertiere Maske
660
  print(" ✅ Maske invertiert (Person schwarz, Hintergrund weiß)")
661
 
662
+ # Weiße Punkte in der Person (schwarz) entfernen
663
+ kernel_open = np.ones((3,3), np.uint8)
664
+ mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_OPEN, kernel_open, iterations=3)
665
+ print(" ✅ MORPH_OPEN entfernt weiße Punkte in der Person")
666
+
667
  # Morphologische Operationen für saubere Umgebung
668
+ kernel_close = np.ones((5,5), np.uint8)
669
+ mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_CLOSE, kernel_close)
670
  print(" ✅ MORPH_CLOSE für zusammenhängende Umgebung")
671
+
672
+ # Weiche Ränder für bessere Integration der Person
673
+ mask_array = cv2.GaussianBlur(mask_array, (9, 9), 2.0)
674
+ print(" ✅ Gaussian Blur für weiche Übergänge")
675
+
676
+ # Gamma-Korrektur für präzisere Ränder
677
+ mask_array = mask_array.astype(np.float32) / 255.0
678
+ mask_array = np.clip(mask_array, 0.0, 1.0)
679
+ mask_array = mask_array ** 0.85
680
+ mask_array = (mask_array * 255).astype(np.uint8)
681
+ print(" ✅ Gamma-Korrektur (0.85) gegen milchige Ränder")
682
+
683
+ # QUALITÄTSKONTROLLE
684
+ white_pixels = np.sum(mask_array > 127)
685
+ total_pixels = mask_array.size
686
+ white_ratio = white_pixels / total_pixels * 100
687
+
688
  print("-" * 60)
689
  print("📊 MASKEN-STATISTIK (FINAL)")
690
  print(f" Weiße Pixel (Veränderungsbereich): {white_pixels:,} ({white_ratio:.1f}%)")