asimfayaz commited on
Commit
0a862c7
·
1 Parent(s): 26182bb

Removed all the alpha matting stuff because bgr will be removed by the caller

Browse files
Files changed (1) hide show
  1. hy3dshape/hy3dshape/rembg.py +12 -58
hy3dshape/hy3dshape/rembg.py CHANGED
@@ -38,7 +38,7 @@ from PIL import Image
38
  from rembg import remove, new_session
39
 
40
 
41
- def _remove_bg_in_process(image_bytes, use_alpha_matting=True):
42
  """Function to run in a separate process to avoid OpenMP issues"""
43
  try:
44
  # Create a new session in this process
@@ -47,26 +47,12 @@ def _remove_bg_in_process(image_bytes, use_alpha_matting=True):
47
  # Convert bytes back to PIL Image
48
  image = Image.open(io.BytesIO(image_bytes))
49
 
50
- if use_alpha_matting:
51
- # High quality mode with alpha matting
52
- output = remove(
53
- image,
54
- session=session,
55
- bgcolor=[255, 255, 255, 0],
56
- alpha_matting=True,
57
- alpha_matting_foreground_threshold=220,
58
- alpha_matting_background_threshold=5,
59
- alpha_matting_erode_structure_size=5,
60
- alpha_matting_base_size=512,
61
- )
62
- else:
63
- # Fallback mode without alpha matting
64
- output = remove(
65
- image,
66
- session=session,
67
- bgcolor=[255, 255, 255, 0],
68
- alpha_matting=False,
69
- )
70
 
71
  # Convert back to bytes
72
  output_bytes = io.BytesIO()
@@ -114,10 +100,9 @@ class BackgroundRemover():
114
  if image.mode != 'RGBA':
115
  image = image.convert('RGBA')
116
 
117
- print("Starting direct background removal (OpenMP conflicts handled by environment variables)...")
118
 
119
- # Try alpha matting first (high quality)
120
- print("Attempting alpha matting background removal...")
121
  try:
122
  # Use pre-loaded session if available, otherwise create new one
123
  session = self.session if self.session is not None else new_session('u2net')
@@ -125,47 +110,16 @@ class BackgroundRemover():
125
  image,
126
  session=session,
127
  bgcolor=[255, 255, 255, 0],
128
- alpha_matting=True,
129
- alpha_matting_foreground_threshold=220,
130
- alpha_matting_background_threshold=5,
131
- alpha_matting_erode_structure_size=5,
132
- alpha_matting_base_size=512,
133
  )
134
- print("Alpha matting successful!")
135
  return output
136
  except Exception as e:
137
  error_msg = str(e)
138
- print(f"Alpha matting failed: {e}")
139
 
140
  # Check for specific OpenMP fork errors
141
  if "fork()" in error_msg and "OpenMP" in error_msg:
142
- print("DETECTED: OpenMP fork() error in rembg library itself!")
143
- print("This is an internal rembg issue, not our code.")
144
- print("Falling back to standard background removal...")
145
- else:
146
- import traceback
147
- traceback.print_exc()
148
-
149
- # If alpha matting fails, try standard background removal
150
- print("Alpha matting failed, falling back to standard background removal")
151
- try:
152
- # Use pre-loaded session if available, otherwise create new one
153
- session = self.session if self.session is not None else new_session('u2net')
154
- output = remove(
155
- image,
156
- session=session,
157
- bgcolor=[255, 255, 255, 0],
158
- alpha_matting=False,
159
- )
160
- print("Standard background removal successful!")
161
- return output
162
- except Exception as e:
163
- error_msg = str(e)
164
- print(f"Standard background removal failed: {e}")
165
-
166
- # Check for specific OpenMP fork errors
167
- if "fork()" in error_msg and "OpenMP" in error_msg:
168
- print("DETECTED: OpenMP fork() error in rembg library (standard mode)!")
169
  print("The rembg library itself has internal multiprocessing that conflicts with OpenMP.")
170
  print("This is a known issue with the rembg library in daemon environments.")
171
  else:
 
38
  from rembg import remove, new_session
39
 
40
 
41
+ def _remove_bg_in_process(image_bytes):
42
  """Function to run in a separate process to avoid OpenMP issues"""
43
  try:
44
  # Create a new session in this process
 
47
  # Convert bytes back to PIL Image
48
  image = Image.open(io.BytesIO(image_bytes))
49
 
50
+ # Standard background removal
51
+ output = remove(
52
+ image,
53
+ session=session,
54
+ bgcolor=[255, 255, 255, 0],
55
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Convert back to bytes
58
  output_bytes = io.BytesIO()
 
100
  if image.mode != 'RGBA':
101
  image = image.convert('RGBA')
102
 
103
+ print("Starting background removal (OpenMP conflicts handled by environment variables)...")
104
 
105
+ # Standard background removal
 
106
  try:
107
  # Use pre-loaded session if available, otherwise create new one
108
  session = self.session if self.session is not None else new_session('u2net')
 
110
  image,
111
  session=session,
112
  bgcolor=[255, 255, 255, 0],
 
 
 
 
 
113
  )
114
+ print("Background removal successful!")
115
  return output
116
  except Exception as e:
117
  error_msg = str(e)
118
+ print(f"Background removal failed: {e}")
119
 
120
  # Check for specific OpenMP fork errors
121
  if "fork()" in error_msg and "OpenMP" in error_msg:
122
+ print("DETECTED: OpenMP fork() error in rembg library!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  print("The rembg library itself has internal multiprocessing that conflicts with OpenMP.")
124
  print("This is a known issue with the rembg library in daemon environments.")
125
  else: