Commit
·
135bed3
1
Parent(s):
0ac4eff
Add configurable ONNX model path in app.py for background removal. Implement error handling for missing model file and ensure valid image processing. Updated remove_background function to utilize environment variable for model path.
Browse files
app.py
CHANGED
@@ -19,6 +19,9 @@ from model import CRM
|
|
19 |
from inference import generate3d
|
20 |
from dis_bg_remover import remove_background as dis_remove_background
|
21 |
|
|
|
|
|
|
|
22 |
pipeline = None
|
23 |
|
24 |
|
@@ -44,8 +47,14 @@ def remove_background(
|
|
44 |
force: bool = False,
|
45 |
**rembg_kwargs,
|
46 |
) -> PIL.Image.Image:
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
return extracted_img
|
50 |
|
51 |
def do_resize_content(original_image: Image, scale_rate):
|
@@ -79,6 +88,8 @@ def preprocess_image(image, background_choice, foreground_ratio, backgroud_color
|
|
79 |
image = Image.alpha_composite(background, image)
|
80 |
else:
|
81 |
image = remove_background(image, force=True)
|
|
|
|
|
82 |
image = do_resize_content(image, foreground_ratio)
|
83 |
image = expand_to_square(image)
|
84 |
image = add_background(image, backgroud_color)
|
|
|
19 |
from inference import generate3d
|
20 |
from dis_bg_remover import remove_background as dis_remove_background
|
21 |
|
22 |
+
# Configurable ONNX model path (can be set via environment variable)
|
23 |
+
DIS_ONNX_MODEL_PATH = os.environ.get("DIS_ONNX_MODEL_PATH", "isnet_dis.onnx")
|
24 |
+
|
25 |
pipeline = None
|
26 |
|
27 |
|
|
|
47 |
force: bool = False,
|
48 |
**rembg_kwargs,
|
49 |
) -> PIL.Image.Image:
|
50 |
+
# Check if the ONNX model exists
|
51 |
+
if not os.path.exists(DIS_ONNX_MODEL_PATH):
|
52 |
+
raise gr.Error(
|
53 |
+
f"DIS background remover model file not found at {DIS_ONNX_MODEL_PATH}. "
|
54 |
+
"Please download it from https://huggingface.co/stoned0651/isnet_dis.onnx/resolve/main/isnet_dis.onnx "
|
55 |
+
"and place it in the project directory or set the DIS_ONNX_MODEL_PATH environment variable."
|
56 |
+
)
|
57 |
+
extracted_img, mask = dis_remove_background(DIS_ONNX_MODEL_PATH, image)
|
58 |
return extracted_img
|
59 |
|
60 |
def do_resize_content(original_image: Image, scale_rate):
|
|
|
88 |
image = Image.alpha_composite(background, image)
|
89 |
else:
|
90 |
image = remove_background(image, force=True)
|
91 |
+
if image is None:
|
92 |
+
raise gr.Error("Background removal failed. Please check the input image and ensure the model file exists and is valid.")
|
93 |
image = do_resize_content(image, foreground_ratio)
|
94 |
image = expand_to_square(image)
|
95 |
image = add_background(image, backgroud_color)
|