Astridkraft commited on
Commit
7755dba
·
verified ·
1 Parent(s): 2562888

Update controlnet_facefix.py

Browse files
Files changed (1) hide show
  1. controlnet_facefix.py +33 -22
controlnet_facefix.py CHANGED
@@ -1,4 +1,4 @@
1
- # controlnet_facefix.py - EINFACHE VERSION (GANZES BILD VERBESSERN)
2
  import torch
3
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
  from PIL import Image
@@ -7,7 +7,7 @@ import cv2
7
  import numpy as np
8
 
9
  print("="*60)
10
- print("FACE-FIX: GANZES BILD VERBESSERN")
11
  print("="*60)
12
 
13
  # WICHTIG: Dieselben Modelle wie in controlnet_module.py!
@@ -26,7 +26,7 @@ def _initialize_components():
26
  try:
27
  print("1. Lade ControlNet Depth...")
28
  _controlnet_depth = ControlNetModel.from_pretrained(
29
- "lllyasviel/sd-controlnet-depth", # ← FUNKTIONIERT BEI DIR!
30
  torch_dtype=torch.float16
31
  )
32
  print(" ✅ ControlNet Depth OK")
@@ -37,7 +37,7 @@ def _initialize_components():
37
  try:
38
  print("2. Lade ControlNet OpenPose...")
39
  _controlnet_pose = ControlNetModel.from_pretrained(
40
- "lllyasviel/sd-controlnet-openpose", # ← FUNKTIONIERT BEI DIR!
41
  torch_dtype=torch.float16
42
  )
43
  print(" ✅ ControlNet OpenPose OK")
@@ -73,11 +73,11 @@ def _extract_pose_simple(image):
73
  return image.convert("RGB")
74
 
75
  def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
76
- """GANZES BILD verbessern mit ControlNets"""
77
  print("\n" + "🎭"*50)
78
- print("FACE-FIX: VERBESSERE GANZES BILD")
79
  print(f" Model: {model_id}")
80
- print(f" Seed: {seed}")
81
  print("🎭"*50)
82
 
83
  start_time = time.time()
@@ -120,32 +120,43 @@ def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: i
120
  print(f" Device: {device}")
121
  pipeline = _pipeline.to(device)
122
 
123
- # 5. Prompts für allgemeine Verbesserung
124
- enhanced_prompt = f"{prompt}, high quality, detailed, sharp focus, professional photography"
125
- enhanced_negative = f"{negative_prompt}, blurry, low quality, pixelated, artifacts"
 
 
126
 
127
- print("⚡ Verbessere gesamtes Bild...")
 
128
 
129
- # 6. GANZES BILD verbessern (mask_image=None)
 
 
 
 
 
130
  result = pipeline(
131
- prompt=enhanced_prompt,
132
- negative_prompt=enhanced_negative,
133
- image=[pose_img, depth_img], # ← WICHTIG: control_image als Liste
134
- controlnet_conditioning_scale=[0.7, 0.5],
135
- num_inference_steps=20,
136
- guidance_scale=7.0,
137
- generator=torch.Generator(device).manual_seed(seed),
138
  height=512,
139
  width=512,
140
  ).images[0]
141
-
142
 
143
  # Zurück auf Originalgröße
144
  if image.size != (512, 512):
145
  result = result.resize(image.size)
146
 
147
  duration = time.time() - start_time
148
- print(f"\n✅✅✅ BILDVERBESSERUNG ERFOLGREICH in {duration:.1f}s ✅✅✅")
 
 
 
 
149
 
150
  return result
151
 
@@ -156,5 +167,5 @@ def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: i
156
  return image
157
 
158
  print("="*60)
159
- print("FACE-FIX MODUL FERTIG (GANZES BILD VERBESSERN)")
160
  print("="*60)
 
1
+ # controlnet_facefix.py - OPTIMIERT FÜR GESICHTSVERBESSERUNG
2
  import torch
3
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
  from PIL import Image
 
7
  import numpy as np
