Spaces:
Running on Zero
Running on Zero
Download remove_background.py from fedorajuandy/remove-background: direct link, hf CLI and curl.
- Browser
- Download file 822 Bytes
-
https://huggingface.co/spaces/fedorajuandy/remove-background/resolve/main/remove_background.py
- Command line
-
hf download hf://spaces/fedorajuandy/remove-background/remove_background.py
-
curl -L -o remove_background.py https://huggingface.co/spaces/fedorajuandy/remove-background/resolve/main/remove_background.py
822 Bytes
| """Background removal with the commercially permissive BiRefNet model. | |
| The model is provided by ``rembg`` and is downloaded once on first use. See | |
| README.md for model attribution and hosting notes. | |
| """ | |
| # pylint: disable=broad-exception-caught | |
| from functools import lru_cache | |
| from PIL import Image | |
| from rembg import new_session, remove | |
| MODEL_NAME = "birefnet-general" | |
| def get_session(): | |
| """Create and retain one BiRefNet inference session per process.""" | |
| return new_session(MODEL_NAME) | |
| def remove_background(image: Image.Image) -> Image.Image: | |
| """Return *image* as an RGBA image with its background made transparent.""" | |
| if image is None: | |
| raise ValueError("Please upload an image first.") | |
| return remove(image.convert("RGBA"), session=get_session()).convert("RGBA") | |