leonelhs commited on
Commit
fdb0069
Β·
1 Parent(s): 5aff4e7

Minimal code implementation

Browse files
Files changed (5) hide show
  1. README.md +5 -4
  2. app.py +78 -123
  3. notes.txt +3 -0
  4. old_app.py +132 -0
  5. requirements.txt +2 -7
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
  title: Rembg
3
- emoji: πŸ¦€
4
- colorFrom: pink
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Rembg
3
+ emoji: πŸ‘€
4
+ colorFrom: yellow
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 5.46.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ short_description: 'Remove images background tool'
12
  ---
13
 
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,132 +1,87 @@
1
- import logging
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import gradio as gr
 
 
4
  from rembg import new_session
 
5
 
6
- from cutter import remove, make_label
7
- from utils import *
8
-
9
- remove_bg_models = {
10
- "U2NET": "u2net",
11
- "U2NET Human Seg": "u2net_human_seg",
12
- "U2NET Cloth Seg": "u2net_cloth_seg"
13
  }
14
 
15
- model_choices = keys(remove_bg_models)
16
-
17
-
18
- def predict(image, session, smoot, matting, bg_color):
19
-
20
- session = new_session(remove_bg_models[session])
21
-
22
- try:
23
- return remove(session, image, smoot, matting, bg_color)
24
- except ValueError as err:
25
- logging.error(err)
26
- return make_label(str(err)), None
27
-
28
-
29
- def change_show_mask(chk_state):
30
- return gr.Image(visible=chk_state)
31
-
32
-
33
- def change_include_matting(chk_state):
34
- return gr.Group(visible=chk_state), (0, 0, 0), 0, 0, 0
35
-
36
-
37
- def change_foreground_threshold(fg_value, value):
38
- fg, bg, erode = value
39
- return fg_value, bg, erode
40
-
41
-
42
- def change_background_threshold(bg_value, value):
43
- fg, bg, erode = value
44
- return fg, bg_value, erode
45
-
46
-
47
- def change_erode_size(erode_value, value):
48
- fg, bg, erode = value
49
- return fg, bg, erode_value
50
-
51
-
52
- def set_dominant_color(chk_state):
53
- return chk_state, gr.ColorPicker(visible=not chk_state)
54
-
55
-
56
- def change_picker_color(picker, dominant):
57
- if not dominant:
58
- return picker
59
- return dominant
60
-
61
-
62
- def change_background_mode(chk_state):
63
- return gr.ColorPicker(visible=chk_state), \
64
- gr.Checkbox(value=False, visible=chk_state)
65
-
66
-
67
- footer = r"""
68
- <center>
69
- <b>
70
- Demo based on <a href='https://github.com/danielgatis/rembg'>Rembg</a>
71
- </b>
72
- </center>
73
- """
74
-
75
- with gr.Blocks(title="Remove background") as app:
76
- color_state = gr.State(value=False)
77
- matting_state = gr.State(value=(0, 0, 0))
78
-
79
- gr.HTML("<center><h1>Remove Background Tool</h1></center>")
80
- with gr.Row(equal_height=False):
81
- with gr.Column():
82
- input_img = gr.Image(type="pil", label="Input image")
83
- drp_models = gr.Dropdown(choices=model_choices, label="Model Segment", value="U2NET")
84
  with gr.Row():
85
- chk_include_matting = gr.Checkbox(label="Matting", value=False)
86
- chk_smoot_mask = gr.Checkbox(label="Smoot Mask", value=False)
87
- chk_show_mask = gr.Checkbox(label="Show Mask", value=False)
88
- with gr.Group(visible=False) as slider_matting:
89
- slr_fg_threshold = gr.Slider(0, 300, value=270, step=1, label="Alpha matting foreground threshold")
90
- slr_bg_threshold = gr.Slider(0, 50, value=20, step=1, label="Alpha matting background threshold")
91
- slr_erode_size = gr.Slider(0, 20, value=11, step=1, label="Alpha matting erode size")
92
- with gr.Group():
93
- with gr.Row():
94
- chk_change_color = gr.Checkbox(label="Change background color", value=False)
95
- pkr_color = gr.ColorPicker(label="Pick a new color", visible=False)
96
- chk_dominant = gr.Checkbox(label="Use dominant color", value=False, visible=False)
97
- run_btn = gr.Button(value="Remove background", variant="primary")
98
- with gr.Column():
99
- output_img = gr.Image(type="pil", label="Image Result")
100
- mask_img = gr.Image(type="pil", label="Image Mask", visible=False)
101
- gr.ClearButton(components=[input_img, output_img, mask_img])
102
-
103
- chk_include_matting.change(change_include_matting, inputs=[chk_include_matting],
104
- outputs=[slider_matting, matting_state,
105
- slr_fg_threshold, slr_bg_threshold, slr_erode_size])
106
 
