diego2554 commited on
Commit
3015442
1 Parent(s): e3ddad3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
2
  import os
3
  import cv2
 
 
 
4
 
5
  def inference(file, mask, model, alpha_influence, segmentation_strength):
6
  im = cv2.imread(file, cv2.IMREAD_COLOR)
7
  cv2.imwrite(os.path.join("input.png"), im)
8
 
9
- from rembg import new_session, remove
10
-
11
  input_path = 'input.png'
12
  output_path = 'output.png'
13
 
@@ -16,16 +17,19 @@ def inference(file, mask, model, alpha_influence, segmentation_strength):
16
  input = i.read()
17
  output = remove(
18
  input,
19
- session=new_session(model),
20
  only_mask=(True if mask == "Mask only" else False),
21
- alpha=alpha_influence, # Control de influencia del canal alfa
22
- bg_color=(0, 0, 0, segmentation_strength) # Control de fuerza de segmentación
 
 
 
23
  )
24
 
25
  o.write(output)
26
- return os.path.join("output.png")
27
 
28
- title = "RemBG_ Super"
 
 
29
  description = "Gradio demo for RemBG. To use it, simply upload your image and adjust the alpha influence and segmentation strength."
30
  article = "<p style='text-align: center;'><a href='https://github.com/danielgatis/rembg' target='_blank'>Github Repo</a></p>"
31
 
@@ -59,7 +63,7 @@ gr.Interface(
59
  gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Alpha Influence"),
60
  gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Segmentation Strength"),
61
  ],
62
- gr.outputs.Image(type="filepath", label="Output"),
63
  title=title,
64
  description=description,
65
  article=article,
@@ -67,4 +71,3 @@ gr.Interface(
67
  enable_queue=True
68
  ).launch()
69
 
70
-
 
1
  import gradio as gr
2
  import os
3
  import cv2
4
+ from rembg import new_session, remove
5
+ from PIL import Image
6
+ from io import BytesIO
7
 
8
  def inference(file, mask, model, alpha_influence, segmentation_strength):
9
  im = cv2.imread(file, cv2.IMREAD_COLOR)
10
  cv2.imwrite(os.path.join("input.png"), im)
11
 
 
 
12
  input_path = 'input.png'
13
  output_path = 'output.png'
14
 
 
17
  input = i.read()
18
  output = remove(
19
  input,
 
20
  only_mask=(True if mask == "Mask only" else False),
21
+ alpha_matting=True, # Habilitar el modo alpha matting
22
+ alpha_matting_foreground_threshold=alpha_influence, # Control de influencia del canal alfa
23
+ alpha_matting_background_threshold=1 - alpha_influence, # Control del canal alfa para el fondo
24
+ alpha_matting_erode_size=int(segmentation_strength * 10), # Control de fuerza de segmentación
25
+ session=new_session(model)
26
  )
27
 
28
  o.write(output)
 
29
 
30
+ return Image.open(BytesIO(output))
31
+
32
+ title = "RemBG"
33
  description = "Gradio demo for RemBG. To use it, simply upload your image and adjust the alpha influence and segmentation strength."
34
  article = "<p style='text-align: center;'><a href='https://github.com/danielgatis/rembg' target='_blank'>Github Repo</a></p>"
35
 
 
63
  gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Alpha Influence"),
64
  gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Segmentation Strength"),
65
  ],
66
+ gr.outputs.Image(type="PIL", label="Output"),
67
  title=title,
68
  description=description,
69
  article=article,
 
71
  enable_queue=True
72
  ).launch()
73