diego2554 commited on
Commit
15215aa
1 Parent(s): 29ca47e

Update rembg/bg.py

Browse files
Files changed (1) hide show
  1. rembg/bg.py +12 -29
rembg/bg.py CHANGED
@@ -1,41 +1,27 @@
1
  import io
2
- from enum import Enum
3
- from typing import Any, List, Optional, Tuple, Union
4
-
5
  import numpy as np
6
- from cv2 import (
7
- BORDER_DEFAULT,
8
- MORPH_ELLIPSE,
9
- MORPH_OPEN,
10
- GaussianBlur,
11
- getStructuringElement,
12
- morphologyEx,
13
- )
14
  from PIL import Image, ImageOps
15
- from PIL.Image import Image as PILImage
 
16
  from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf
17
  from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml
18
- from pymatting.util.util import stack_images
19
  from scipy.ndimage import binary_erosion
20
 
21
- from .session_factory import new_session
22
- from .sessions import sessions_class
23
- from .sessions.base import BaseSession
24
-
25
- kernel = getStructuringElement(MORPH_ELLIPSE, (3, 3))
26
-
27
 
28
  class ReturnType(Enum):
29
  BYTES = 0
30
  PILLOW = 1
31
  NDARRAY = 2
32
 
33
-
34
  def alpha_matting_cutout(
35
  img: PILImage,
36
  mask: PILImage,
37
- foreground_threshold: int,
38
- background_threshold: int,
39
  erode_structure_size: int,
40
  ) -> PILImage:
41
  if img.mode == "RGBA" or img.mode == "CMYK":
@@ -44,14 +30,12 @@ def alpha_matting_cutout(
44
  img = np.asarray(img)
45
  mask = np.asarray(mask)
46
 
47
- is_foreground = mask > foreground_threshold
48
- is_background = mask < background_threshold
49
 
50
  structure = None
51
  if erode_structure_size > 0:
52
- structure = np.ones(
53
- (erode_structure_size, erode_structure_size), dtype=np.uint8
54
- )
55
 
56
  is_foreground = binary_erosion(is_foreground, structure=structure)
57
  is_background = binary_erosion(is_background, structure=structure, border_value=1)
@@ -71,8 +55,7 @@ def alpha_matting_cutout(
71
  cutout = Image.fromarray(cutout)
72
 
73
  return cutout
74
-
75
-
76
  def naive_cutout(img: PILImage, mask: PILImage) -> PILImage:
77
  empty = Image.new("RGBA", (img.size), 0)
78
  cutout = Image.composite(img, empty, mask)
 
1
  import io
 
 
 
2
  import numpy as np
 
 
 
 
 
 
 
 
3
  from PIL import Image, ImageOps
4
+ from typing import Any, List, Optional, Tuple, Union
5
+ from enum import Enum
6
  from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf
7
  from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml
 
8
  from scipy.ndimage import binary_erosion
9
 
10
+ # Definir los valores por defecto para los parámetros
11
+ DEFAULT_ALPHA_INFLUENCE = 0.5
12
+ DEFAULT_SEGMENTATION_STRENGTH = 0.5
13
+ DEFAULT_SMOOTHING = 0.5
 
 
14
 
15
  class ReturnType(Enum):
16
  BYTES = 0
17
  PILLOW = 1
18
  NDARRAY = 2
19
 
 
20
  def alpha_matting_cutout(
21
  img: PILImage,
22
  mask: PILImage,
23
+ alpha_influence: float,
24
+ segmentation_strength: float,
25
  erode_structure_size: int,
26
  ) -> PILImage:
27
  if img.mode == "RGBA" or img.mode == "CMYK":
 
30
  img = np.asarray(img)
31
  mask = np.asarray(mask)
32
 
33
+ is_foreground = mask > alpha_influence
34
+ is_background = mask < (1.0 - alpha_influence)
35
 
36
  structure = None
37
  if erode_structure_size > 0:
38
+ structure = np.ones((erode_structure_size, erode_structure_size), dtype=np.uint8)
 
 
39
 
40
  is_foreground = binary_erosion(is_foreground, structure=structure)
41
  is_background = binary_erosion(is_background, structure=structure, border_value=1)
 
55
  cutout = Image.fromarray(cutout)
56
 
57
  return cutout
58
+ #aca termina la modificacion
 
59
  def naive_cutout(img: PILImage, mask: PILImage) -> PILImage:
60
  empty = Image.new("RGBA", (img.size), 0)
61
  cutout = Image.composite(img, empty, mask)