Rembg is a tool to remove images background
feat: make bria-rmbg the default model (#845)
Daniel Gatisf91a1ce328c7
diff --git a/README.md b/README.mdindex ae06798..4809160 100644--- a/README.md+++ b/README.md@@ -376,9 +376,10 @@ some images; rembg falls back to a decontaminated cutout when that happens.### Which model to pair it with-The newer models (`birefnet-*`, `isnet-general-use`, `bria-rmbg`) already+The newer models (`bria-rmbg`, `birefnet-*`, `isnet-general-use`) alreadyproduce a soft, well-shaped alpha, so their masks rarely need `-a` — reach for-`-dc` first and only try `-a` if the edge shape itself is wrong.+`-dc` first and only try `-a` if the edge shape itself is wrong. `bria-rmbg` is+the default, so this is the advice that applies unless you pass `-m`.The older models (`u2net`, `u2netp`, `silueta`) tend to produce firmer, blockieredges. They benefit most from `-a` on hair-heavy portraits, and are also where@@ -464,11 +465,11 @@ sudo docker run --rm -it --gpus all -v /dev/dri:/dev/dri -v $PWD:/data rembg-nvi**Tips:**- You can create your own NVIDIA CUDA image and install `rembg[gpu,cli]` in it.-- Use `-v /path/to/models/:/root/.u2net` to store model files outside the container, avoiding re-downloads.+- Use `-v /path/to/models/:/root/.rembg` to store model files outside the container, avoiding re-downloads.## Models-Local ONNX models are automatically downloaded and saved to `~/.u2net/` on first use. The `withoutbg` model is a cloud API backend and does not download a local model.+Local ONNX models are automatically downloaded on first use and saved under `~/.rembg/models/` (see [Where models are stored](#where-models-are-stored)). The `withoutbg` model is a cloud API backend and does not download a local model.### Available Models@@ -487,7 +488,7 @@ Local ONNX models are automatically downloaded and saved to `~/.u2net/` on first- birefnet-hrsod ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet-HRSOD_DHU-epoch_115.onnx), [source](https://github.com/ZhengPeng7/BiRefNet)): A pre-trained model for high-resolution salient object detection (HRSOD).- birefnet-cod ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet-COD-epoch_125.onnx), [source](https://github.com/ZhengPeng7/BiRefNet)): A pre-trained model for concealed object detection (COD).- birefnet-massive ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet-massive-TR_DIS5K_TR_TEs-epoch_420.onnx), [source](https://github.com/ZhengPeng7/BiRefNet)): A pre-trained model with massive dataset.-- bria-rmbg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/bria-rmbg-2.0.onnx), [source](https://huggingface.co/briaai/RMBG-2.0)): A state-of-the-art background removal model by BRIA AI.+- bria-rmbg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/bria-rmbg-2.0.onnx), [source](https://huggingface.co/briaai/RMBG-2.0)): **The default.** A state-of-the-art background removal model by BRIA AI. At ~1.02 GB it is the largest local model here and runs at 1024x1024, so it is slower than u2net; pass `-m u2net` for a smaller, faster model with blockier edges. Note that RMBG-2.0 is released under a [BRIA license](https://huggingface.co/briaai/RMBG-2.0) that requires a paid agreement for commercial use. Model weights carry their own licenses, independent of rembg's MIT license — check the linked source before using any model commercially.- withoutbg ([API](https://withoutbg.com/?ref=rembg), [sample results](https://withoutbg.com/pro-model/results?ref=rembg)): Cloud API backend. [Get 50 free credits with signup](https://withoutbg.com/signup?ref=rembg). Pass `api_key` via `-x` / `new_session(...)`, or set `WITHOUTBG_API_KEY`. Images are sent to withoutBG's servers; max upload size is 20 MB.## Environment Variablesdiff --git a/USAGE.md b/USAGE.mdindex e66cd87..83da999 100644--- a/USAGE.md+++ b/USAGE.md@@ -16,7 +16,7 @@ input = Image.open(input_path)### Without additional arguments-This defaults to the `u2net` model.+This defaults to the `bria-rmbg` model.```pythonoutput = remove(input)diff --git a/rembg/bg.py b/rembg/bg.pyindex 6814857..339371d 100644--- a/rembg/bg.py+++ b/rembg/bg.py@@ -274,7 +274,7 @@ def remove("""Remove the background from an input image.- This function takes in various parameters and returns a modified version of the input image with the background removed. The function can handle input data in the form of bytes, a PIL image, or a numpy array. The function first checks the type of the input data and converts it to a PIL image if necessary. It then fixes the orientation of the image and proceeds to perform background removal using the 'u2net' model. The result is a list of binary masks representing the foreground objects in the image. These masks are post-processed and combined to create a final cutout image. If a background color is provided, it is applied to the cutout image. The function returns the resulting cutout image in the format specified by the input 'return_type' parameter or as python bytes if force_return_bytes is true.+ This function takes in various parameters and returns a modified version of the input image with the background removed. The function can handle input data in the form of bytes, a PIL image, or a numpy array. The function first checks the type of the input data and converts it to a PIL image if necessary. It then fixes the orientation of the image and proceeds to perform background removal using the 'bria-rmbg' model. The result is a list of binary masks representing the foreground objects in the image. These masks are post-processed and combined to create a final cutout image. If a background color is provided, it is applied to the cutout image. The function returns the resulting cutout image in the format specified by the input 'return_type' parameter or as python bytes if force_return_bytes is true.Parameters:data (Union[bytes, PILImage, np.ndarray]): The input image data.@@ -282,7 +282,7 @@ def remove(alpha_matting_foreground_threshold (int, optional): Foreground threshold for alpha matting. Defaults to 240.alpha_matting_background_threshold (int, optional): Background threshold for alpha matting. Defaults to 10.alpha_matting_erode_size (int, optional): Erosion size for alpha matting. Defaults to 10.- session (Optional[BaseSession], optional): A session object for the 'u2net' model. Defaults to None.+ session (Optional[BaseSession], optional): A session object for the 'bria-rmbg' model. Defaults to None.only_mask (bool, optional): Flag indicating whether to return only the binary masks. Defaults to False.post_process_mask (bool, optional): Flag indicating whether to post-process the masks. Defaults to False.decontaminate (bool, optional): Flag indicating whether to remove the background color fringing left on soft edges. Ignored when alpha_matting is on. Defaults to False.@@ -316,7 +316,7 @@ def remove(img = fix_image_orientation(img)if session is None:- session = new_session("u2net", *args, **kwargs)+ session = new_session("bria-rmbg", *args, **kwargs)masks = session.predict(img, *args, **kwargs)cutouts = []diff --git a/rembg/commands/b_command.py b/rembg/commands/b_command.pyindex 402e17e..e630628 100644--- a/rembg/commands/b_command.py+++ b/rembg/commands/b_command.py@@ -20,7 +20,7 @@ from ..sessions import sessions_names@click.option("-m","--model",- default="u2net",+ default="bria-rmbg",type=click.Choice(sessions_names),show_default=True,show_choices=True,diff --git a/rembg/commands/i_command.py b/rembg/commands/i_command.pyindex cc9fe99..12077b6 100644--- a/rembg/commands/i_command.py+++ b/rembg/commands/i_command.py@@ -17,7 +17,7 @@ from ..sessions import sessions_names@click.option("-m","--model",- default="u2net",+ default="bria-rmbg",type=click.Choice(sessions_names),show_default=True,show_choices=True,diff --git a/rembg/commands/p_command.py b/rembg/commands/p_command.pyindex 4dcd462..6bb85a2 100644--- a/rembg/commands/p_command.py+++ b/rembg/commands/p_command.py@@ -86,7 +86,7 @@ def _confirm_usage_cost(@click.option("-m","--model",- default="u2net",+ default="bria-rmbg",type=click.Choice(sessions_names),show_default=True,show_choices=True,diff --git a/rembg/commands/s_command.py b/rembg/commands/s_command.pyindex f3e2e3a..bc7ca52 100644--- a/rembg/commands/s_command.py+++ b/rembg/commands/s_command.py@@ -113,7 +113,7 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -model: str = Query(description="Model to use when processing image",regex=r"(" + "|".join(sessions_names) + ")",- default="u2net",+ default="bria-rmbg",),a: bool = Query(default=False, description="Enable Alpha Matting"),af: int = Query(@@ -160,7 +160,7 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -model: str = Form(description="Model to use when processing image",regex=r"(" + "|".join(sessions_names) + ")",- default="u2net",+ default="bria-rmbg",),a: bool = Form(default=False, description="Enable Alpha Matting"),af: int = Form(@@ -425,7 +425,9 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -inference,[gr.components.Image(type="pil", label="Input"),- gr.components.Dropdown(sessions_names, value="u2net", label="Models"),+ gr.components.Dropdown(+ sessions_names, value="bria-rmbg", label="Models"+ ),gr.components.Checkbox(value=True, label="Alpha matting"),gr.components.Slider(value=240, minimum=0, maximum=255, label="Foreground threshold"diff --git a/rembg/session_factory.py b/rembg/session_factory.pyindex be5e791..f3e236a 100644--- a/rembg/session_factory.py+++ b/rembg/session_factory.py@@ -8,7 +8,7 @@ from .sessions.base import BaseSessiondef new_session(- model_name: str = "u2net",+ model_name: str = "bria-rmbg",*args,sess_opts: Optional[ort.SessionOptions] = None,**kwargs,