8
 
9
  print("="*60)
10
+ print("FACE-FIX: GESICHTSVERBESSERUNG MIT OPENPOSE + DEPTH")
11
  print("="*60)
12
 
13
  # WICHTIG: Dieselben Modelle wie in controlnet_module.py!
 
26
  try:
27
  print("1. Lade ControlNet Depth...")
28
  _controlnet_depth = ControlNetModel.from_pretrained(
29
+ "lllyasviel/sd-controlnet-depth",
30
  torch_dtype=torch.float16
31
  )
32
  print(" ✅ ControlNet Depth OK")
 
37
  try:
38
  print("2. Lade ControlNet OpenPose...")
39
  _controlnet_pose = ControlNetModel.from_pretrained(
40
+ "lllyasviel/sd-controlnet-openpose",
41
  torch_dtype=torch.float16
42
  )
43
  print(" ✅ ControlNet OpenPose OK")
 
73
  return image.convert("RGB")
74
 
75
  def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
76
+ """OPTIMIERT: Gesichter verbessern, Gesamtbild minimal ändern"""
77
  print("\n" + "🎭"*50)
78
+ print("FACE-FIX: OPTIMIERT FÜR GESICHTER")
79
  print(f" Model: {model_id}")
80
+ print(f" Original Seed: {seed}")
81
  print("🎭"*50)
82
 
83
  start_time = time.time()
 
120
  print(f" Device: {device}")
121
  pipeline = _pipeline.to(device)
122
 
123
+ # 5. OPTIMIERTE PARAMETER FÜR GESICHTSVERBESSERUNG:
124
+ # - OpenPose stärker (0.6) für Gesichtsdetails
125
+ # - Depth schwächer (0.2) für minimalen Hintergrund-Einfluss
126
+ # - Gleicher Seed für Konsistenz
127
+ # - Kürzerer, fokussierter Prompt nur für Qualität
128
 
129
+ # Gesichtsspezifischer Prompt (ohne Inhalt zu ändern)
130
+ face_quality_prompt = "perfect face, detailed skin, realistic eyes, sharp facial features, high quality"
131
 
132
+ # Negativer Prompt für Gesichtsfehler
133
+ face_negative = "deformed face, blurry face, bad eyes, asymmetric, low quality, mutated"
134
+
135
+ print("⚡ Verbessere Gesichter (OpenPose stärker, Depth schwächer)...")
136
+
137
+ # 6. OPTIMIERTE INFERENCE
138
  result = pipeline(
139
+ prompt=face_quality_prompt, # Nur Qualität, kein Inhalt!
140
+ negative_prompt=face_negative,
141
+ image=[pose_img, depth_img],
142
+ controlnet_conditioning_scale=[0.6, 0.2], # OPTIMIERT: OpenPose 0.6, Depth 0.2
143
+ num_inference_steps=12, # Weniger Steps = weniger Änderung
144
+ guidance_scale=5.0, # Mittel für Balance
145
+ generator=torch.Generator(device).manual_seed(seed), # Gleicher Seed!
146
  height=512,
147
  width=512,
148
  ).images[0]
 
149
 
150
  # Zurück auf Originalgröße
151
  if image.size != (512, 512):
152
  result = result.resize(image.size)
153
 
154
  duration = time.time() - start_time
155
+ print(f"\n✅✅✅ GESICHTSVERBESSERUNG ERFOLGREICH in {duration:.1f}s ✅✅✅")
156
+ print(f" - OpenPose Strength: 0.6 (für Gesicht)")
157
+ print(f" - Depth Strength: 0.2 (minimaler Hintergrund-Einfluss)")
158
+ print(f" - Steps: 12 (weniger Änderung)")
159
+ print(f" - Gleicher Seed: {seed}")
160
 
161
  return result
162
 
 
167
  return image
168
 
169
  print("="*60)
170
+ print("FACE-FIX MODUL FERTIG (OPTIMIERT FÜR GESICHTER)")
171
  print("="*60)