SynthAIzer commited on
Commit
0118b63
·
verified ·
1 Parent(s): 7842ff6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -1,32 +1,24 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from PIL import Image
4
-
5
- # -------------------------------
6
- # Load Hugging Face background removal model
7
- # -------------------------------
8
- pipe = pipeline(
9
- task="image-segmentation",
10
- model="briaai/RMBG-1.4",
11
- trust_remote_code=True
12
- )
13
 
14
  # -------------------------------
15
  # Function to remove background
16
  # -------------------------------
17
  def remove_bg(image: Image.Image):
18
- # Run model this returns a PIL mask directly
19
- mask = pipe(image)
 
 
20
 
21
- # Ensure mask is grayscale
22
- mask = mask.convert("L")
23
 
24
- # Apply mask to original image
25
- image = image.convert("RGBA")
26
- mask = mask.resize(image.size)
27
- image.putalpha(mask)
28
 
29
- return image
30
 
31
  # -------------------------------
32
  # Gradio UI
@@ -36,7 +28,7 @@ demo = gr.Interface(
36
  inputs=gr.Image(type="pil", label="Upload an image"),
37
  outputs=gr.Image(type="pil", label="Background Removed"),
38
  title="Background Remover",
39
- description="Upload an image to remove its background using Hugging Face RMBG model.",
40
  )
41
 
42
  # -------------------------------
 
1
  import gradio as gr
2
+ from rembg import remove
3
  from PIL import Image
4
+ import io
 
 
 
 
 
 
 
 
5
 
6
  # -------------------------------
7
  # Function to remove background
8
  # -------------------------------
9
  def remove_bg(image: Image.Image):
10
+ # Convert PIL image to bytes
11
+ img_byte_arr = io.BytesIO()
12
+ image.save(img_byte_arr, format='PNG')
13
+ img_bytes = img_byte_arr.getvalue()
14
 
15
+ # Remove background using rembg
16
+ output_bytes = remove(img_bytes)
17
 
18
+ # Convert bytes back to PIL image
19
+ output_image = Image.open(io.BytesIO(output_bytes))
 
 
20
 
21
+ return output_image
22
 
23
  # -------------------------------
24
  # Gradio UI
 
28
  inputs=gr.Image(type="pil", label="Upload an image"),
29
  outputs=gr.Image(type="pil", label="Background Removed"),
30
  title="Background Remover",
31
+ description="Upload an image to remove its background using the rembg library.",
32
  )
33
 
34
  # -------------------------------