Janeka commited on
Commit
5a72035
·
verified ·
1 Parent(s): 25a04e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -25
app.py CHANGED
@@ -3,39 +3,32 @@ from rembg import remove
3
  from PIL import Image, ImageDraw
4
  import io
5
 
6
- def remove_bg(image, points):
7
  if image is None:
8
  return None
 
9
 
10
- # Load image
11
- image = Image.open(image).convert("RGBA")
12
-
13
- # Convert points format
14
- keep_points = [tuple(p["coordinates"]) for p in points if p["label"] == "keep"]
15
- remove_points = [tuple(p["coordinates"]) for p in points if p["label"] == "remove"]
16
-
17
- # Apply automatic background removal
18
  output = remove(image)
19
 
20
- # Apply manual refinement (simple demonstration using circles)
21
- draw = ImageDraw.Draw(output)
22
- for point in keep_points:
23
- draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill=(255, 255, 255, 255))
24
- for point in remove_points:
25
- draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill=(0, 0, 0, 0))
26
 
27
  return output
28
 
29
  # Creating Gradio interface
30
- iface = gr.Interface(
31
- fn=remove_bg,
32
- inputs=[
33
- gr.Image(type="filepath"),
34
- gr.AnnotatedImage(choices=["keep", "remove"], label="Refine Background")
35
- ],
36
- outputs=gr.Image(type="pil"),
37
- title="AI Background Remover with Refinement",
38
- description="Upload an image, remove its background, and refine the result using + (keep) and - (remove) points!",
39
- )
 
40
 
41
  iface.launch()
 
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:
23
+ gr.Markdown("# AI Background Remover with Refinement")
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()