Astridkraft commited on
Commit
1943b28
·
verified ·
1 Parent(s): c035e32

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +61 -67
controlnet_module.py CHANGED
@@ -143,73 +143,67 @@ class ControlNetProcessor:
143
  return self.pipe_canny
144
 
145
  def generate_with_controlnet(
146
- self, image, prompt, negative_prompt,
147
- steps, guidance_scale, controlnet_strength,
148
- progress=None, keep_environment=False
149
- ):
150
- """Generiert Bild mit ControlNet und Fortschrittsanzeige"""
151
- try:
152
- # --- KORREKTE LOGIK ---
153
- if keep_environment:
154
- # UMGEBUNG BEIBEHALTEN, PERSON ÄNDERN
155
- controlnet_type = "canny" # ✅ Canny behält Umgebung
156
- print("🎯 ControlNet Modus: Umgebung beibehalten (Canny Edge)")
157
- conditioning_image = self.extract_canny_edges(image)
158
- else:
159
- # PERSON BEIBEHALTEN, UMGEBUNG ÄNDERN
160
- controlnet_type = "openpose" # OpenPose behält Person
161
- print("🎯 ControlNet Modus: Person beibehalten (OpenPose)")
162
- conditioning_image = self.extract_pose(image)
163
-
164
- pipe = self.load_controlnet_pipeline(controlnet_type)
165
-
166
- # Zufälliger Seed
167
- seed = random.randint(0, 2**32 - 1)
168
- generator = torch.Generator(device=self.device).manual_seed(seed)
169
- print(f"ControlNet Seed: {seed}")
170
-
171
- # Fortschritt-Callback
172
- callback = ControlNetProgressCallback(progress, int(steps)) if progress is not None else None
173
-
174
- print("🔄 ControlNet: Starte Pipeline...")
175
-
176
- # ControlNet Generierung
177
- result = pipe(
178
- prompt=prompt,
179
- image=conditioning_image,
180
- negative_prompt=negative_prompt,
181
- num_inference_steps=int(steps),
182
- guidance_scale=guidance_scale,
183
- generator=generator,
184
- controlnet_conditioning_scale=controlnet_strength,
185
- height=512,
186
- width=512,
187
- output_type="pil",
188
- callback_on_step_end=callback,
189
- callback_on_step_end_tensor_inputs=[],
190
- )
191
-
192
- # Debug-Ausgabe Scheduler Steps
193
- try:
194
- scheduler = pipe.scheduler
195
- if hasattr(scheduler, "timesteps"):
196
- actual_steps = len(scheduler.timesteps)
197
- print(f"🎯 CONTROLNET TATSÄCHLICHE STEPS: {actual_steps} (von {steps} angefordert)")
198
- except Exception as e:
199
- print(f"⚠️ Konnte ControlNet Scheduler-Info nicht auslesen: {e}")
200
-
201
- print("✅ ControlNet abgeschlossen!")
202
-
203
- # ZWEI Werte zurückgeben
204
- return result.images[0], conditioning_image
205
-
206
- except Exception as e:
207
- print(f"❌ Fehler in ControlNet: {e}")
208
- import traceback
209
- traceback.print_exc()
210
- error_image = image.convert("RGB").resize((512, 512))
211
- return error_image, error_image
212
-
213
  def prepare_inpaint_input(self, image, keep_environment=False):
214
  """
215
  Bereitet das Input-Bild für Inpaint vor
 
143
  return self.pipe_canny
144
 
145
  def generate_with_controlnet(
146
+ self, image, prompt, negative_prompt,
147
+ steps, guidance_scale, controlnet_strength,
148
+ progress=None, keep_environment=False
149
+ ):
150
+ """Generiert Bild mit ControlNet und Fortschrittsanzeige"""
151
+ try:
152
+ # --- KORREKTE LOGIK ---
153
+ if keep_environment:
154
+ # UMGEBUNG BEIBEHALTEN, PERSON ÄNDERN
155
+ controlnet_type = "canny" # ✅ Canny behält Umgebung
156
+ print("🎯 ControlNet Modus: Umgebung beibehalten (Canny Edge)")
157
+ conditioning_image = self.extract_canny_edges(image)
158
+ inpaint_input = image # ✅ ORIGINALBILD für Inpaint!
159
+ else:
160
+ # PERSON BEIBEHALTEN, UMGEBUNG ÄNDERN
161
+ controlnet_type = "openpose" # OpenPose behält Person
162
+ print("🎯 ControlNet Modus: Person beibehalten (OpenPose)")
163
+ conditioning_image = self.extract_pose(image)
164
+ inpaint_input = conditioning_image # ✅ POSE-MAP für Inpaint
165
+
166
+ pipe = self.load_controlnet_pipeline(controlnet_type)
167
+
168
+ # Zufälliger Seed
169
+ seed = random.randint(0, 2**32 - 1)
170
+ generator = torch.Generator(device=self.device).manual_seed(seed)
171
+ print(f"ControlNet Seed: {seed}")
172
+
173
+ # Fortschritt-Callback
174
+ callback = ControlNetProgressCallback(progress, int(steps)) if progress is not None else None
175
+
176
+ print("🔄 ControlNet: Starte Pipeline...")
177
+
178
+ # ControlNet Generierung
179
+ result = pipe(
180
+ prompt=prompt,
181
+ image=conditioning_image,
182
+ negative_prompt=negative_prompt,
183
+ num_inference_steps=int(steps),
184
+ guidance_scale=guidance_scale,
185
+ generator=generator,
186
+ controlnet_conditioning_scale=controlnet_strength,
187
+ height=512,
188
+ width=512,
189
+ output_type="pil",
190
+ callback_on_step_end=callback,
191
+ callback_on_step_end_tensor_inputs=[],
192
+ )
193
+
194
+ print("✅ ControlNet abgeschlossen!")
195
+
196
+ # DREI Werte zurückgeben
197
+ return result.images[0], conditioning_image, inpaint_input
198
+
199
+ except Exception as e:
200
+ print(f"❌ Fehler in ControlNet: {e}")
201
+ import traceback
202
+ traceback.print_exc()
203
+ error_image = image.convert("RGB").resize((512, 512))
204
+ return error_image, error_image, error_image
205
+
206
+
 
 
 
 
 
 
207
  def prepare_inpaint_input(self, image, keep_environment=False):
208
  """
209
  Bereitet das Input-Bild für Inpaint vor