Astridkraft commited on
Commit
51be382
·
verified ·
1 Parent(s): 8230f4e

Delete controlnet_facefix.py

Browse files
Files changed (1) hide show
  1. controlnet_facefix.py +0 -228
controlnet_facefix.py DELETED
@@ -1,228 +0,0 @@
1
- # controlnet_facefix.py - PURE QUALITY ENHANCEMENT WITH MINIMAL CHANGE
2
- import torch
3
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, AutoencoderKL
4
- from PIL import Image, ImageFilter, ImageEnhance
5
- import time
6
- import cv2
7
- import numpy as np
8
- from torchvision import transforms
9
-
10
- print("="*60)
11
- print("FACE-FIX: REINE QUALITÄTSVERBESSERUNG - MINIMALE ÄNDERUNG")
12
- print("="*60)
13
-
14
- _components_loaded = False
15
- _controlnet_depth = None
16
- _controlnet_pose = None
17
- _pipeline = None
18
-
19
- def _initialize_components():
20
- """Lade nur notwendige Komponenten"""
21
- global _components_loaded, _controlnet_depth, _controlnet_pose
22
-
23
- if _components_loaded:
24
- return True
25
-
26
- print("⚠️ Lade nur OpenPose (Depth wird deaktiviert)...")
27
-
28
- try:
29
- # NUR OPENPOSE - Depth verändert zu viel
30
- _controlnet_pose = ControlNetModel.from_pretrained(
31
- "lllyasviel/sd-controlnet-openpose",
32
- torch_dtype=torch.float16
33
- )
34
- print("✅ OpenPose geladen")
35
-
36
- # Depth wird NICHT geladen - es verändert den Hintergrund zu stark
37
- _controlnet_depth = None
38
-
39
- _components_loaded = True
40
- return True
41
- except Exception as e:
42
- print(f"❌ Fehler: {e}")
43
- return False
44
-
45
- def _extract_precise_pose(image):
46
- """SEHR PRÄZISE Pose-Extraktion nur für Gesicht"""
47
- try:
48
- img_array = np.array(image.convert("RGB"))
49
- gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
50
-
51
- # EXTREM NIEDRIGE Thresholds für minimale Kanten
52
- edges = cv2.Canny(gray, 15, 45) # Nur feinste Kanten
53
-
54
- # Face detection für Fokus
55
- face_cascade = cv2.CascadeClassifier(
56
- cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
57
- )
58
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
59
-
60
- # Erstelle leere Pose Map
61
- pose_map = np.zeros_like(img_array)
62
-
63
- # Nur Gesichtskanten einfügen
64
- if len(faces) > 0:
65
- for (x, y, w, h) in faces:
66
- # Extrahiere Gesichtsregion
67
- face_region = edges[y:y+h, x:x+w]
68
- # Nur 10% der stärksten Kanten behalten
69
- threshold = np.percentile(face_region[face_region > 0], 90)
70
- face_region[face_region < threshold] = 0
71
- pose_map[y:y+h, x:x+w, 0] = face_region
72
- else:
73
- # Falls kein Gesicht erkannt, minimale Kanten
74
- pose_map[..., 0] = edges * 0.3 # Noch schwächer
75
-
76
- return Image.fromarray(pose_map)
77
- except:
78
- # Fallback: minimale Kanten
79
- gray = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2GRAY)
80
- edges = cv2.Canny(gray, 10, 30) * 0.2 # Sehr schwach
81
- return Image.fromarray(edges).convert("RGB")
82
-
83
- def _apply_face_enhancement(image):
84
- """EINFACHE Face Enhancement ohne AI"""
85
- try:
86
- img_array = np.array(image.convert("RGB"))
87
-
88
- # 1. Scharfe Kanten (nur leicht)
89
- sharpened = cv2.filter2D(img_array, -1,
90
- np.array([[-0.5, -0.5, -0.5],
91
- [-0.5, 5.0, -0.5],
92
- [-0.5, -0.5, -0.5]]) / 3.0)
93
-
94
- # 2. Leichter De-Noise
95
- denoised = cv2.fastNlMeansDenoisingColored(sharpened, None, 3, 3, 7, 21)
96
-
97
- # 3. Kontrast leicht erhöhen
98
- lab = cv2.cvtColor(denoised, cv2.COLOR_RGB2LAB)
99
- l, a, b = cv2.split(lab)
100
- clahe = cv2.createCLAHE(clipLimit=1.0, tileGridSize=(8,8))
101
- l = clahe.apply(l)
102
- enhanced = cv2.merge([l, a, b])
103
- enhanced = cv2.cvtColor(enhanced, cv2.COLOR_LAB2RGB)
104
-
105
- return Image.fromarray(enhanced)
106
- except:
107
- return image
108
-
109
- def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
110
- """
111
- SUPER-SUBTILE QUALITÄTSVERBESSERUNG
112
-
113
- Strategie:
114
- 1. NUR OpenPose (kein Depth - das verändert zu viel)
115
- 2. SEHR niedrige ControlNet-Stärke
116
- 3. Fast kein CFG Scale
117
- 4. Identischer Prompt
118
- """
119
- print("\n" + "🎯"*50)
120
- print("SUBTILE QUALITÄTSVERBESSERUNG")
121
- print(f" Größe: {image.size}")
122
- print("🎯"*50)
123
-
124
- start_time = time.time()
125
-
126
- # OPTION 1: Einfache non-AI Verbesserung (empfohlen)
127
- print("\n⚡ OPTION 1: Einfache non-AI Verbesserung...")
128
- enhanced = _apply_face_enhancement(image)
129
-
130
- # Optional: AI-Verbesserung nur wenn nötig
131
- use_ai_enhancement = False # Auf False setzen für minimale Änderung
132
-
133
- if not use_ai_enhancement:
134
- duration = time.time() - start_time
135
- print(f"✅ Non-AI Verbesserung in {duration:.1f}s")
136
- return enhanced
137
-
138
- # OPTION 2: Minimale AI-Verbesserung (falls gewünscht)
139
- print("⚠️ Starte MINIMALE AI-Verbesserung...")
140
-
141
- if not _initialize_components():
142
- return enhanced
143
-
144
- # Control Map vorbereiten
145
- original_size = image.size
146
- control_size = (512, 512)
147
- resized_image = image.resize(control_size, Image.Resampling.LANCZOS)
148
-
149
- # MINIMALE Pose Map
150
- pose_img = _extract_precise_pose(resized_image)
151
- pose_img.save("debug_minimal_pose.png")
152
-
153
- # Pipeline (nur falls nicht geladen)
154
- global _pipeline
155
- if _pipeline is None:
156
- try:
157
- print("🔄 Lade Pipeline...")
158
- _pipeline = StableDiffusionControlNetPipeline.from_pretrained(
159
- model_id,
160
- controlnet=[_controlnet_pose], # NUR OpenPose!
161
- torch_dtype=torch.float16,
162
- safety_checker=None,
163
- requires_safety_checker=False,
164
- )
165
-
166
- _pipeline.enable_attention_slicing()
167
- _pipeline.enable_vae_slicing()
168
-
169
- print("✅ Pipeline geladen")
170
- except Exception as e:
171
- print(f"❌ Pipeline Fehler: {e}")
172
- return enhanced
173
-
174
- try:
175
- device = "cuda" if torch.cuda.is_available() else "cpu"
176
- print(f" Device: {device}")
177
- pipeline = _pipeline.to(device)
178
-
179
- # KRITISCHE ÄNDERUNGEN:
180
- # 1. GLEICHER PROMPT wie ursprünglich
181
- # 2. SEHR niedrige ControlNet-Stärke
182
- # 3. FAST KEIN CFG
183
-
184
- print("\n⚙️ EXTREM SUBTILE PARAMETER:")
185
- print(" • OpenPose Strength: 0.3 (SEHR NIEDRIG)")
186
- print(" • Steps: 15 (wenig)")
187
- print(" • CFG: 2.0 (fast kein Guidance)")
188
- print(" • Gleicher Seed")
189
-
190
- result = pipeline(
191
- prompt=prompt, # WICHTIG: GLEICHER PROMPT!
192
- negative_prompt=f"{negative_prompt}, deformed, blurry",
193
- image=[pose_img], # Nur Pose
194
- controlnet_conditioning_scale=[0.3], # EXTREM NIEDRIG
195
- num_inference_steps=15, # WENIG Steps
196
- guidance_scale=2.0, # FAST KEIN CFG
197
- generator=torch.Generator(device).manual_seed(seed + 100), # Leicht anderer Seed
198
- height=512,
199
- width=512,
200
- ).images[0]
201
-
202
- # Zurück auf Originalgröße
203
- if original_size != (512, 512):
204
- result = result.resize(original_size, Image.Resampling.LANCZOS)
205
-
206
- # 50/50 Blend mit Original für noch weniger Änderung
207
- result_array = np.array(result).astype(float)
208
- original_array = np.array(image).astype(float)
209
-
210
- # 70% Original, 30% AI-result
211
- blended = (original_array * 0.7 + result_array * 0.3).astype(np.uint8)
212
- final_result = Image.fromarray(blended)
213
-
214
- duration = time.time() - start_time
215
- print(f"\n✅ SUBTILE VERBESSERUNG in {duration:.1f}s")
216
- print(f" • 70% Original, 30% AI")
217
- print(f" • OpenPose: 0.3")
218
- print(f" • CFG: 2.0")
219
-
220
- return final_result
221
-
222
- except Exception as e:
223
- print(f"\n❌ AI-Verbesserung fehlgeschlagen: {e}")
224
- return enhanced
225
-
226
- print("="*60)
227
- print("FACE-FIX: REINE QUALITÄTSVERBESSERUNG")
228
- print("="*60)