Janeka commited on
Commit
ebfc1e7
·
verified ·
1 Parent(s): f1139b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -62
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(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__":
70
- demo.launch()
 
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()