use split method
Browse files
app.py
CHANGED
|
@@ -27,8 +27,11 @@ def hex_to_rgba(hex_color):
|
|
| 27 |
hex_color += 'FF' # Add full opacity if no alpha is provided
|
| 28 |
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4, 6))
|
| 29 |
|
| 30 |
-
def remove_background(input_image, bg_color,
|
| 31 |
try:
|
|
|
|
|
|
|
|
|
|
| 32 |
# Set up the session with the chosen model, or None if no model is selected
|
| 33 |
session = new_session(model_name) if model_name else None
|
| 34 |
|
|
@@ -71,7 +74,7 @@ def remove_background(input_image, bg_color, model_name, alpha_matting, post_pro
|
|
| 71 |
# Save the output image to a temporary file
|
| 72 |
# Generate a unique timestamp for the output file name
|
| 73 |
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 74 |
-
output_path = f"
|
| 75 |
output_image.save(output_path)
|
| 76 |
logging.info(f"Saved output image {output_path}")
|
| 77 |
|
|
@@ -87,11 +90,12 @@ iface = gr.Interface(
|
|
| 87 |
inputs=[
|
| 88 |
gr.Image(type="pil"),
|
| 89 |
gr.ColorPicker(label="Background Color", value=None),
|
| 90 |
-
gr.Dropdown(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
gr.Checkbox(label="Enable Alpha Matting", value=False),
|
| 96 |
gr.Checkbox(label="Post-Process Mask (to post process the mask to get better results)", value=False),
|
| 97 |
gr.Checkbox(label="Only Return Mask ", value=False)
|
|
@@ -100,7 +104,7 @@ iface = gr.Interface(
|
|
| 100 |
gr.Image(type="pil", label="Output Image"),
|
| 101 |
gr.File(label="Download the output image")
|
| 102 |
],
|
| 103 |
-
title="Advanced Background Remover v2.
|
| 104 |
description="Upload an image to remove the background. Customize the result with different options, including background color, model selection, alpha matting, and more.",
|
| 105 |
allow_flagging="never",
|
| 106 |
)
|
|
|
|
| 27 |
hex_color += 'FF' # Add full opacity if no alpha is provided
|
| 28 |
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4, 6))
|
| 29 |
|
| 30 |
+
def remove_background(input_image, bg_color, model_choice, alpha_matting, post_process_mask, only_mask):
|
| 31 |
try:
|
| 32 |
+
# Extract the model name from the choice
|
| 33 |
+
model_name = model_choice.split(' | ')[0] if model_choice else ""
|
| 34 |
+
|
| 35 |
# Set up the session with the chosen model, or None if no model is selected
|
| 36 |
session = new_session(model_name) if model_name else None
|
| 37 |
|
|
|
|
| 74 |
# Save the output image to a temporary file
|
| 75 |
# Generate a unique timestamp for the output file name
|
| 76 |
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 77 |
+
output_path = f"output_remove_background_{timestamp}.png"
|
| 78 |
output_image.save(output_path)
|
| 79 |
logging.info(f"Saved output image {output_path}")
|
| 80 |
|
|
|
|
| 90 |
inputs=[
|
| 91 |
gr.Image(type="pil"),
|
| 92 |
gr.ColorPicker(label="Background Color", value=None),
|
| 93 |
+
gr.Dropdown(
|
| 94 |
+
choices=[""] + [f"{k} | {v}" for k, v in MODEL_OPTIONS.items() if k != ""],
|
| 95 |
+
label="Model Selection",
|
| 96 |
+
value="",
|
| 97 |
+
type="value"
|
| 98 |
+
),
|
| 99 |
gr.Checkbox(label="Enable Alpha Matting", value=False),
|
| 100 |
gr.Checkbox(label="Post-Process Mask (to post process the mask to get better results)", value=False),
|
| 101 |
gr.Checkbox(label="Only Return Mask ", value=False)
|
|
|
|
| 104 |
gr.Image(type="pil", label="Output Image"),
|
| 105 |
gr.File(label="Download the output image")
|
| 106 |
],
|
| 107 |
+
title="Advanced Background Remover v2.5",
|
| 108 |
description="Upload an image to remove the background. Customize the result with different options, including background color, model selection, alpha matting, and more.",
|
| 109 |
allow_flagging="never",
|
| 110 |
)
|