Janeka commited on
Commit
39624be
·
verified ·
1 Parent(s): 7910b4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -20
app.py CHANGED
@@ -11,28 +11,17 @@ def remove_bg(image):
11
  output = remove(image)
12
  return output
13
 
14
- def refine_bg(image, annotations, threshold):
15
- if image is None:
16
  return None
17
  image = Image.open(image).convert("RGBA")
18
- mask = Image.new("L", image.size, 0)
19
- draw = ImageDraw.Draw(mask)
20
-
21
- for annotation in annotations:
22
- points = annotation["points"]
23
- label = annotation["label"]
24
-
25
- if label == "keep":
26
- draw.polygon(points, fill=255)
27
- else: # label == "remove"
28
- draw.polygon(points, fill=0)
29
-
30
  refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
31
  return refined
32
 
33
  # Creating Gradio interface
34
  with gr.Blocks() as iface:
35
- gr.Markdown("# AI Background Remover with Refinement")
36
 
37
  with gr.Row():
38
  input_img = gr.Image(type="filepath", label="Upload Image")
@@ -41,10 +30,10 @@ with gr.Blocks() as iface:
41
  remove_btn = gr.Button("Remove Background")
42
  remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
43
 
44
- with gr.Accordion("Edit Background Refinement", open=False):
45
- annotator = gr.AnnotatedImage(label="Refine Background")
46
- threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
47
- refine_btn = gr.Button("Apply Refinements")
48
- refine_btn.click(refine_bg, inputs=[input_img, annotator, threshold_slider], outputs=output_img)
49
 
50
  iface.launch()
 
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
  image = Image.open(image).convert("RGBA")
18
+ mask = Image.open(mask).convert("L")
 
 
 
 
 
 
 
 
 
 
 
19
  refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
20
  return refined
21
 
22
  # Creating Gradio interface
23
  with gr.Blocks() as iface:
24
+ gr.Markdown("# AI Background Remover with Inline Refinement")
25
 
26
  with gr.Row():
27
  input_img = gr.Image(type="filepath", label="Upload Image")
 
30
  remove_btn = gr.Button("Remove Background")
31
  remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
32
 
33
+ gr.Markdown("### Refine Background Directly on Image")
34
+ mask_editor = gr.Image(type="filepath", label="Draw on Image (Use + for Keep, - for Remove)", tool="sketch")
35
+ threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
36
+ refine_btn = gr.Button("Apply Refinements")
37
+ refine_btn.click(refine_bg, inputs=[input_img, mask_editor, threshold_slider], outputs=output_img)
38
 
39
  iface.launch()