Update controlnet_facefix.py
Browse files- controlnet_facefix.py +145 -50
controlnet_facefix.py
CHANGED
|
@@ -1,67 +1,162 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel
|
| 3 |
-
from controlnet_aux import ZoeDetector
|
| 4 |
from PIL import Image
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
print("
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
print("✅ ControlNet Depth geladen")
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
model_id,
|
| 28 |
-
controlnet=[
|
| 29 |
torch_dtype=torch.float16,
|
| 30 |
safety_checker=None,
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
|
| 35 |
-
try:
|
| 36 |
-
pipe = _get_facefix_pipeline(model_id)
|
| 37 |
-
depth_img = depth_processor(image)
|
| 38 |
-
|
| 39 |
-
face_prompt = prompt + ", perfect face, detailed skin"
|
| 40 |
-
face_negative = negative_prompt + ", deformed face"
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
image=image,
|
| 46 |
-
mask_image=None,
|
| 47 |
-
control_image=[depth_img],
|
| 48 |
-
controlnet_conditioning_scale=[0.7],
|
| 49 |
-
strength=0.35,
|
| 50 |
-
num_inference_steps=20,
|
| 51 |
-
guidance_scale=7.0,
|
| 52 |
-
generator=torch.Generator("cuda").manual_seed(seed),
|
| 53 |
-
).images[0]
|
| 54 |
-
|
| 55 |
-
return fixed
|
| 56 |
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
-
print(f"
|
| 59 |
return image
|
| 60 |
-
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print(f"❌ Face-Fix Setup fehlgeschlagen: {e}")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# controlnet_facefix.py - KORRIGIERTE VERSION FÜR HF SPACES
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
print("="*60)
|
| 8 |
+
print("CONTROLNET_FACEFIX START")
|
| 9 |
+
print("="*60)
|
| 10 |
|
| 11 |
+
# WICHTIG: Keine .to("cuda") beim Laden!
|
| 12 |
+
_components_loaded = False
|
| 13 |
+
_depth_processor = None
|
| 14 |
+
_controlnet_depth = None
|
| 15 |
+
_controlnet_pose = None
|
| 16 |
+
_pipeline = None
|
| 17 |
+
|
| 18 |
+
def _initialize_components():
|
| 19 |
+
"""Lade alle Komponenten mit Fehlerbehandlung"""
|
| 20 |
+
global _components_loaded, _depth_processor, _controlnet_depth, _controlnet_pose
|
| 21 |
+
|
| 22 |
+
if _components_loaded:
|
| 23 |
+
return True
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
print("1. Lade Depth Processor...")
|
| 27 |
+
from controlnet_aux import ZoeDetector
|
| 28 |
+
_depth_processor = ZoeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 29 |
+
print(" ✅ Depth Processor OK")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f" ❌ Depth Processor Fehler: {e}")
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
print("2. Lade ControlNet Depth...")
|
| 36 |
+
_controlnet_depth = ControlNetModel.from_pretrained(
|
| 37 |
+
"lllyasviel/control_v11f1e_sd15_depth",
|
| 38 |
+
torch_dtype=torch.float16 # KEIN .to("cuda") hier!
|
| 39 |
+
)
|
| 40 |
+
print(" ✅ ControlNet Depth OK")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f" ❌ ControlNet Depth Fehler: {e}")
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
print("3. Lade ControlNet OpenPose...")
|
| 47 |
+
# WICHTIG: KEIN subfolder="faceonly" - das gibt es nicht!
|
| 48 |
+
_controlnet_pose = ControlNetModel.from_pretrained(
|
| 49 |
+
"lllyasviel/control_v11p_sd15_openpose",
|
| 50 |
+
torch_dtype=torch.float16 # KEIN .to("cuda") hier!
|
| 51 |
+
)
|
| 52 |
+
print(" ✅ ControlNet OpenPose OK")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f" ❌ ControlNet OpenPose Fehler: {e}")
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
_components_loaded = True
|
| 58 |
+
print("✅ ALLE KOMPONENTEN GELADEN")
|
| 59 |
+
return True
|
| 60 |
+
|
| 61 |
+
def _create_control_images(image):
|
| 62 |
+
"""Erstelle Depth und Pose Bilder"""
|
| 63 |
+
try:
|
| 64 |
+
print(" Erstelle Depth Map...")
|
| 65 |
+
depth_img = _depth_processor(image)
|
| 66 |
+
|
| 67 |
+
print(" Erstelle OpenPose...")
|
| 68 |
+
from controlnet_aux import OpenposeDetector
|
| 69 |
+
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 70 |
+
pose_img = openpose(image)
|
| 71 |
+
|
| 72 |
+
return pose_img, depth_img
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f" ❌ Control Images Fehler: {e}")
|
| 75 |
+
return None, None
|
| 76 |
+
|
| 77 |
+
def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
|
| 78 |
+
"""HAUPTFUNKTION - Optimiert für HF Spaces"""
|
| 79 |
+
print("\n" + "🎭"*50)
|
| 80 |
+
print("FACE-FIX WIRD AUSGEFÜHRT")
|
| 81 |
+
print(f" Model: {model_id}")
|
| 82 |
+
print(f" Seed: {seed}")
|
| 83 |
+
print("🎭"*50)
|
| 84 |
+
|
| 85 |
+
start_time = time.time()
|
| 86 |
|
| 87 |
+
# 1. Komponenten initialisieren
|
| 88 |
+
if not _initialize_components():
|
| 89 |
+
print("❌ Komponenten konnten nicht geladen werden")
|
| 90 |
+
return image
|
|
|
|
| 91 |
|
| 92 |
+
# 2. Control Images erstellen
|
| 93 |
+
pose_img, depth_img = _create_control_images(image)
|
| 94 |
+
if pose_img is None or depth_img is None:
|
| 95 |
+
print("❌ Control Images konnten nicht erstellt werden")
|
| 96 |
+
return image
|
| 97 |
|
| 98 |
+
# 3. Pipeline erstellen (lazy)
|
| 99 |
+
global _pipeline
|
| 100 |
+
if _pipeline is None:
|
| 101 |
+
try:
|
| 102 |
+
print("🔄 Lade Pipeline...")
|
| 103 |
+
_pipeline = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
| 104 |
model_id,
|
| 105 |
+
controlnet=[_controlnet_pose, _controlnet_depth],
|
| 106 |
torch_dtype=torch.float16,
|
| 107 |
safety_checker=None,
|
| 108 |
+
requires_safety_checker=False,
|
| 109 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
# HF Spaces Optimierungen
|
| 112 |
+
_pipeline.enable_attention_slicing()
|
| 113 |
+
_pipeline.enable_vae_slicing()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
print("✅ Pipeline geladen")
|
| 116 |
except Exception as e:
|
| 117 |
+
print(f"❌ Pipeline Fehler: {e}")
|
| 118 |
return image
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
try:
|
| 121 |
+
# 4. Auf GPU bewegen (erst JETZT!)
|
| 122 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 123 |
+
print(f" Device: {device}")
|
| 124 |
+
pipeline = _pipeline.to(device)
|
| 125 |
+
|
| 126 |
+
# 5. Prompts optimieren
|
| 127 |
+
face_prompt = f"{prompt}, perfect face, detailed skin, realistic eyes, sharp focus"
|
| 128 |
+
face_negative = f"{negative_prompt}, deformed face, blurry face, bad anatomy, ugly"
|
| 129 |
+
|
| 130 |
+
print("⚡ Führe Face-Fix Inference aus...")
|
| 131 |
+
|
| 132 |
+
# 6. Face-Fix ausführen
|
| 133 |
+
result = pipeline(
|
| 134 |
+
prompt=face_prompt,
|
| 135 |
+
negative_prompt=face_negative,
|
| 136 |
+
image=image,
|
| 137 |
+
mask_image=None, # Ganzer Bildbereich
|
| 138 |
+
control_image=[pose_img, depth_img],
|
| 139 |
+
controlnet_conditioning_scale=[0.8, 0.6], # OpenPose stärker
|
| 140 |
+
strength=0.4, # Mittlere Stärke
|
| 141 |
+
num_inference_steps=20,
|
| 142 |
+
guidance_scale=7.0,
|
| 143 |
+
generator=torch.Generator(device).manual_seed(seed),
|
| 144 |
+
height=512,
|
| 145 |
+
width=512,
|
| 146 |
+
).images[0]
|
| 147 |
+
|
| 148 |
+
duration = time.time() - start_time
|
| 149 |
+
print(f"\n✅✅✅ FACE-FIX ERFOLGREICH in {duration:.1f}s ✅✅✅")
|
| 150 |
+
|
| 151 |
+
return result
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f"\n❌❌❌ FACE-FIX FEHLGESCHLAGEN: {e} ❌❌❌")
|
| 155 |
+
import traceback
|
| 156 |
+
traceback.print_exc()
|
| 157 |
+
return image
|
| 158 |
+
|
| 159 |
+
print("="*60)
|
| 160 |
+
print("CONTROLNET_FACEFIX INITIALISIERT")
|
| 161 |
+
print(f"apply_facefix Funktion: {'apply_facefix' in globals()}")
|
| 162 |
+
print("="*60)
|