Astridkraft commited on
Commit
5a77243
·
verified ·
1 Parent(s): b417ab1

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +47 -28
controlnet_module.py CHANGED
@@ -1138,40 +1138,59 @@ class ControlNetProcessor:
1138
  # ============================================================
1139
  # POSTPROCESSING unterschiedlich für Crop/Original
1140
  # ============================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1141
  if use_crop_strategy:
1142
  print("👤 POSTPROCESSING AUF CROP-GRÖSSE")
1143
 
1144
- # 1. Größte zusammenhängende Komponente finden
1145
- labeled_array, num_features = ndimage.label(mask_array)
1146
-
1147
- if num_features > 0:
1148
- print(f" 🔍 Gefundene Komponenten: {num_features}")
1149
 
1150
- sizes = ndimage.sum(mask_array, labeled_array, range(1, num_features + 1))
1151
- largest_component_idx = np.argmax(sizes) + 1
1152
-
1153
- print(f" 👑 Größte Komponente: Nr. {largest_component_idx} mit {sizes[largest_component_idx-1]:,} Pixel")
1154
-
1155
- # NUR die größte Komponente behalten (der Kopf)
1156
- mask_array = np.where(labeled_array == largest_component_idx, mask_array, 0)
1157
-
1158
- # MORPHOLOGISCHE OPERATIONEN FÜR SAUBEREN KOPF
1159
- print(" ⚙️ Morphologische Operationen für sauberen Kopf")
1160
-
1161
- # Zuerst CLOSE, um kleine Löcher im Kopf zu füllen
1162
- kernel_close = np.ones((7, 7), np.uint8)
1163
- mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_CLOSE, kernel_close, iterations=1)
1164
- print(" • MORPH_CLOSE (7x7) - Löcher im Kopf füllen")
1165
 
1166
- # Dann OPEN, um kleine Ausreißer zu entfernen
1167
- kernel_open = np.ones((5, 5), np.uint8)
1168
- mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_OPEN, kernel_open, iterations=1)
1169
- print(" • MORPH_OPEN (5x5) - Rauschen entfernen")
1170
 
1171
- # LEICHTER DILATE FÜR MEHR ABDECKUNG (wichtig für Gesicht!)
1172
- print(" 🔲 Leichter Dilate für natürliche Abdeckung")
1173
- kernel_dilate = np.ones((5, 5), np.uint8) # Größerer Kernel für Gesicht
1174
- mask_array = cv2.dilate(mask_array, kernel_dilate, iterations=1)
 
1175
 
1176
 
1177
  #Speicherung der Binärmaske für Inpaint da keine Graupixel
 
1138
  # ============================================================
1139
  # POSTPROCESSING unterschiedlich für Crop/Original
1140
  # ============================================================
1141
+
1142
+ labeled_array, num_features = ndimage.label(mask_array)
1143
+
1144
+ if num_features > 0:
1145
+ # ERSTER SCHRITT: Nur Komponenten behalten, die in/nah der HEURISTIK-BBOX liegen
1146
+ regions = measure.regionprops(labeled_array)
1147
+ valid_regions = []
1148
+
1149
+ for region in regions:
1150
+ centroid_y, centroid_x = region.centroid
1151
+ bbox_distance = np.sqrt((centroid_x - bbox_center[0])**2 +
1152
+ (centroid_y - bbox_center[1])**2)
1153
+
1154
+ # Maximaler Abstand: 50% der BBox-Diagonale
1155
+ max_distance = np.sqrt(bbox_width**2 + bbox_height**2) * 0.5
1156
+
1157
+ if bbox_distance <= max_distance:
1158
+ valid_regions.append(region)
1159
+
1160
+ if valid_regions:
1161
+ # Größte valide Region finden
1162
+ largest_region = max(valid_regions, key=lambda r: r.area)
1163
+ mask_array = np.where(labeled_array == largest_region.label, mask_array, 0)
1164
+ print(f" ✅ {len(valid_regions)}/{num_features} Regionen in/nah BBox, größte behalten")
1165
+ else:
1166
+ print(f" ⚠️ Keine Region in/nah BBox gefunden, alle behalten")
1167
+
1168
+ # Binärmaske für Inpaint speichern (VOR weiteren Operationen!)
1169
+ inpaint_binary_mask = mask_array.copy()
1170
+
1171
  if use_crop_strategy:
1172
  print("👤 POSTPROCESSING AUF CROP-GRÖSSE")
1173
 
1174
+
1175
+ # MORPHOLOGISCHE OPERATIONEN FÜR SAUBEREN KOPF
1176
+ print(" ⚙️ Morphologische Operationen für sauberen Kopf")
 
 
1177
 
1178
+
1179
+ # Zuerst CLOSE, um kleine Löcher im Kopf zu füllen
1180
+ kernel_close = np.ones((7, 7), np.uint8)
1181
+ mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_CLOSE, kernel_close, iterations=1)
1182
+ print(" • MORPH_CLOSE (7x7) - Löcher im Kopf füllen")
 
 
 
 
 
 
 
 
 
 
1183
 
1184
+ # Dann OPEN, um kleine Ausreißer zu entfernen
1185
+ kernel_open = np.ones((5, 5), np.uint8)
1186
+ mask_array = cv2.morphologyEx(mask_array, cv2.MORPH_OPEN, kernel_open, iterations=1)
1187
+ print(" • MORPH_OPEN (5x5) - Rauschen entfernen")
1188
 
1189
+
1190
+ # LEICHTER DILATE FÜR MEHR ABDECKUNG (wichtig für Gesicht!)
1191
+ print(" 🔲 Leichter Dilate für natürliche Abdeckung")
1192
+ kernel_dilate = np.ones((5, 5), np.uint8) # Größerer Kernel für Gesicht
1193
+ mask_array = cv2.dilate(mask_array, kernel_dilate, iterations=1)
1194
 
1195
 
1196
  #Speicherung der Binärmaske für Inpaint da keine Graupixel