Janeka commited on
Commit
d76bdf0
·
verified ·
1 Parent(s): 8c9ecb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -11,19 +11,24 @@ def remove_bg(image):
11
  output = remove(image)
12
  return output
13
 
14
- def refine_bg(image, mask, threshold):
15
- if image is None or mask is None:
16
  return None
17
 
18
  image = Image.open(image).convert("RGBA")
19
- mask = Image.open(mask).convert("L")
 
 
 
 
 
 
20
 
21
  refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
22
  return refined
23
 
24
- # Creating Gradio interface
25
  with gr.Blocks() as iface:
26
- gr.Markdown("# AI Background Remover with Inline Refinement")
27
 
28
  with gr.Row():
29
  input_img = gr.Image(type="filepath", label="Upload Image")
@@ -32,18 +37,20 @@ with gr.Blocks() as iface:
32
  remove_btn = gr.Button("Remove Background")
33
  remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
34
 
35
- refine_section = gr.Column(visible=False)
36
- with refine_section:
 
 
 
37
  gr.Markdown("### Refine Background")
38
- refine_editor = gr.Image(type="filepath", label="Edit Mask (White to Keep, Black to Remove)")
39
  threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
40
- refine_btn = gr.Button("Apply Refinements")
41
- refine_btn.click(refine_bg, inputs=[input_img, refine_editor, threshold_slider], outputs=output_img)
42
 
43
  def show_refine_section(image):
44
  return gr.update(visible=True)
45
 
46
- refine_toggle_btn = gr.Button("Refine")
47
- refine_toggle_btn.click(show_refine_section, inputs=input_img, outputs=refine_section)
48
 
49
- iface.launch()
 
11
  output = remove(image)
12
  return output
13
 
14
+ def refine_bg(image, points, threshold):
15
+ if image is None:
16
  return None
17
 
18
  image = Image.open(image).convert("RGBA")
19
+ mask = Image.new("L", image.size, 0)
20
+ draw = ImageDraw.Draw(mask)
21
+
22
+ for point in points:
23
+ x, y, mode = point
24
+ color = 255 if mode == "keep" else 0
25
+ draw.ellipse((x - threshold * 10, y - threshold * 10, x + threshold * 10, y + threshold * 10), fill=color)
26
 
27
  refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
28
  return refined
29
 
 
30
  with gr.Blocks() as iface:
31
+ gr.Markdown("# AI Background Remover with Refinement")
32
 
33
  with gr.Row():
34
  input_img = gr.Image(type="filepath", label="Upload Image")
 
37
  remove_btn = gr.Button("Remove Background")
38
  remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
39
 
40
+ with gr.Row():
41
+ refine_btn = gr.Button("Refine")
42
+ refine_panel = gr.Column(visible=False)
43
+
44
+ with refine_panel:
45
  gr.Markdown("### Refine Background")
46
+ refine_editor = gr.AnnotatedImage(label="Tap to Add Points (+ to Keep, - to Remove)")
47
  threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
48
+ apply_refine_btn = gr.Button("Apply Refinements")
49
+ apply_refine_btn.click(refine_bg, inputs=[input_img, refine_editor, threshold_slider], outputs=output_img)
50
 
51
  def show_refine_section(image):
52
  return gr.update(visible=True)
53
 
54
+ refine_btn.click(show_refine_section, inputs=input_img, outputs=refine_panel)
 
55
 
56
+ iface.launch()