Janeka commited on
Commit
25a04e5
·
verified ·
1 Parent(s): 2803aaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -1,22 +1,41 @@
1
  import gradio as gr
2
  from rembg import remove
3
- from PIL import Image
 
4
 
5
- def remove_bg(image_path):
6
- if image_path is None:
7
  return None
8
- image = Image.open(image_path) # ✅ Open the image from the file path
 
 
 
 
 
 
 
 
9
  output = remove(image)
 
 
 
 
 
 
 
 
10
  return output
11
 
12
  # Creating Gradio interface
13
  iface = gr.Interface(
14
  fn=remove_bg,
15
- inputs=gr.Image(type="filepath"), # ✅ Make sure Gradio passes the file path
 
 
 
16
  outputs=gr.Image(type="pil"),
17
- title="AI Background Remover",
18
- description="Upload an image and remove its background automatically!",
19
  )
20
 
21
  iface.launch()
22
-
 
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, 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()