Astridkraft commited on
Commit
1fd9dad
·
verified ·
1 Parent(s): 6a2da42

Update controlnet_module.py

Browse files
Files changed (1) hide show
  1. controlnet_module.py +60 -9
controlnet_module.py CHANGED
@@ -94,26 +94,77 @@ class ControlNetProcessor:
94
  print(f"Fehler bei Canny Edge Extraction: {e}")
95
  return image.convert("RGB").resize((512, 512))
96
 
 
97
  def extract_depth_map(self, image):
98
- """Extrahiert Depth Map für räumliche Konsistenz"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  try:
100
- # Für echte Depth-Maps würde man ein Depth-Estimation-Modell verwenden
101
- # Hier als Fallback: Konvertierung zu Grayscale als Depth-Approximation
102
  img_array = np.array(image.convert("RGB"))
103
  gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
104
-
105
- # Depth-ähnliche Map erstellen (helle Bereiche = nah, dunkle = fern)
106
  depth_map = cv2.GaussianBlur(gray, (5, 5), 0)
107
  depth_rgb = cv2.cvtColor(depth_map, cv2.COLOR_GRAY2RGB)
108
  depth_image = Image.fromarray(depth_rgb)
109
-
110
- print("✅ Depth Map erstellt (Grayscale Approximation)")
111
  return depth_image
112
- except Exception as e:
113
- print(f"Fehler bei Depth Map Extraction: {e}")
114
  return image.convert("RGB").resize((512, 512))
115
 
116
 
 
117
  def prepare_controlnet_maps(self, image, keep_environment=False):
118
  """
119
  ERSTELLT NUR CONDITIONING-MAPS, generiert KEIN Bild.
 
94
  print(f"Fehler bei Canny Edge Extraction: {e}")
95
  return image.convert("RGB").resize((512, 512))
96
 
97
+
98
  def extract_depth_map(self, image):
99
+ """
100
+ Extrahiert Depth Map mit MiDaS Small (Fallback auf alten Filter).
101
+ """
102
+ try:
103
+ print("🔄 Versuche MiDaS Small für Depth Map...")
104
+ # 1. MiDaS Modelle vor dem ersten Gebrauch laden (spart VRAM)
105
+ if not hasattr(self, 'midas_model'):
106
+ from torchvision.transforms import Compose, Resize, ToTensor, Normalize
107
+ import midas
108
+
109
+ self.midas_transform = Compose([
110
+ Resize(384, interpolation=midas.utils.interpolation),
111
+ ToTensor(),
112
+ Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
113
+ ])
114
+
115
+ # WICHTIG: MiDaS Small lädt automatisch die 'small'-Variante (weniger VRAM)
116
+ self.midas_model = midas.MiDaS()
117
+ self.midas_model.eval()
118
+
119
+ if self.device == 'cuda':
120
+ self.midas_model.to(self.device)
121
+ print("✅ MiDaS Small Modell geladen (GPU)")
122
+ else:
123
+ print("✅ MiDaS Small Modell geladen (CPU)")
124
+
125
+ # 2. Bild für MiDaS vorbereiten
126
+ img_input = self.midas_transform(image).unsqueeze(0).to(self.device)
127
+
128
+ # 3. Depth Map berechnen
129
+ with torch.no_grad():
130
+ prediction = self.midas_model(img_input)
131
+ prediction = torch.nn.functional.interpolate(
132
+ prediction.unsqueeze(1),
133
+ size=image.size[::-1], # (height, width)
134
+ mode="bicubic",
135
+ align_corners=False,
136
+ ).squeeze()
137
+
138
+ # 4. Normalisierung für sichtbare Ausgabe
139
+ depth_np = prediction.cpu().numpy()
140
+ depth_min, depth_max = depth_np.min(), depth_np.max()
141
+ if depth_max > depth_min:
142
+ depth_np = (depth_np - depth_min) / (depth_max - depth_min)
143
+ depth_np = (depth_np * 255).astype(np.uint8)
144
+
145
+ depth_image = Image.fromarray(depth_np).convert("RGB")
146
+ print("✅ MiDaS Depth Map erfolgreich erstellt")
147
+ return depth_image
148
+
149
+ except Exception as e:
150
+ print(f"⚠️ MiDaS Fehler: {e}. Verwende Fallback (Grayscale Filter)...")
151
+ # Fallback auf Ihren bestehenden Filter-Code
152
  try:
 
 
153
  img_array = np.array(image.convert("RGB"))
154
  gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
155
+
156
+ # Depth-ähnliche Map erstellen (helle Bereiche = nah, dunkle = fern)
157
  depth_map = cv2.GaussianBlur(gray, (5, 5), 0)
158
  depth_rgb = cv2.cvtColor(depth_map, cv2.COLOR_GRAY2RGB)
159
  depth_image = Image.fromarray(depth_rgb)
160
+ print("✅ Fallback Depth Map erstellt")
 
161
  return depth_image
162
+ except Exception as fallback_error:
163
+ print(f" Auch Fallback fehlgeschlagen: {fallback_error}")
164
  return image.convert("RGB").resize((512, 512))
165
 
166
 
167
+
168
  def prepare_controlnet_maps(self, image, keep_environment=False):
169
  """
170
  ERSTELLT NUR CONDITIONING-MAPS, generiert KEIN Bild.