Spaces:
Running
on
Zero
Running
on
Zero
Restored the background removal file to the original state
Browse files- gradio_app.py +1 -1
- hy3dshape/hy3dshape/rembg.py +3 -116
gradio_app.py
CHANGED
@@ -923,7 +923,7 @@ def build_app():
|
|
923 |
# with gr.Tab('Text Prompt', id='tab_txt_prompt', visible=HAS_T2I and not MV_MODE) as tab_tp:
|
924 |
# caption = gr.Textbox(label='Text Prompt',
|
925 |
# placeholder='HunyuanDiT will be used to generate image.',
|
926 |
-
# info='Example: A 3D model of a cute cat, white background')
|
927 |
with gr.Tab('MultiView Prompt', visible=True) as tab_mv:
|
928 |
# gr.Label('Please upload at least one front image.')
|
929 |
with gr.Row():
|
|
|
923 |
# with gr.Tab('Text Prompt', id='tab_txt_prompt', visible=HAS_T2I and not MV_MODE) as tab_tp:
|
924 |
# caption = gr.Textbox(label='Text Prompt',
|
925 |
# placeholder='HunyuanDiT will be used to generate image.',
|
926 |
+
# info='Example: A 3D model of a cute cat, white background.')
|
927 |
with gr.Tab('MultiView Prompt', visible=True) as tab_mv:
|
928 |
# gr.Label('Please upload at least one front image.')
|
929 |
with gr.Row():
|
hy3dshape/hy3dshape/rembg.py
CHANGED
@@ -12,127 +12,14 @@
|
|
12 |
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
13 |
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
14 |
|
15 |
-
import os
|
16 |
-
import io
|
17 |
-
import traceback
|
18 |
-
import numpy as np
|
19 |
-
|
20 |
-
# Set comprehensive OpenMP environment variables to prevent ANY threading/fork conflicts
|
21 |
-
# This prevents both our code and the rembg library from causing OpenMP fork() errors
|
22 |
-
os.environ["OMP_NUM_THREADS"] = "1" # Limit OpenMP to single thread
|
23 |
-
os.environ["MKL_NUM_THREADS"] = "1" # Intel MKL threading
|
24 |
-
os.environ["NUMEXPR_NUM_THREADS"] = "1" # NumExpr threading
|
25 |
-
os.environ["OPENBLAS_NUM_THREADS"] = "1" # OpenBLAS threading
|
26 |
-
os.environ["VECLIB_MAXIMUM_THREADS"] = "1" # Apple Accelerate threading
|
27 |
-
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # Avoid Intel MKL errors
|
28 |
-
os.environ["OMP_THREAD_LIMIT"] = "1" # Limit total number of OpenMP threads
|
29 |
-
os.environ["OMP_DISPLAY_ENV"] = "FALSE" # Don't display OpenMP environment
|
30 |
-
os.environ["KMP_WARNINGS"] = "FALSE" # Suppress KMP warnings
|
31 |
-
os.environ["PYTHONFAULTHANDLER"] = "1" # Better error reporting
|
32 |
-
# Additional settings to prevent subprocess/multiprocessing issues in rembg
|
33 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "false" # Disable tokenizer parallelism
|
34 |
-
os.environ["OMP_WAIT_POLICY"] = "PASSIVE" # Use passive waiting
|
35 |
-
os.environ["KMP_INIT_AT_FORK"] = "FALSE" # Don't initialize OpenMP at fork
|
36 |
-
|
37 |
from PIL import Image
|
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
|
45 |
-
session = new_session()
|
46 |
-
|
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()
|
59 |
-
output.save(output_bytes, format='PNG')
|
60 |
-
return output_bytes.getvalue()
|
61 |
-
|
62 |
-
except Exception as e:
|
63 |
-
print(f"Background removal process error: {e}")
|
64 |
-
traceback.print_exc()
|
65 |
-
return None
|
66 |
-
|
67 |
-
|
68 |
class BackgroundRemover():
|
69 |
def __init__(self):
|
70 |
-
|
71 |
-
# OpenMP conflicts are prevented by environment variables set at module level
|
72 |
-
print("Initializing BackgroundRemover with direct processing (no multiprocessing needed)")
|
73 |
-
|
74 |
-
# Pre-initialize session to download models and avoid runtime downloads
|
75 |
-
# This prevents subprocess calls during actual background removal
|
76 |
-
try:
|
77 |
-
print("Pre-loading rembg models to prevent runtime downloads...")
|
78 |
-
self.session = new_session('u2net') # Pre-load the default model
|
79 |
-
print("rembg models pre-loaded successfully!")
|
80 |
-
except Exception as e:
|
81 |
-
print(f"Warning: Could not pre-load rembg models: {e}")
|
82 |
-
print("Models will be downloaded on first use (may cause OpenMP issues)")
|
83 |
-
self.session = None
|
84 |
|
85 |
def __call__(self, image: Image.Image):
|
86 |
-
|
87 |
-
|
88 |
-
try:
|
89 |
-
if hasattr(image, 'convert'):
|
90 |
-
image = image.convert('RGBA')
|
91 |
-
else:
|
92 |
-
# Try to create PIL Image from input
|
93 |
-
image = Image.fromarray(np.asarray(image))
|
94 |
-
except Exception as e:
|
95 |
-
print(f"Could not convert input to PIL Image: {e}")
|
96 |
-
return image
|
97 |
-
|
98 |
-
try:
|
99 |
-
# Ensure image is in RGBA mode
|
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')
|
109 |
-
output = remove(
|
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:
|
126 |
-
import traceback
|
127 |
-
traceback.print_exc()
|
128 |
-
|
129 |
-
# If all else fails, return the original image
|
130 |
-
print("Background removal completely failed, returning original image")
|
131 |
-
return image
|
132 |
-
|
133 |
-
except Exception as e:
|
134 |
-
print(f"Background removal error in main process: {e}")
|
135 |
-
traceback.print_exc()
|
136 |
-
# If all else fails, just return the original image
|
137 |
-
# This ensures the pipeline doesn't break completely
|
138 |
-
return image
|
|
|
12 |
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
13 |
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
from PIL import Image
|
16 |
from rembg import remove, new_session
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
class BackgroundRemover():
|
20 |
def __init__(self):
|
21 |
+
self.session = new_session()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def __call__(self, image: Image.Image):
|
24 |
+
output = remove(image, session=self.session, bgcolor=[255, 255, 255, 0])
|
25 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|