Astridkraft commited on
Commit
a850477
·
verified ·
1 Parent(s): 13851f6

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +54 -17
controlnet_module.py CHANGED
@@ -1,7 +1,7 @@
1
  import torch
2
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
3
  from controlnet_aux import OpenposeDetector
4
- from PIL import Image, ImageFilter
5
  import random
6
  import cv2
7
  import numpy as np
@@ -160,6 +160,14 @@ class ControlNetProcessor:
160
  cropped_image = image.crop((crop_x1, crop_y1, crop_x2, crop_y2))
161
  print(f" ✅ Ausschnitt erstellt: {cropped_image.size}")
162
 
 
 
 
 
 
 
 
 
163
  # BBox-Koordinaten relativ zum Ausschnitt neu berechnen
164
  rel_x1 = x1 - crop_x1
165
  rel_y1 = y1 - crop_y1
@@ -173,7 +181,7 @@ class ControlNetProcessor:
173
  image = cropped_image
174
  x1, y1, x2, y2 = rel_x1, rel_y1, rel_x2, rel_y2
175
 
176
- print(" 🔄 SAM wird auf Ausschnitt (nicht Vollbild) ausgeführt")
177
 
178
  # ============================================================
179
  # GEMEINSAME SAM-LOGIK FÜR ALLE MODI
@@ -224,24 +232,53 @@ class ControlNetProcessor:
224
  # 6. In NumPy konvertieren und Schwellenwert anwenden
225
  mask_np = final_mask.sigmoid().cpu().numpy()
226
  print(f" Nach Sigmoid und CPU: {mask_np.shape}, Wertebereich: [{mask_np.min():.3f}, {mask_np.max():.3f}]")
227
-
228
-
229
- # VERWENDEN SIE:
230
- # 1. Finde den maximalen Wert in mask_np
231
  max_val = mask_np.max()
232
  print(f" 🔍 Maximaler SAM-Konfidenzwert: {max_val:.3f}")
233
-
234
- # 2. Setze Threshold auf 80% des Maximalwerts (oder einen anderen Prozentsatz)
235
- dynamic_threshold = max_val * 0.8
236
- print(f" 🎯 Dynamischer Threshold: {dynamic_threshold:.3f} (80% von Maximum)")
237
-
238
- # 3. Anwenden
239
- mask_array = (mask_np > dynamic_threshold).astype(np.uint8) * 255
240
 
241
- #mask_array = (mask_np > 0.9).astype(np.uint8) * 255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  unique_vals = np.unique(mask_array)
243
- print(f" Nach Threshold (0.9): {mask_array.shape}, Unique Werte: {unique_vals}")
244
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  # 7. BEIDE MASKEN ERSTELLEN (vor Nachbearbeitung)
246
  original_mask_array = mask_array.copy() # Person weiß (255), Hintergrund schwarz (0)
247
  inverted_mask_array = 255 - mask_array # Person schwarz (0), Hintergrund weiß (255)
@@ -330,7 +367,7 @@ class ControlNetProcessor:
330
 
331
  if num_features > 0:
332
  sizes = ndimage.sum(mask_array, labeled_array, range(1, num_features + 1))
333
- print(f" Größen der weißen Komponenten auf AUSSCHNITT: {sizes}")
334
 
335
  if num_features > 1:
336
  # WICHTIG: Für Gesicht nehmen wir die GRÖSSTE Komponente im AUSSCHNITT
 
1
  import torch
2
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
3
  from controlnet_aux import OpenposeDetector
4
+ from PIL import Image, ImageFilter, ImageEnhance # NEU: ImageEnhance für Kontrast
5
  import random
6
  import cv2
7
  import numpy as np
 
160
  cropped_image = image.crop((crop_x1, crop_y1, crop_x2, crop_y2))
161
  print(f" ✅ Ausschnitt erstellt: {cropped_image.size}")
162
 
163
+ # ============================================================
164
+ # NEU: KONTRASTVERSTÄRKUNG FÜR BESSERE SAM-ERKENNUNG
165
+ # ============================================================
166
+ print(" 🔍 Wende Kontrastverstärkung an für bessere Segmentierung...")
167
+ contrast_enhancer = ImageEnhance.Contrast(cropped_image)
168
+ cropped_image = contrast_enhancer.enhance(1.5) # 50% mehr Kontrast
169
+ print(" ✅ Kontrast um 50% erhöht")
170
+
171
  # BBox-Koordinaten relativ zum Ausschnitt neu berechnen
172
  rel_x1 = x1 - crop_x1
