Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,39 @@
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
-
import gradio as gr
|
| 5 |
|
| 6 |
-
def refine_edges(
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
|
| 52 |
-
|
| 53 |
-
gr.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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__":
|
| 70 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
+
def refine_edges(image: Image.Image):
|
| 7 |
+
# Convert PIL Image to NumPy array
|
| 8 |
+
image_np = np.array(image)
|
| 9 |
+
|
| 10 |
+
# Check if image has alpha channel
|
| 11 |
+
if image_np.shape[2] != 4:
|
| 12 |
+
return image # Return original if no alpha channel
|
| 13 |
+
|
| 14 |
+
# Separate color and alpha channels
|
| 15 |
+
b, g, r, a = cv2.split(image_np)
|
| 16 |
+
|
| 17 |
+
# Apply Gaussian blur to alpha channel
|
| 18 |
+
a_blurred = cv2.GaussianBlur(a, (5, 5), 0)
|
| 19 |
+
|
| 20 |
+
# Apply morphological operations to smooth edges
|
| 21 |
+
kernel = np.ones((3, 3), np.uint8)
|
| 22 |
+
a_morph = cv2.morphologyEx(a_blurred, cv2.MORPH_OPEN, kernel)
|
| 23 |
+
|
| 24 |
+
# Merge channels back
|
| 25 |
+
result = cv2.merge((b, g, r, a_morph))
|
| 26 |
+
|
| 27 |
+
# Convert back to PIL Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return Image.fromarray(result)
|
| 29 |
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=refine_edges,
|
| 32 |
+
inputs=gr.Image(type="pil", image_mode="RGBA", label="Upload Transparent PNG"),
|
| 33 |
+
outputs=gr.Image(type="pil", image_mode="RGBA", label="Refined Image"),
|
| 34 |
+
title="Edge Smoother",
|
| 35 |
+
description="Upload a transparent PNG image to refine its edges, especially around hair and fur."
|
| 36 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
iface.launch()
|