Update controlnet_facefix.py
Browse files- controlnet_facefix.py +57 -73
controlnet_facefix.py
CHANGED
|
@@ -1,83 +1,67 @@
|
|
| 1 |
import torch
|
| 2 |
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel
|
| 3 |
-
from controlnet_aux import
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
print("Lade
|
| 7 |
|
| 8 |
-
#
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
_facefix_pipe
|
| 32 |
-
|
| 33 |
-
def
|
| 34 |
-
global _facefix_pipe
|
| 35 |
-
if _facefix_pipe is None:
|
| 36 |
-
print(f"Lade Face-Fix-Pipeline für Modell: {model_id}")
|
| 37 |
-
_facefix_pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
| 38 |
-
model_id,
|
| 39 |
-
controlnet=[controlnet_face, controlnet_depth],
|
| 40 |
-
torch_dtype=torch.float16,
|
| 41 |
-
safety_checker=None,
|
| 42 |
-
).to("cuda")
|
| 43 |
-
|
| 44 |
-
# Nur wenn verfügbar
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
print("XFormers nicht verfügbar, überspringe...")
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
except:
|
| 53 |
-
print("CPU Offload nicht verfügbar, überspringe...")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
except Exception as e:
|
| 81 |
-
print(f"Face-Fix Fehler: {e}")
|
| 82 |
-
# Bei Fehler das Originalbild zurückgeben
|
| 83 |
return image
|
|
|
|
| 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("Lade Face-Fix System (Depth-basiert)...")
|
| 8 |
|
| 9 |
+
# Vereinfachte Version NUR mit Depth
|
| 10 |
try:
|
| 11 |
+
depth_processor = ZoeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 12 |
+
print("✅ Depth Processor geladen")
|
| 13 |
+
|
| 14 |
+
controlnet_depth = ControlNetModel.from_pretrained(
|
| 15 |
+
"lllyasviel/control_v11f1e_sd15_depth",
|
| 16 |
+
torch_dtype=torch.float16
|
| 17 |
+
).to("cuda")
|
| 18 |
+
print("✅ ControlNet Depth geladen")
|
| 19 |
+
|
| 20 |
+
_facefix_pipe = None
|
| 21 |
+
|
| 22 |
+
def _get_facefix_pipeline(model_id: str):
|
| 23 |
+
global _facefix_pipe
|
| 24 |
+
if _facefix_pipe is None:
|
| 25 |
+
print(f"Lade Face-Fix-Pipeline für Modell: {model_id}")
|
| 26 |
+
_facefix_pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
| 27 |
+
model_id,
|
| 28 |
+
controlnet=[controlnet_depth],
|
| 29 |
+
torch_dtype=torch.float16,
|
| 30 |
+
safety_checker=None,
|
| 31 |
+
).to("cuda")
|
| 32 |
+
return _facefix_pipe
|
| 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 |
+
fixed = pipe(
|
| 43 |
+
prompt=face_prompt,
|
| 44 |
+
negative_prompt=face_negative,
|
| 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"Face-Fix Error: {e}")
|
| 59 |
+
return image
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"❌ Face-Fix Setup fehlgeschlagen: {e}")
|
| 63 |
+
|
| 64 |
+
# Fallback: Dummy-Funktion die nichts macht
|
| 65 |
+
def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
|
| 66 |
+
print("⚠️ Face-Fix nicht verfügbar, gebe Original zurück")
|
|
|
|
|
|
|
|
|
|
| 67 |
return image
|