Astridkraft commited on
Commit
9b14886
·
verified ·
1 Parent(s): 3a6d54d

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +26 -89
controlnet_module.py CHANGED
@@ -253,97 +253,34 @@ class ControlNetProcessor:
253
  print(f"Fehler beim Laden von Multi-ControlNet Outside: {e}")
254
  raise
255
  return self.pipe_multi_outside
256
-
257
- def generate_with_controlnet(
258
- self, image, prompt, negative_prompt,
259
- steps, guidance_scale, controlnet_strength,
260
- progress=None, keep_environment=False
261
- ):
262
- """
263
- GENERIERT BILD MIT CONTROLNET
264
- WICHTIG: Diese Funktion wird von app.py aufgerufen
265
-
266
- Parameter keep_environment bestimmt:
267
- - True: "Umgebung ändern" und "Ausschließlich Gesicht" → Depth+Canny
268
- - False: "Focus verändern" → OpenPose+Canny
269
-
270
- Die eigentliche Maskenlogik wird in app.py (create_face_mask) gehandhabt
271
- """
272
- try:
273
- # --- LOGIK FÜR 3 MODI (VON APP.PY GESTEUERT) ---
274
- if keep_environment:
275
- # FALL 1 & 3: Umgebung ändern ODER Ausschließlich Gesicht → Depth + Canny
276
- print("🎯 ControlNet: Depth + Canny (keep_environment=True)")
277
-
278
- # Beide Conditioning Maps erstellen
279
- depth_image = self.extract_depth_map(image)
280
- canny_image = self.extract_canny_edges(image)
281
- print("✅ Depth + Canny Maps für Outside/Inside-Box erstellt")
282
-
283
- # Multi-ControlNet für Outside verwenden
284
- conditioning_images = [depth_image, canny_image]
285
- controlnet_type = "multi_outside"
286
-
287
- # Gewichtung: Depth 60%, Canny 40%
288
- controlnet_conditioning_scale = [controlnet_strength * 0.6, # Depth: 60% für räumliche Tiefe
289
- controlnet_strength * 0.4] # Canny: 40% für Strukturen
290
-
291
- else:
292
- # FALL 2: Focus verändern → OpenPose + Canny
293
- print("🎯 ControlNet: OpenPose + Canny (keep_environment=False)")
294
-
295
- # Beide Conditioning Maps erstellen
296
- pose_image = self.extract_pose(image)
297
- canny_image = self.extract_canny_edges(image)
298
- print("✅ OpenPose + Canny Maps für Inside-Box erstellt")
299
-
300
- # Multi-ControlNet für Inside verwenden
301
- conditioning_images = [pose_image, canny_image]
302
- controlnet_type = "multi_inside"
303
-
304
- # Gewichtung: OpenPose 70%, Canny 30%
305
- controlnet_conditioning_scale = [controlnet_strength * 0.7, # OpenPose: 70% für Person
306
- controlnet_strength * 0.3] # Canny: 30% für Konturen
307
-
308
- # Zufälliger Seed
309
- seed = random.randint(0, 2**32 - 1)
310
- generator = torch.Generator(device=self.device).manual_seed(seed)
311
- print(f"ControlNet Seed: {seed}")
312
-
313
- pipe = self.load_controlnet_pipeline(controlnet_type)
314
-
315
- # Fortschritt-Callback
316
- callback = ControlNetProgressCallback(progress, int(steps)) if progress is not None else None
317
-
318
- print("🔄 ControlNet: Starte Pipeline...")
319
-
320
- # ControlNet Generierung
321
- result = pipe(
322
- prompt=prompt,
323
- image=conditioning_images,
324
- negative_prompt=negative_prompt,
325
- num_inference_steps=int(steps),
326
- guidance_scale=guidance_scale,
327
- generator=generator,
328
- controlnet_conditioning_scale=controlnet_conditioning_scale,
329
- height=512,
330
- width=512,
331
- output_type="pil",
332
- callback_on_step_end=callback,
333
- callback_on_step_end_tensor_inputs=[],
334
- )
335
-
336
- print("✅ ControlNet abgeschlossen!")
337
 
338
- # Rückgabe: ControlNet-Output + Originalbild (für Inpaint)
339
- return result.images[0], image
340
 
341
- except Exception as e:
342
- print(f"❌ Fehler in ControlNet: {e}")
343
- import traceback
344
- traceback.print_exc()
345
- error_image = image.convert("RGB").resize((512, 512))
346
- return error_image, error_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
  def prepare_inpaint_input(self, image, keep_environment=False):
349
  """
 
253
  print(f"Fehler beim Laden von Multi-ControlNet Outside: {e}")
254
  raise
255
  return self.pipe_multi_outside
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
 
 
 
257
 
258
+ def prepare_controlnet_maps(self, image, keep_environment=False):
259
+ """
260
+ ERSTELLT NUR CONDITIONING-MAPS, generiert KEIN Bild.
261
+ """
262
+ print("🎯 ControlNet: Erstelle Conditioning-Maps...")
263
+
264
+ if keep_environment:
265
+ # Depth + Canny
266
+ print(" Modus: Depth + Canny")
267
+ conditioning_images = [
268
+ self.extract_depth_map(image),
269
+ self.extract_canny_edges(image)
270
+ ]
271
+ else:
272
+ # OpenPose + Canny
273
+ print(" Modus: OpenPose + Canny")
274
+ conditioning_images = [
275
+ self.extract_pose(image),
276
+ self.extract_canny_edges(image)
277
+ ]
278
+
279
+ print(f"✅ {len(conditioning_images)} Conditioning-Maps erstellt.")
280
+ return conditioning_images # Rückgabe: Liste der PIL Images
281
+
282
+
283
+
284
 
285
  def prepare_inpaint_input(self, image, keep_environment=False):
286
  """