Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from ultralytics import YOLO
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
import torch
|
| 9 |
import os
|
| 10 |
-
from PIL import Image
|
| 11 |
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
model_path = "https://huggingface.co/DreamingOracle/Quagmaform_alpha-1/blob/main/DPS_Quagmaform_Alpha1.safetensors" # Your custom model file URL
|
|
@@ -51,24 +51,66 @@ def infer(
|
|
| 51 |
prompt=prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, width=width, height=height, generator=generator,
|
| 52 |
).images[0]
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Save in selected format for download consistency
|
| 73 |
output_path = f"generated_image.{save_format}"
|
| 74 |
image.save(output_path, format=save_format.upper())
|
|
@@ -131,4 +173,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 131 |
outputs=[result, seed],
|
| 132 |
)
|
| 133 |
if __name__ == "__main__":
|
| 134 |
-
demo.launch()
|
|
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
import torch
|
| 9 |
import os
|
| 10 |
+
from PIL import Image, ImageFilter, ImageOps
|
| 11 |
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
model_path = "https://huggingface.co/DreamingOracle/Quagmaform_alpha-1/blob/main/DPS_Quagmaform_Alpha1.safetensors" # Your custom model file URL
|
|
|
|
| 51 |
prompt=prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, width=width, height=height, generator=generator,
|
| 52 |
).images[0]
|
| 53 |
|
| 54 |
+
# ---------------------------
|
| 55 |
+
# ADetailer post-processing for face enhancement (with padding + soft blend)
|
| 56 |
+
# ---------------------------
|
| 57 |
+
try:
|
| 58 |
+
results = adetailer_model(image)
|
| 59 |
+
# ultralytics YOLO results typically have results[0].boxes
|
| 60 |
+
if results and len(results) and getattr(results[0], "boxes", None):
|
| 61 |
+
for box in results[0].boxes:
|
| 62 |
+
# extract bbox (xyxy)
|
| 63 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 64 |
+
|
| 65 |
+
# 1) Pad bbox so inpaint has surrounding context
|
| 66 |
+
w = max(1, x2 - x1)
|
| 67 |
+
h = max(1, y2 - y1)
|
| 68 |
+
pad = int(max(10, 0.18 * max(w, h))) # ~18% padding, tweak if desired
|
| 69 |
+
x1p = max(0, x1 - pad)
|
| 70 |
+
y1p = max(0, y1 - pad)
|
| 71 |
+
x2p = min(image.width, x2 + pad)
|
| 72 |
+
y2p = min(image.height, y2 + pad)
|
| 73 |
+
|
| 74 |
+
face = image.crop((x1p, y1p, x2p, y2p))
|
| 75 |
+
|
| 76 |
+
# 2) Make dimensions divisible by 8 (many pipelines prefer multiples of 8)
|
| 77 |
+
fw, fh = face.size
|
| 78 |
+
fw8 = max(8, (fw // 8) * 8)
|
| 79 |
+
fh8 = max(8, (fh // 8) * 8)
|
| 80 |
+
if (fw8, fh8) != (fw, fh):
|
| 81 |
+
face = face.resize((fw8, fh8), Image.LANCZOS)
|
| 82 |
+
|
| 83 |
+
# 3) Create a full white mask for inpainting (255 = area to replace)
|
| 84 |
+
mask = Image.new("L", face.size, 255)
|
| 85 |
+
|
| 86 |
+
# 4) Create a blurred mask for soft blending when pasting back
|
| 87 |
+
blur_radius = max(4, int(min(face.size) / 10)) # heuristic; adjust if needed
|
| 88 |
+
paste_mask = mask.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 89 |
+
|
| 90 |
+
# 5) Inpaint on the crop — use gentler strength so output doesn't look pasted
|
| 91 |
+
# (this re-uses your existing `pipe` call which previously worked for you)
|
| 92 |
+
inpaint_result = pipe(
|
| 93 |
+
prompt=prompt + ", high detail face",
|
| 94 |
+
image=face,
|
| 95 |
+
mask_image=mask,
|
| 96 |
+
strength=0.45, # reduced from 0.75 to lower mismatch
|
| 97 |
+
num_inference_steps=20,
|
| 98 |
+
guidance_scale=7.5,
|
| 99 |
+
generator=generator
|
| 100 |
+
).images[0]
|
| 101 |
+
|
| 102 |
+
# 6) Optionally attempt a simple color-match (blend) if lighting differs.
|
| 103 |
+
# Here we keep it simple and rely on the blurred mask to smooth edges.
|
| 104 |
+
# Ensure paste_mask is 'L'
|
| 105 |
+
if paste_mask.mode != "L":
|
| 106 |
+
paste_mask = paste_mask.convert("L")
|
| 107 |
+
|
| 108 |
+
# 7) Paste the inpainted crop back using the soft mask for feathered edges
|
| 109 |
+
image.paste(inpaint_result, (x1p, y1p), paste_mask)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
# Log exception to console for debugging but don't crash the whole run
|
| 112 |
+
print("ADetailer post-process failed:", e)
|
| 113 |
+
|
| 114 |
# Save in selected format for download consistency
|
| 115 |
output_path = f"generated_image.{save_format}"
|
| 116 |
image.save(output_path, format=save_format.upper())
|
|
|
|
| 173 |
outputs=[result, seed],
|
| 174 |
)
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
demo.launch()
|