173
  rel_y1 = y1 - crop_y1
 
181
  image = cropped_image
182
  x1, y1, x2, y2 = rel_x1, rel_y1, rel_x2, rel_y2
183
 
184
+ print(" 🔄 SAM wird auf kontrastverstärktem Ausschnitt (nicht Vollbild) ausgeführt")
185
 
186
  # ============================================================
187
  # GEMEINSAME SAM-LOGIK FÜR ALLE MODI
 
232
  # 6. In NumPy konvertieren und Schwellenwert anwenden
233
  mask_np = final_mask.sigmoid().cpu().numpy()
234
  print(f" Nach Sigmoid und CPU: {mask_np.shape}, Wertebereich: [{mask_np.min():.3f}, {mask_np.max():.3f}]")
235
+
236
+ # ============================================================
237
+ # KRITISCH: DYNAMISCHER THRESHOLD FÜR UNSICHERE SAM-VORHERSAGEN
238
+ # ============================================================
239
  max_val = mask_np.max()
240
  print(f" 🔍 Maximaler SAM-Konfidenzwert: {max_val:.3f}")
 
 
 
 
 
 
 
241
 
242
+ # NEUE LOGIK: Unterscheidung basierend auf SAM-Konfidenz
243
+ if max_val < 0.6:
244
+ # Fall: SAM ist unsicher (wie in Ihrem Log: max_val=0.505)
245
+ # Verwende festen, niedrigen Threshold
246
+ dynamic_threshold = 0.2 # Sehr niedrig für unsichere Vorhersagen
247
+ print(f" ⚠️ SAM ist unsicher (max_val={max_val:.3f} < 0.6)")
248
+ print(f" 🎯 Verwende festen niedrigen Threshold: {dynamic_threshold:.3f}")
249
+ else:
250
+ # Fall: SAM ist sicher
251
+ # Verwende prozentualen Threshold basierend auf Maximum
252
+ dynamic_threshold = max_val * 0.8
253
+ print(f" ✅ SAM ist sicher (max_val={max_val:.3f} >= 0.6)")
254
+ print(f" 🎯 Dynamischer Threshold: {dynamic_threshold:.3f} (80% von Maximum)")
255
+
256
+ mask_array = (mask_np > dynamic_threshold).astype(np.uint8) * 255
257
  unique_vals = np.unique(mask_array)
258
+ print(f" Nach Threshold ({dynamic_threshold:.3f}): {mask_array.shape}, Unique Werte: {unique_vals}")
259
+
260
+ # ============================================================
261
+ # NEU: VORFILTERUNG FÜR KLEINE KOMPONENTEN (RAUSCHEN ENTFERNEN)
262
+ # ============================================================
263
+ if np.sum(mask_array > 0) > 0: # Nur wenn weiße Pixel existieren
264
+ labeled_array, num_features = ndimage.label(mask_array)
265
+ if num_features > 1:
266
+ print(f" 🧹 Vorfilterung: Gefundene Komponenten vor Filterung: {num_features}")
267
+
268
+ sizes = ndimage.sum(mask_array, labeled_array, range(1, num_features + 1))
269
+ min_size = 1000 # Minimale Größe für eine sinnvolle Komponente
270
+
271
+ # Zähle, wie viele Komponenten die Mindestgröße erreichen
272
+ valid_components = 0
273
+ for i in range(1, num_features + 1):
274
+ if sizes[i-1] >= min_size:
275
+ valid_components += 1
276
+ else:
277
+ # Entferne kleine Komponenten (Rauschen)
278
+ mask_array = np.where(labeled_array == i, 0, mask_array)
279
+
280
+ print(f" ✅ Entferne kleine Komponenten (<{min_size}px): {num_features} → {valid_components} Komponenten")
281
+
282
  # 7. BEIDE MASKEN ERSTELLEN (vor Nachbearbeitung)
283
  original_mask_array = mask_array.copy() # Person weiß (255), Hintergrund schwarz (0)
284
  inverted_mask_array = 255 - mask_array # Person schwarz (0), Hintergrund weiß (255)
 
367
 
368
  if num_features > 0:
369
  sizes = ndimage.sum(mask_array, labeled_array, range(1, num_features + 1))
370
+ print(f" Größen der weißen Komponenten auf AUSSCHNITT: {sizes[:10]}...") # Nur erste 10 anzeigen
371
 
372
  if num_features > 1:
373
  # WICHTIG: Für Gesicht nehmen wir die GRÖSSTE Komponente im AUSSCHNITT