Spaces:
Running
on
Zero
Running
on
Zero
Adds full docstrings for your main functions, MCP server ready !
#21
by
fffiloni
- opened
app.py
CHANGED
|
@@ -21,6 +21,21 @@ transform_image = transforms.Compose(
|
|
| 21 |
)
|
| 22 |
|
| 23 |
def fn(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
im = load_img(image, output_type="pil")
|
| 25 |
im = im.convert("RGB")
|
| 26 |
origin = im.copy()
|
|
@@ -29,6 +44,18 @@ def fn(image):
|
|
| 29 |
|
| 30 |
@spaces.GPU
|
| 31 |
def process(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
image_size = image.size
|
| 33 |
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
| 34 |
# Prediction
|
|
@@ -41,6 +68,15 @@ def process(image):
|
|
| 41 |
return image
|
| 42 |
|
| 43 |
def process_file(f):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
name_path = f.rsplit(".", 1)[0] + ".png"
|
| 45 |
im = load_img(f, output_type="pil")
|
| 46 |
im = im.convert("RGB")
|
|
@@ -68,4 +104,4 @@ demo = gr.TabbedInterface(
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
demo.launch(show_error=True)
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
def fn(image):
|
| 24 |
+
"""
|
| 25 |
+
Remove the background from an image and return both the transparent version and the original.
|
| 26 |
+
|
| 27 |
+
This function performs background removal using a BiRefNet segmentation model. It is intended for use
|
| 28 |
+
with image input (either uploaded or from a URL). The function returns a transparent PNG version of the image
|
| 29 |
+
with the background removed, along with the original RGB version for comparison.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
image (PIL.Image or str): The input image, either as a PIL object or a filepath/URL string.
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
tuple:
|
| 36 |
+
- processed_image (PIL.Image): The input image with the background removed and transparency applied.
|
| 37 |
+
- origin (PIL.Image): The original RGB image, unchanged.
|
| 38 |
+
"""
|
| 39 |
im = load_img(image, output_type="pil")
|
| 40 |
im = im.convert("RGB")
|
| 41 |
origin = im.copy()
|
|
|
|
| 44 |
|
| 45 |
@spaces.GPU
|
| 46 |
def process(image):
|
| 47 |
+
"""
|
| 48 |
+
Apply BiRefNet-based image segmentation to remove the background.
|
| 49 |
+
|
| 50 |
+
This function preprocesses the input image, runs it through a BiRefNet segmentation model to obtain a mask,
|
| 51 |
+
and applies the mask as an alpha (transparency) channel to the original image.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
image (PIL.Image): The input RGB image.
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
PIL.Image: The image with the background removed, using the segmentation mask as transparency.
|
| 58 |
+
"""
|
| 59 |
image_size = image.size
|
| 60 |
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
| 61 |
# Prediction
|
|
|
|
| 68 |
return image
|
| 69 |
|
| 70 |
def process_file(f):
|
| 71 |
+
"""
|
| 72 |
+
Load an image file from disk, remove the background, and save the output as a transparent PNG.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
f (str): Filepath of the image to process.
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
str: Path to the saved PNG image with background removed.
|
| 79 |
+
"""
|
| 80 |
name_path = f.rsplit(".", 1)[0] + ".png"
|
| 81 |
im = load_img(f, output_type="pil")
|
| 82 |
im = im.convert("RGB")
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
demo.launch(show_error=True, mcp_server=True)
|