Rembg is a tool to remove images background
better readme
Daniel Gatis08e0dfac7e75
diff --git a/README.md b/README.mdindex 85dab69..af241e9 100644--- a/README.md+++ b/README.md@@ -1,14 +1,16 @@# Rembg-[](https://repomapr.com/danielgatis/rembg)--[](https://img.shields.io/pypi/dm/rembg.svg)[](https://img.shields.io/badge/License-MIT-blue.svg)[](https://huggingface.co/spaces/KenjieDec/RemBG)[](https://bgremoval.streamlit.app/)[](https://colab.research.google.com/github/danielgatis/rembg/blob/main/rembg.ipynb)+[](https://repomapr.com/danielgatis/rembg)++<p align="center">+ <img src="logo.png" alt="Rembg Logo" width="600" />+</p>-Rembg is a tool to remove images background.+Rembg is a tool to remove image backgrounds. It can be used as a CLI, Python library, HTTP server, or Docker container.**If this project has helped you, please consider making a [donation](https://www.buymeacoffee.com/danielgatis).**@@ -52,46 +54,42 @@ pip install "rembg[cpu,cli]" # for library + cli### GPU support (NVIDIA/CUDA)-First of all, you need to check if your system supports the `onnxruntime-gpu`.--Go to [onnxruntime.ai](<https://onnxruntime.ai/getting-started>) and check the installation matrix.+First, check if your system supports `onnxruntime-gpu` by visiting [onnxruntime.ai](https://onnxruntime.ai/getting-started) and reviewing the installation matrix.<p style="display: flex;align-items: center;justify-content: center;"><img alt="onnxruntime-installation-matrix" src="./onnxruntime-installation-matrix.png" width="400" /></p>-If yes, just run:+If your system is compatible, run:```bashpip install "rembg[gpu]" # for librarypip install "rembg[gpu,cli]" # for library + cli```-Nvidia GPU may require onnxruntime-gpu, cuda, and cudnn-devel. [#668](https://github.com/danielgatis/rembg/issues/668#issuecomment-2689830314) . If rembg[gpu] doesn't work and you can't install cuda or cudnn-devel, use rembg[cpu] and onnxruntime instead.+> **Note:** NVIDIA GPUs may require `onnxruntime-gpu`, CUDA, and `cudnn-devel`. See [#668](https://github.com/danielgatis/rembg/issues/668#issuecomment-2689830314) for details. If `rembg[gpu]` doesn't work and you can't install CUDA or `cudnn-devel`, use `rembg[cpu]` with `onnxruntime` instead.-### GPU support (AMD/ROCM):+### GPU support (AMD/ROCm)-ROCM support requires the `onnxruntime-rocm` package. Install it following-[AMD's documentation](https://rocm.docs.amd.com/projects/radeon/en/latest/docs/install/native_linux/install-onnx.html).+ROCm support requires the `onnxruntime-rocm` package. Install it by following [AMD's documentation](https://rocm.docs.amd.com/projects/radeon/en/latest/docs/install/native_linux/install-onnx.html).-If `onnxruntime-rocm` is installed and working, install the `rembg[rocm]`-version of rembg:+Once `onnxruntime-rocm` is installed and working, install rembg with ROCm support:```bashpip install "rembg[rocm]" # for librarypip install "rembg[rocm,cli]" # for library + cli```-## Usage as a cli+## Usage as a CLI-After the installation step you can use rembg just typing `rembg` in your terminal window.+After installation, you can use rembg by typing `rembg` in your terminal.The `rembg` command has 4 subcommands, one for each input type:-- `i` for files-- `p` for folders-- `s` for http server-- `b` for RGB24 pixel binary stream+- `i` - single files+- `p` - folders (batch processing)+- `s` - HTTP server+- `b` - RGB24 pixel binary streamYou can get help about the main command using:@@ -99,7 +97,7 @@ You can get help about the main command using:rembg --help```-As well, about all the subcommands using:+You can also get help for any subcommand:```shellrembg <COMMAND> --help@@ -107,63 +105,61 @@ rembg <COMMAND> --help### rembg `i`-Used when input and output are files.+Used for processing single files.-Remove the background from a remote image+**Remove background from a remote image:**```shellcurl -s http://input.png | rembg i > output.png```-Remove the background from a local file+**Remove background from a local file:**```shellrembg i path/to/input.png path/to/output.png```-Remove the background specifying a model+**Specify a model:**```shellrembg i -m u2netp path/to/input.png path/to/output.png```-Remove the background returning only the mask+**Return only the mask:**```shellrembg i -om path/to/input.png path/to/output.png```-Remove the background applying an alpha matting+**Apply alpha matting:**```shellrembg i -a path/to/input.png path/to/output.png```-Passing extras parameters+**Pass extra parameters (SAM example):**```shell-SAM example-rembg i -m sam -x '{ "sam_prompt": [{"type": "point", "data": [724, 740], "label": 1}] }' examples/plants-1.jpg examples/plants-1.out.png```-```shell-Custom model example+**Pass extra parameters (custom model):**+```shellrembg i -m u2net_custom -x '{"model_path": "~/.u2net/u2net.onnx"}' path/to/input.png path/to/output.png```### rembg `p`-Used when input and output are folders.+Used for batch processing entire folders.-Remove the background from all images in a folder+**Process all images in a folder:**```shellrembg p path/to/input path/to/output```-Same as before, but watching for new/changed files to process+**Watch mode (process new/changed files automatically):**```shellrembg p -w path/to/input path/to/output@@ -171,109 +167,99 @@ rembg p -w path/to/input path/to/output### rembg `s`-Used to start http server.+Used to start an HTTP server.```shellrembg s --host 0.0.0.0 --port 7000 --log_level info```-To see the complete endpoints documentation, go to: `http://localhost:7000/api`.+For complete API documentation, visit: `http://localhost:7000/api`-Remove the background from an image url+**Remove background from an image URL:**```shellcurl -s "http://localhost:7000/api/remove?url=http://input.png" -o output.png```-Remove the background from an uploaded image+**Remove background from an uploaded image:**```shell-curl -s -F file=@/path/to/input.jpg "http://localhost:7000/api/remove" -o output.png+curl -s -F file=@/path/to/input.jpg "http://localhost:7000/api/remove" -o output.png```### rembg `b`-Process a sequence of RGB24 images from stdin. This is intended to be used with another program, such as FFMPEG, that outputs RGB24 pixel data to stdout, which is piped into the stdin of this program, although nothing prevents you from manually typing in images at stdin.+Process a sequence of RGB24 images from stdin. This is intended to be used with programs like FFmpeg that output RGB24 pixel data to stdout.```shell-rembg b image_width image_height -o output_specifier+rembg b <width> <height> -o <output_specifier>```-Arguments:+**Arguments:**-- image_width : width of input image(s)-- image_height : height of input image(s)-- output_specifier: printf-style specifier for output filenames, for example if `output-%03u.png`, then output files will be named `output-000.png`, `output-001.png`, `output-002.png`, etc. Output files will be saved in PNG format regardless of the extension specified. You can omit it to write results to stdout.+| Argument | Description |+|----------|-------------|+| `width` | Width of input image(s) |+| `height` | Height of input image(s) |+| `output_specifier` | Printf-style specifier for output filenames (e.g., `output-%03u.png` produces `output-000.png`, `output-001.png`, etc.). Omit to write to stdout. |-Example usage with FFMPEG:+**Example with FFmpeg:**```shellffmpeg -i input.mp4 -ss 10 -an -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1280 720 -o folder/output-%03u.png```-The width and height values must match the dimension of output images from FFMPEG. Note for FFMPEG, the "`-an -f rawvideo -pix_fmt rgb24 pipe:1`" part is required for the whole thing to work.+> **Note:** The width and height must match FFmpeg's output dimensions. The flags `-an -f rawvideo -pix_fmt rgb24 pipe:1` are required for FFmpeg compatibility.-## Usage as a library+## Usage as a Library-Input and output as bytes+**Input and output as bytes:**```pythonfrom rembg import remove-input_path = 'input.png'-output_path = 'output.png'--with open(input_path, 'rb') as i:- with open(output_path, 'wb') as o:+with open('input.png', 'rb') as i:+ with open('output.png', 'wb') as o:input = i.read()output = remove(input)o.write(output)```-Input and output as a PIL image+**Input and output as a PIL image:**```pythonfrom rembg import removefrom PIL import Image-input_path = 'input.png'-output_path = 'output.png'--input = Image.open(input_path)+input = Image.open('input.png')output = remove(input)-output.save(output_path)+output.save('output.png')```-Input and output as a numpy array+**Input and output as a NumPy array:**```pythonfrom rembg import removeimport cv2-input_path = 'input.png'-output_path = 'output.png'--input = cv2.imread(input_path)+input = cv2.imread('input.png')output = remove(input)-cv2.imwrite(output_path, output)+cv2.imwrite('output.png', output)```-Force output as bytes+**Force output as bytes:**```pythonfrom rembg import remove-input_path = 'input.png'-output_path = 'output.png'--with open(input_path, 'rb') as i:- with open(output_path, 'wb') as o:+with open('input.png', 'rb') as i:+ with open('output.png', 'wb') as o:input = i.read()output = remove(input, force_return_bytes=True)o.write(output)```-How to iterate over files in a performatic way+**Batch processing with session reuse (recommended for performance):**```pythonfrom pathlib import Path@@ -292,45 +278,48 @@ for file in Path('path/to/folder').glob('*.png'):o.write(output)```-To see a full list of examples on how to use rembg, go to the [examples](USAGE.md) page.+For more examples, see the [examples](USAGE.md) page.-## Usage as a docker+## Usage with Docker-### Only CPU+### CPU Only-Just replace the `rembg` command for `docker run danielgatis/rembg`.--Try this:+Replace the `rembg` command with `docker run danielgatis/rembg`:```shelldocker run -v path/to/input:/rembg danielgatis/rembg i input.png path/to/output/output.png```-### Nvidia CUDA Hardware Acceleration+### NVIDIA CUDA GPU Acceleration++**Requirements:** Your host must have the [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html) installed.-Requirement: using CUDA in docker needs your **host** has **NVIDIA Container Toolkit** installed. [NVIDIA Container Toolkit Install Guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)+CUDA acceleration requires `cudnn-devel`, so you need to build the Docker image yourself. See [#668](https://github.com/danielgatis/rembg/issues/668#issuecomment-2689914205) for details.-**Nvidia CUDA Hardware Acceleration** needs cudnn-devel so you need to build the docker image by yourself. [#668](https://github.com/danielgatis/rembg/issues/668#issuecomment-2689914205)+**Build the image:**-Here is a example shows you how to build an image and name it *rembg-nvidia-cuda-cudnn-gpu*```shelldocker build -t rembg-nvidia-cuda-cudnn-gpu -f Dockerfile_nvidia_cuda_cudnn_gpu .```-Be aware: It would take 11GB of your disk space. (The cpu version only takes about 1.6GB). Models didn't included.-After you build the image, run it like this as a cli+> **Note:** This image requires ~11GB of disk space (CPU version is ~1.6GB). Models are not included.++**Run the container:**+```shellsudo docker run --rm -it --gpus all -v /dev/dri:/dev/dri -v $PWD:/rembg rembg-nvidia-cuda-cudnn-gpu i -m birefnet-general input.png output.png```-- Trick 1: Actually you can also make up a nvidia-cuda-cudnn-gpu image and install rembg[gpu, cli] in it.-- Trick 2: Try param `-v /somewhereYouStoresModelFiles/:/root/.u2net` so to download/store model files out of docker images. You can even comment the line `RUN rembg d u2net` so when building the image, it download will no models, so you can download the specific model you want even without the default u2net model.+**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.## Models-All models are downloaded and saved in the user home folder in the `.u2net` directory.+All models are automatically downloaded and saved to `~/.u2net/` on first use.-The available models are:+### Available Models- u2net ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx), [source](https://github.com/xuebinqin/U-2-Net)): A pre-trained model for general use cases.- u2netp ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2netp.onnx), [source](https://github.com/xuebinqin/U-2-Net)): A lightweight version of u2net model.@@ -347,22 +336,21 @@ The available models are:- 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.-### How to train your own model--If You need more fine tuned models try this:-<https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289>+### Training Your Own Model+For fine-tuned models, see [this discussion](https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289).## FAQ-### When will this library provide support for Python version 3.xx?+### When will this library support Python version 3.xx?-This library directly depends on the [onnxruntime](https://pypi.org/project/onnxruntime) library. Therefore, we can only update the Python version when [onnxruntime](https://pypi.org/project/onnxruntime) provides support for that specific version.+This library depends on [onnxruntime](https://pypi.org/project/onnxruntime). Python version support is determined by onnxruntime's compatibility.-## Buy me a coffee+## Support-Liked some of my work? Buy me a coffee (or more likely a beer)+If you find this project useful, consider buying me a coffee (or a beer):<a href="https://www.buymeacoffee.com/danielgatis" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;"></a>@@ -374,4 +362,4 @@ Liked some of my work? Buy me a coffee (or more likely a beer)Copyright (c) 2020-present [Daniel Gatis](https://github.com/danielgatis)-Licensed under [MIT License](./LICENSE.txt)+Licensed under the [MIT License](./LICENSE.txt).diff --git a/logo.png b/logo.pngnew file mode 100644index 0000000..0cfebb6Binary files /dev/null and b/logo.png differ