Janeka commited on
Commit
94e9c31
·
verified ·
1 Parent(s): d76bdf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -11,7 +11,7 @@ def remove_bg(image):
11
  output = remove(image)
12
  return output
13
 
14
- def refine_bg(image, points, threshold):
15
  if image is None:
16
  return None
17
 
@@ -20,7 +20,7 @@ def refine_bg(image, points, threshold):
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
 
@@ -28,7 +28,7 @@ def refine_bg(image, points, threshold):
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,20 +37,32 @@ with gr.Blocks() as iface:
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()
 
11
  output = remove(image)
12
  return output
13
 
14
+ def refine_bg(image, points, threshold, mode):
15
  if image is None:
16
  return None
17
 
 
20
  draw = ImageDraw.Draw(mask)
21
 
22
  for point in points:
23
+ x, y = 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
 
 
28
  return refined
29
 
30
  with gr.Blocks() as iface:
31
+ gr.Markdown("# AI Background Remover")
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.Column():
41
  refine_btn = gr.Button("Refine")
42
+ refine_options = gr.Row(visible=False)
43
 
44
+ with refine_options:
45
  gr.Markdown("### Refine Background")
46
+ refine_editor = gr.AnnotatedImage(label="Tap to Add Points")
47
+ keep_btn = gr.Button("+")
48
+ remove_btn = gr.Button("-")
49
  threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
50
  apply_refine_btn = gr.Button("Apply Refinements")
51
+
52
+ refine_mode = gr.State("keep")
53
 
54
  def show_refine_section(image):
55
  return gr.update(visible=True)
56
 
57
+ def set_mode_keep():
58
+ return "keep"
59
+
60
+ def set_mode_remove():
61
+ return "remove"
62
+
63
+ refine_btn.click(show_refine_section, inputs=input_img, outputs=refine_options)
64
+ keep_btn.click(set_mode_keep, outputs=refine_mode)
65
+ remove_btn.click(set_mode_remove, outputs=refine_mode)
66
+ apply_refine_btn.click(refine_bg, inputs=[input_img, refine_editor, threshold_slider, refine_mode], outputs=output_img)
67
 
68
+ iface.launch()