107
- slr_fg_threshold.change(change_foreground_threshold, inputs=[slr_fg_threshold, matting_state],
108
- outputs=[matting_state])
109
-
110
- slr_bg_threshold.change(change_background_threshold, inputs=[slr_bg_threshold, matting_state],
111
- outputs=[matting_state])
112
-
113
- slr_erode_size.change(change_erode_size, inputs=[slr_erode_size, matting_state],
114
- outputs=[matting_state])
115
-
116
- chk_show_mask.change(change_show_mask, inputs=[chk_show_mask], outputs=[mask_img])
117
-
118
- chk_change_color.change(change_background_mode, inputs=[chk_change_color],
119
- outputs=[pkr_color, chk_dominant])
120
-
121
- pkr_color.change(change_picker_color, inputs=[pkr_color, chk_dominant], outputs=[color_state])
122
-
123
- chk_dominant.change(set_dominant_color, inputs=[chk_dominant], outputs=[color_state, pkr_color])
124
-
125
- run_btn.click(predict, inputs=[input_img, drp_models, chk_smoot_mask, matting_state, color_state],
126
- outputs=[output_img, mask_img])
127
-
128
- with gr.Row():
129
- gr.HTML(footer)
130
 
131
- app.queue()
132
- app.launch(share=False, debug=True, show_error=True)
 
1
+ #######################################################################################
2
+ #
3
+ # MIT License
4
+ #
5
+ # Copyright (c) [2025] [[email protected]]
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #
25
+ #######################################################################################
26
+
27
+ # Implements an API endpoint for background image removal.
28
+ #
29
+ # This project is one of several repositories exploring image segmentation techniques.
30
+ # All related projects and interactive demos can be found at:
31
+ # https://huggingface.co/spaces/leonelhs/removators
32
+ # Self app: https://huggingface.co/spaces/leonelhs/rembg
33
+ #
34
+ # Source code is based on or inspired by several projects.
35
+ # For more details and proper attribution, please refer to the following resources:
36
+ #
37
+ # - [Rembg] - [https://github.com/danielgatis/rembg]
38
+ # - [huggingface] [https://huggingface.co/spaces/KenjieDec/RemBG]
39
+ #
40
 
41
  import gradio as gr
42
+ import numpy as np
43
+ from PIL import Image
44
  from rembg import new_session
45
+ from rembg.bg import post_process
46
 
47
+ MODELS = {
48
+ "General segmentation": "u2net",
49
+ "Human segmentation": "u2net_human_seg",
50
+ "Cloth segmentation": "u2net_cloth_seg"
 
 
 
51
  }
52
 
