Commit
·
1756164
1
Parent(s):
882d814
Refactor background removal and image preprocessing in app.py
Browse files- Enhanced the remove_background function to handle mask processing and integrate it with the original image.
- Updated preprocess_image to support RGBA images and added functionality for alpha masking.
- Removed unused hex_to_rgb function and streamlined the image processing flow for improved clarity and performance.
app.py
CHANGED
|
@@ -72,9 +72,16 @@ def remove_background(
|
|
| 72 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp:
|
| 73 |
# Save the PIL image to the temporary file
|
| 74 |
image.save(temp.name)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return extracted_img
|
| 79 |
|
| 80 |
def do_resize_content(original_image: Image, scale_rate):
|
|
@@ -98,37 +105,19 @@ def add_background(image, bg_color=(255, 255, 255)):
|
|
| 98 |
return Image.alpha_composite(background, image)
|
| 99 |
|
| 100 |
|
| 101 |
-
def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
|
| 102 |
-
"""Converts a hex color string to an RGB tuple."""
|
| 103 |
-
hex_color = hex_color.lstrip('#')
|
| 104 |
-
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
| 105 |
-
|
| 106 |
-
|
| 107 |
def preprocess_image(image, background_choice, foreground_ratio, backgroud_color):
|
| 108 |
"""
|
| 109 |
-
|
| 110 |
-
and adding a new solid background.
|
| 111 |
-
Returns an RGB PIL Image.
|
| 112 |
"""
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
image = remove_background(image)
|
| 118 |
if image is None:
|
| 119 |
raise gr.Error("Background removal failed. Please check the input image and ensure the model file exists and is valid.")
|
| 120 |
|
| 121 |
-
# Resize the content of the image
|
| 122 |
-
image = do_resize_content(image, foreground_ratio)
|
| 123 |
-
|
| 124 |
-
# Add a solid background color
|
| 125 |
-
rgb_background = hex_to_rgb(backgroud_color)
|
| 126 |
-
image_with_bg = add_background(image, rgb_background)
|
| 127 |
-
|
| 128 |
-
# Convert to RGB and return
|
| 129 |
-
return image_with_bg.convert("RGB")
|
| 130 |
-
|
| 131 |
-
|
| 132 |
@spaces.GPU
|
| 133 |
def gen_image(input_image, seed, scale, step):
|
| 134 |
global pipeline, model, args
|
|
|
|
| 72 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as temp:
|
| 73 |
# Save the PIL image to the temporary file
|
| 74 |
image.save(temp.name)
|
| 75 |
+
extracted_img, mask = dis_remove_background(DIS_ONNX_MODEL_PATH, temp.name)
|
| 76 |
+
if isinstance(extracted_img, np.ndarray):
|
| 77 |
+
if mask.dtype != np.uint8:
|
| 78 |
+
mask = (np.clip(mask, 0, 1) * 255).astype(np.uint8)
|
| 79 |
+
if mask.ndim == 3:
|
| 80 |
+
mask = mask[..., 0]
|
| 81 |
+
image = image.convert("RGBA")
|
| 82 |
+
image_np = np.array(image)
|
| 83 |
+
image_np[..., 3] = mask
|
| 84 |
+
return Image.fromarray(image_np)
|
| 85 |
return extracted_img
|
| 86 |
|
| 87 |
def do_resize_content(original_image: Image, scale_rate):
|
|
|
|
| 105 |
return Image.alpha_composite(background, image)
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
def preprocess_image(image, background_choice, foreground_ratio, backgroud_color):
|
| 109 |
"""
|
| 110 |
+
input image is a pil image in RGBA, return RGB image
|
|
|
|
|
|
|
| 111 |
"""
|
| 112 |
+
print(background_choice)
|
| 113 |
+
if background_choice == "Alpha as mask":
|
| 114 |
+
background = Image.new("RGBA", image.size, (0, 0, 0, 0))
|
| 115 |
+
image = Image.alpha_composite(background, image)
|
| 116 |
+
else:
|
| 117 |
image = remove_background(image)
|
| 118 |
if image is None:
|
| 119 |
raise gr.Error("Background removal failed. Please check the input image and ensure the model file exists and is valid.")
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
@spaces.GPU
|
| 122 |
def gen_image(input_image, seed, scale, step):
|
| 123 |
global pipeline, model, args
|