Janeka commited on
Commit
7910b4e
·
verified ·
1 Parent(s): 5a72035

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -1,22 +1,34 @@
1
  import gradio as gr
2
  from rembg import remove
3
  from PIL import Image, ImageDraw
 
4
  import io
5
 
6
- def remove_bg(image, mask=None):
7
  if image is None:
8
  return None
9
  image = Image.open(image)
10
-
11
- # Apply background removal
12
  output = remove(image)
 
 
 
 
 
 
 
 
13
 
14
- # Apply manual mask refinements if provided
15
- if mask:
16
- mask_image = Image.open(mask).convert("L")
17
- output.paste(image, mask=mask_image)
 
 
 
 
18
 
19
- return output
 
20
 
21
  # Creating Gradio interface
22
  with gr.Blocks() as iface:
@@ -24,11 +36,15 @@ with gr.Blocks() as iface:
24
 
25
  with gr.Row():
26
  input_img = gr.Image(type="filepath", label="Upload Image")
27
- mask_img = gr.Image(type="filepath", label="Upload Mask (Optional)")
28
 
29
- output_img = gr.Image(type="pil", label="Output Image")
 
30
 
31
- process_btn = gr.Button("Remove Background")
32
- process_btn.click(remove_bg, inputs=[input_img, mask_img], outputs=output_img)
 
 
 
33
 
34
  iface.launch()
 
1
  import gradio as gr
2
  from rembg import remove
3
  from PIL import Image, ImageDraw
4
+ import numpy as np
5
  import io
6
 
7
+ def remove_bg(image):
8
  if image is None:
9
  return None
10
  image = Image.open(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:
 
36
 
37
  with gr.Row():
38
  input_img = gr.Image(type="filepath", label="Upload Image")
39
+ output_img = gr.Image(type="pil", label="Output Image")
40
 
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()