Astridkraft commited on
Commit
3cbb0e3
·
verified ·
1 Parent(s): d916b1d

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +34 -10
controlnet_module.py CHANGED
@@ -269,20 +269,44 @@ class ControlNetProcessor:
269
  print("🖼️ BILDAUFBEREITUNG FÜR SAM 2")
270
  image_np = np.array(image.convert("RGB"))
271
 
272
- # KORREKTUR: Immer nur eine BBox verwenden (SAM 2 erwartet genau 1)
273
  input_boxes = [[[x1, y1, x2, y2]]]
274
- print(f" Konvertiere Bild zu NumPy Array: {image_np.shape}")
275
- print(f" Erstelle EINZIGE Input Box: {input_boxes}")
276
- print(" ℹ️ SAM 2 erwartet genau eine BBox pro Vorhersage")
277
-
278
- print("🎯 SCHRITT 4-5: SAM MIT BOX-PROMPT")
279
- print(" Verarbeite Bild mit SAM 2 Processor...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  inputs = self.sam_processor(
281
- image_np,
282
  input_boxes=input_boxes,
 
 
283
  return_tensors="pt"
284
- ).to(self.device)
285
- print(f"✅ Processor-Ausgabe: {len(inputs)} Elemente")
 
 
 
 
 
 
286
 
287
  # 4. SAM2 Vorhersage
288
  print("-" * 60)
 
269
  print("🖼️ BILDAUFBEREITUNG FÜR SAM 2")
270
  image_np = np.array(image.convert("RGB"))
271
 
272
+ # Immer nur eine BBox verwenden (SAM 2 erwartet genau 1)
273
  input_boxes = [[[x1, y1, x2, y2]]]
274
+
275
+ # 1. Punkt in der BBox-Mitte (zur Ünterstützung von SAM damit BBox nicht zu dicht um Kopf gezogen werden muß!)
276
+ center_x = (x1 + x2) // 2
277
+ center_y = (y1 + y2) // 2
278
+
279
+ # 2. Punkt im Gesicht (30% höher vom Mittelpunkt)(auch für größere BBox)
280
+ bbox_height = y2 - y1
281
+ face_offset = int(bbox_height * 0.3)
282
+ face_x = center_x
283
+ face_y = center_y - face_offset
284
+ face_y = max(y1 + 10, min(face_y, y2 - 10)) # In BBox halten
285
+
286
+ # BEIDE Punkte kombinieren
287
+ input_points = [[[center_x, center_y], [face_x, face_y]]] # ZWEI Punkte
288
+ input_labels = [[1, 1]] # Beide sind positive Prompts
289
+
290
+ print(f" 🎯 SAM-Prompt: BBox [{x1},{y1},{x2},{y2}]")
291
+ print(f" 👁️ Punkte: Mitte ({center_x},{center_y}), Gesicht ({face_x},{face_y})")
292
+
293
+
294
+ # Aufruf des SAM-Prozessors mit den Variablen. Der Processor verpackt diese Rohdaten
295
+ # in die für das SAM-Modell erforderlichen Tensoren und speichert sie in inputs.
296
  inputs = self.sam_processor(
297
+ image_np,
298
  input_boxes=input_boxes,
299
+ input_points=input_points, # ZWEI Punkte
300
+ input_labels=input_labels, # Zwei Labels
301
  return_tensors="pt"
302
+ ).to(self.device) # Ohne .to(self.device) werden die Tensoren standardmäßig im CPU-RAM erzeugt und gespeichert! Da GPU-Fehler!
303
+
304
+ print(f"✅ Processor-Ausgabe: Dictionary mit {len(inputs)} Schlüsseln: {list(inputs.keys())}")
305
+ print(f" - 'pixel_values' Shape: {inputs['pixel_values'].shape}")
306
+ print(f" - 'input_boxes' Shape: {inputs['input_boxes'].shape}")
307
+ if 'input_points' in inputs:
308
+ print(f" - 'input_points' Shape: {inputs['input_points'].shape}")
309
+
310
 
311
  # 4. SAM2 Vorhersage
312
  print("-" * 60)