53
+ def predict(image, session="u2net"):
54
+ """
55
+ Remove the background from an image.
56
+ The function extracts the foreground and generates both a background-removed
57
+ image and a binary mask.
58
+ Parameters:
59
+ image (pil): File path to the input image.
60
+ session (string): Model for generate cutting mask.
61
+ Returns:
62
+ paths (tuple): paths for background-removed image and cutting mask.
63
+ """
64
+ session = new_session(session)
65
+ mask = session.predict(image)[0]
66
+ smoot_mask = Image.fromarray(post_process(np.array(mask)))
67
+ image.putalpha(smoot_mask)
68
+ return image, smoot_mask
69
+
70
+ with gr.Blocks(title="Rembg") as app:
71
+ gr.Markdown("## Remove Background Tool")
72
+ with gr.Row():
73
+ with gr.Column(scale=1):
74
+ inp = gr.Image(type="pil", label="Upload Image")
75
+ sess = gr.Dropdown(choices=list(MODELS.items()), label="Model Segment", value="u2net")
76
+ btn_predict = gr.Button("Remove background")
77
+ with gr.Column(scale=2):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  with gr.Row():
79
+ with gr.Column(scale=1):
80
+ out = gr.Image(type="pil", label="Output image")
81
+ with gr.Accordion("See intermediates", open=False):
82
+ out_mask = gr.Image(type="pil", label="Mask")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
+ btn_predict.click(predict, inputs=[inp, sess], outputs=[out, out_mask])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
+ app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True)
87
+ app.queue()
notes.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx
2
+ https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_human_seg.onnx
3
+ https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_cloth_seg.onnx
old_app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ import gradio as gr
4
+ from rembg import new_session
5
+
6
+ from cutter import remove, make_label
7
+ from utils import *
8
+
9
+ remove_bg_models = {
10
+ "U2NET": "u2net",
11
+ "U2NET Human Seg": "u2net_human_seg",
12
+ "U2NET Cloth Seg": "u2net_cloth_seg"
13
+ }
14
+
15
+ model_choices = keys(remove_bg_models)
16
+
17
+
18
+ def predict(image, session, smoot, matting, bg_color):
19
+
20
+ session = new_session(remove_bg_models[session])
21
+
22
+ try:
23
+ return remove(session, image, smoot, matting, bg_color)
24
+ except ValueError as err:
25
+ logging.error(err)
26
+ return make_label(str(err)), None
27
+
28
+
29
+ def change_show_mask(chk_state):
30
+ return gr.Image(visible=chk_state)
31
+
32
+
33
+ def change_include_matting(chk_state):
34
+ return gr.Group(visible=chk_state), (0, 0, 0), 0, 0, 0
35
+
36
+
37
+ def change_foreground_threshold(fg_value, value):
38
+ fg, bg, erode = value
39
+ return fg_value, bg, erode
40
+
41
+
42
+ def change_background_threshold(bg_value, value):
43
+ fg, bg, erode = value
44
+ return fg, bg_value, erode
45
+
46
+
47
+ def change_erode_size(erode_value, value):
48
+ fg, bg, erode = value
49
+ return fg, bg, erode_value
50
+
51
+
52
+ def set_dominant_color(chk_state):
53
+ return chk_state, gr.ColorPicker(visible=not chk_state)
54
+
55
+
56
+ def change_picker_color(picker, dominant):
57
+ if not dominant:
58
+ return picker
59
+ return dominant
60
+
61
+
62
+ def change_background_mode(chk_state):
63
+ return gr.ColorPicker(visible=chk_state), \
64
+ gr.Checkbox(value=False, visible=chk_state)
65
+
66
+
67
+ footer = r"""
68
+ <center>
69
+ <b>
70
+ Demo based on <a href='https://github.com/danielgatis/rembg'>Rembg</a>
71
+ </b>
72
+ </center>
73
+ """
74
+
75
+ with gr.Blocks(title="Remove background") as app:
76
+ color_state = gr.State(value=False)
77
+ matting_state = gr.State(value=(0, 0, 0))
78
+
79
+ gr.HTML("<center><h1>Remove Background Tool</h1></center>")
80
+ with gr.Row(equal_height=False):
81
+ with gr.Column():
82
+ input_img = gr.Image(type="pil", label="Input image")
83
+ drp_models = gr.Dropdown(choices=model_choices, label="Model Segment", value="U2NET")
84
+ with gr.Row():
85
+ chk_include_matting = gr.Checkbox(label="Matting", value=False)
86
+ chk_smoot_mask = gr.Checkbox(label="Smoot Mask", value=False)
87
+ chk_show_mask = gr.Checkbox(label="Show Mask", value=False)
88
+ with gr.Group(visible=False) as slider_matting:
89
+ slr_fg_threshold = gr.Slider(0, 300, value=270, step=1, label="Alpha matting foreground threshold")
90
+ slr_bg_threshold = gr.Slider(0, 50, value=20, step=1, label="Alpha matting background threshold")
91
+ slr_erode_size = gr.Slider(0, 20, value=11, step=1, label="Alpha matting erode size")
92
+ with gr.Group():
93
+ with gr.Row():
94
+ chk_change_color = gr.Checkbox(label="Change background color", value=False)
95
+ pkr_color = gr.ColorPicker(label="Pick a new color", visible=False)
96
+ chk_dominant = gr.Checkbox(label="Use dominant color", value=False, visible=False)
97
+ run_btn = gr.Button(value="Remove background", variant="primary")
98
+ with gr.Column():
99
+ output_img = gr.Image(type="pil", label="Image Result")
100
+ mask_img = gr.Image(type="pil", label="Image Mask", visible=False)
101
+ gr.ClearButton(components=[input_img, output_img, mask_img])
102
+
103
+ chk_include_matting.change(change_include_matting, inputs=[chk_include_matting],
104
+ outputs=[slider_matting, matting_state,
105
+ slr_fg_threshold, slr_bg_threshold, slr_erode_size])
106
+
107
+ slr_fg_threshold.change(change_foreground_threshold, inputs=[slr_fg_threshold, matting_state],
108
+ outputs=[matting_state])
109
+
110
+ slr_bg_threshold.change(change_background_threshold, inputs=[slr_bg_threshold, matting_state],
111
+ outputs=[matting_state])
112
+
113
+ slr_erode_size.change(change_erode_size, inputs=[slr_erode_size, matting_state],
114
+ outputs=[matting_state])
115
+
116
+ chk_show_mask.change(change_show_mask, inputs=[chk_show_mask], outputs=[mask_img])
117
+
118
+ chk_change_color.change(change_background_mode, inputs=[chk_change_color],
119
+ outputs=[pkr_color, chk_dominant])
120
+
121
+ pkr_color.change(change_picker_color, inputs=[pkr_color, chk_dominant], outputs=[color_state])
122
+
123
+ chk_dominant.change(set_dominant_color, inputs=[chk_dominant], outputs=[color_state, pkr_color])
124
+
125
+ run_btn.click(predict, inputs=[input_img, drp_models, chk_smoot_mask, matting_state, color_state],
126
+ outputs=[output_img, mask_img])
127
+
128
+ with gr.Row():
129
+ gr.HTML(footer)
130
+
131
+ app.queue()
132
+ app.launch(share=False, debug=True, show_error=True)
requirements.txt CHANGED
@@ -1,7 +1,2 @@
1
- rembg~=2.0.47
2
- pillow~=10.4.0
3
- pymatting~=1.1.12
4
- opencv-python-headless
5
- gradio~=5.1.0
6
- numpy~=1.26.4
7
- scipy~=1.14.1
 
1
+ rembg>=2.0.67
2
+ onnxruntime>=1.22.1