rdjarbeng commited on
Commit
5c0ed2c
·
1 Parent(s): b1bcf24

add more options bg color, matting...

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -1,19 +1,32 @@
1
  import gradio as gr
2
- from rembg import remove
3
  from PIL import Image
 
4
  import logging
5
 
6
  # Set up logging
7
  logging.basicConfig(level=logging.INFO)
8
 
9
- def remove_background(input_image):
10
  try:
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Remove the background
12
- output_image = remove(input_image)
13
  logging.info("Background removed")
14
 
15
  # Convert to RGB mode if necessary
16
- if output_image.mode != 'RGB':
17
  output_image = output_image.convert('RGB')
18
  logging.info("Converted to RGB mode")
19
 
@@ -31,13 +44,20 @@ def remove_background(input_image):
31
  # Gradio interface
32
  iface = gr.Interface(
33
  fn=remove_background,
34
- inputs=gr.Image(type="pil"),
 
 
 
 
 
 
 
35
  outputs=[
36
  gr.Image(type="pil", label="Output Image"),
37
  gr.File(label="Download the output image")
38
  ],
39
- title="Background Remover",
40
- description="Upload an image to remove the background. You can view the result and download it.",
41
  allow_flagging="never",
42
  )
43
 
 
1
  import gradio as gr
2
+ from rembg import remove, new_session
3
  from PIL import Image
4
+ import numpy as np
5
  import logging
6
 
7
  # Set up logging
8
  logging.basicConfig(level=logging.INFO)
9
 
10
+ def remove_background(input_image, bg_color, model_name, alpha_matting, post_process_mask, only_mask):
11
  try:
12
+ # Set up the session with the chosen model
13
+ session = new_session(model_name) if model_name else None
14
+
15
+ # Prepare additional options
16
+ remove_options = {
17
+ "session": session,
18
+ "bgcolor": bg_color if bg_color else None,
19
+ "alpha_matting": alpha_matting,
20
+ "post_process_mask": post_process_mask,
21
+ "only_mask": only_mask
22
+ }
23
+
24
  # Remove the background
25
+ output_image = remove(input_image, **{k: v for k, v in remove_options.items() if v is not None})
26
  logging.info("Background removed")
27
 
28
  # Convert to RGB mode if necessary
29
+ if not only_mask and output_image.mode != 'RGB':
30
  output_image = output_image.convert('RGB')
31
  logging.info("Converted to RGB mode")
32
 
 
44
  # Gradio interface
45
  iface = gr.Interface(
46
  fn=remove_background,
47
+ inputs=[
48
+ gr.Image(type="pil"),
49
+ gr.ColorPicker(label="Background Color", value=None), # Background color picker
50
+ gr.Dropdown(choices=["u2net", "isnet-general-use", "unet"], label="Model Selection", default="u2net"),
51
+ gr.Checkbox(label="Enable Alpha Matting", default=False),
52
+ gr.Checkbox(label="Post-Process Mask", default=False),
53
+ gr.Checkbox(label="Only Return Mask", default=False)
54
+ ],
55
  outputs=[
56
  gr.Image(type="pil", label="Output Image"),
57
  gr.File(label="Download the output image")
58
  ],
59
+ title="Advanced Background Remover",
60
+ description="Upload an image to remove the background. Customize the result with different options, including background color, model selection, alpha matting, and more.",
61
  allow_flagging="never",
62
  )
63