Update controlnet_facefix.py
Browse files- controlnet_facefix.py +47 -24
controlnet_facefix.py
CHANGED
|
@@ -5,14 +5,20 @@ from PIL import Image
|
|
| 5 |
|
| 6 |
print("Lade OpenPose_faceonly + Depth für perfekte Gesichter...")
|
| 7 |
|
| 8 |
-
# Preprocessors (einmalig laden)
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
depth_processor = ZoeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 11 |
|
| 12 |
# ControlNet Modelle (bleiben im VRAM)
|
| 13 |
controlnet_face = ControlNetModel.from_pretrained(
|
| 14 |
"lllyasviel/control_v11p_sd15_openpose",
|
| 15 |
-
subfolder="faceonly",
|
| 16 |
torch_dtype=torch.float16
|
| 17 |
).to("cuda")
|
| 18 |
|
|
@@ -34,27 +40,44 @@ def _get_facefix_pipeline(model_id: str):
|
|
| 34 |
torch_dtype=torch.float16,
|
| 35 |
safety_checker=None,
|
| 36 |
).to("cuda")
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return _facefix_pipe
|
| 40 |
|
| 41 |
def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
print("Lade OpenPose_faceonly + Depth für perfekte Gesichter...")
|
| 7 |
|
| 8 |
+
# Preprocessors (einmalig laden) - KORRIGIERTE VERSION
|
| 9 |
+
try:
|
| 10 |
+
# Moderne Version
|
| 11 |
+
openpose_face = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 12 |
+
except:
|
| 13 |
+
# Alternative/ältere Version
|
| 14 |
+
from controlnet_aux.open_pose import OpenposeDetector
|
| 15 |
+
openpose_face = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 16 |
+
|
| 17 |
depth_processor = ZoeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 18 |
|
| 19 |
# ControlNet Modelle (bleiben im VRAM)
|
| 20 |
controlnet_face = ControlNetModel.from_pretrained(
|
| 21 |
"lllyasviel/control_v11p_sd15_openpose",
|
|
|
|
| 22 |
torch_dtype=torch.float16
|
| 23 |
).to("cuda")
|
| 24 |
|
|
|
|
| 40 |
torch_dtype=torch.float16,
|
| 41 |
safety_checker=None,
|
| 42 |
).to("cuda")
|
| 43 |
+
|
| 44 |
+
# Nur wenn verfügbar
|
| 45 |
+
try:
|
| 46 |
+
_facefix_pipe.enable_xformers_memory_efficient_attention()
|
| 47 |
+
except:
|
| 48 |
+
print("XFormers nicht verfügbar, überspringe...")
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
_facefix_pipe.enable_model_cpu_offload() # spart ~2 GB!
|
| 52 |
+
except:
|
| 53 |
+
print("CPU Offload nicht verfügbar, überspringe...")
|
| 54 |
+
|
| 55 |
return _facefix_pipe
|
| 56 |
|
| 57 |
def apply_facefix(image: Image.Image, prompt: str, negative_prompt: str, seed: int, model_id: str):
|
| 58 |
+
try:
|
| 59 |
+
pipe = _get_facefix_pipeline(model_id)
|
| 60 |
+
|
| 61 |
+
# Control-Images erzeugen
|
| 62 |
+
pose_img = openpose_face(image)
|
| 63 |
+
depth_img = depth_processor(image)
|
| 64 |
+
|
| 65 |
+
fixed = pipe(
|
| 66 |
+
prompt=prompt,
|
| 67 |
+
negative_prompt=negative_prompt,
|
| 68 |
+
image=image,
|
| 69 |
+
mask_image=None, # Keine Maske für Face-Fix
|
| 70 |
+
control_image=[pose_img, depth_img],
|
| 71 |
+
controlnet_conditioning_scale=[0.85, 0.60],
|
| 72 |
+
strength=0.42,
|
| 73 |
+
num_inference_steps=20,
|
| 74 |
+
guidance_scale=7.0,
|
| 75 |
+
generator=torch.Generator("cuda").manual_seed(seed),
|
| 76 |
+
).images[0]
|
| 77 |
+
|
| 78 |
+
return fixed
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Face-Fix Fehler: {e}")
|
| 82 |
+
# Bei Fehler das Originalbild zurückgeben
|
| 83 |
+
return image
|