Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,79 +2,68 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
def refine_edges(
|
| 8 |
"""
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
np_img = np.array(img)
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# 4. Apply blur only to edge zone
|
| 32 |
-
blurred_edge = cv2.GaussianBlur(edge_zone, (blur_kernel, blur_kernel), 0)
|
| 33 |
-
|
| 34 |
-
# 5. Feathering with thresholding to remove leftover bg
|
| 35 |
-
if feather_amount > 0:
|
| 36 |
-
edge_mask = (blurred_edge > threshold * 255).astype(np.uint8) * 255
|
| 37 |
-
edge_mask = cv2.erode(edge_mask, np.ones((feather_amount, feather_amount), np.uint8))
|
| 38 |
-
final_edge = cv2.GaussianBlur(edge_mask, (blur_kernel, blur_kernel), 0)
|
| 39 |
-
else:
|
| 40 |
-
final_edge = blurred_edge
|
| 41 |
-
|
| 42 |
-
# 6. Combine with strict alpha
|
| 43 |
-
new_alpha = cv2.bitwise_or(strict_alpha, final_edge)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
| 52 |
|
|
|
|
|
|
|
| 53 |
return Image.fromarray(result)
|
| 54 |
|
| 55 |
-
# Gradio
|
| 56 |
-
with gr.Blocks(
|
| 57 |
-
gr.Markdown(""
|
| 58 |
-
# ✨ Advanced Edge Refiner
|
| 59 |
-
Removes leftover background artifacts around hair and fine edges
|
| 60 |
-
""")
|
| 61 |
-
|
| 62 |
with gr.Row():
|
| 63 |
with gr.Column():
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
threshold = gr.Slider(0, 100, value=10, label="Edge Threshold (%)")
|
| 69 |
-
submit_btn = gr.Button("Refine Edges", variant="primary")
|
| 70 |
-
|
| 71 |
with gr.Column():
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
fn=refine_edges,
|
| 76 |
-
inputs=[
|
| 77 |
-
outputs=
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
def refine_edges(img, edge_aggressiveness=3, bg_removal_strength=50):
|
| 7 |
"""
|
| 8 |
+
Advanced edge refinement using:
|
| 9 |
+
1. Alpha matte estimation
|
| 10 |
+
2. Guided filtering
|
| 11 |
+
3. Color decontamination
|
| 12 |
"""
|
| 13 |
+
# Convert to numpy array
|
| 14 |
+
np_img = np.array(img.convert("RGBA"))
|
| 15 |
+
rgb = np_img[..., :3].astype(np.float32) / 255.0
|
| 16 |
+
alpha = np_img[..., 3].astype(np.float32) / 255.0
|
| 17 |
|
| 18 |
+
# 1. Create trimap from alpha
|
| 19 |
+
trimap = np.zeros_like(alpha)
|
| 20 |
+
trimap[alpha > 0.95] = 1 # Definite foreground
|
| 21 |
+
trimap[alpha < 0.05] = 0 # Definite background
|
| 22 |
+
trimap[(alpha >= 0.05) & (alpha <= 0.95)] = 0.5 # Unknown area
|
| 23 |
|
| 24 |
+
# 2. Estimate foreground/background colors
|
| 25 |
+
fg = rgb * (trimap == 1)[..., None]
|
| 26 |
+
bg = rgb * (trimap == 0)[..., None]
|
| 27 |
|
| 28 |
+
# 3. Guided filter for alpha refinement
|
| 29 |
+
radius = edge_aggressiveness * 5
|
| 30 |
+
eps = 0.01
|
| 31 |
+
refined_alpha = cv2.ximgproc.guidedFilter(
|
| 32 |
+
guide=rgb,
|
| 33 |
+
src=alpha,
|
| 34 |
+
radius=radius,
|
| 35 |
+
eps=eps
|
| 36 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# 4. Color decontamination
|
| 39 |
+
bg_removal = bg_removal_strength / 100.0
|
| 40 |
+
new_rgb = (rgb - bg_removal * bg) / (1 - bg_removal * (1 - refined_alpha[..., None]))
|
| 41 |
+
new_rgb = np.clip(new_rgb, 0, 1)
|
| 42 |
|
| 43 |
+
# 5. Final alpha thresholding
|
| 44 |
+
final_alpha = np.clip(refined_alpha * 255, 0, 255).astype(np.uint8)
|
| 45 |
+
new_rgb = (new_rgb * 255).astype(np.uint8)
|
| 46 |
|
| 47 |
+
# Combine channels
|
| 48 |
+
result = np.concatenate([new_rgb, final_alpha[..., None]], axis=-1)
|
| 49 |
return Image.fromarray(result)
|
| 50 |
|
| 51 |
+
# Gradio Interface
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("## ✂️ Advanced Edge Refiner")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
+
img_input = gr.Image(type="pil", label="Input PNG")
|
| 57 |
+
edge_slider = gr.Slider(1, 10, value=3, label="Edge Precision")
|
| 58 |
+
bg_slider = gr.Slider(1, 100, value=50, label="BG Removal Strength")
|
| 59 |
+
process_btn = gr.Button("Refine Edges")
|
|
|
|
|
|
|
|
|
|
| 60 |
with gr.Column():
|
| 61 |
+
img_output = gr.Image(type="pil", label="Refined Result")
|
| 62 |
|
| 63 |
+
process_btn.click(
|
| 64 |
fn=refine_edges,
|
| 65 |
+
inputs=[img_input, edge_slider, bg_slider],
|
| 66 |
+
outputs=img_output
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|