Software and Hardware Environment

  • windows 10 64bit
  • Pytorch 1.7
  • rembg

Introduction to the

Rembg is a Python tool to remove the background of images. It is not only available in Python, but also provides an executable program, which is very convenient.

The installation

Install directly through PIP

pip install rembg
Copy the code

Or pull the source code from Github and install it

git clone https://github.com/danielgatis/rembg.git
cd rembg
pip install -r requirements.txt
Copy the code

Once installed, the REMbg executable is generated, which you can then use for testing

Using the command line

First download the model file. Here are two addresses:

  • Google Web disk links

  • Baidu network backup link: pan.baidu.com/s/1IJOZiELP… , extract code: LGUg

After the file is downloaded, copy it to the ~/.u2net folder. By default, the folder does not exist. You need to create the folder by yourself

Now you can test with your local images

rembg -o output_test.jpg test.jpg
Copy the code

-o indicates the processed image

In addition to working with individual images, Rembg also supports folder processing, where the above two images are placed in a folder and then the command is used

rembg -p test
Copy the code

Rembg also provides the Interface on the Web side to run the rembg-server to start the back-end service, which uses the Flask framework

Then type it in your browser

http://127.0.0.1:5000/?url=https://image.xugaoxiang.com/imgs/2020/12/bf5f5553a596319b.jpg
Copy the code

After url= is the address of the image, you can see that Rembg supports HTTPS

Call rembg in the code

Look at the following example

# app.py
import sys
from rembg.bg import remove

sys.stdout.buffer.write(remove(sys.stdin.buffer.read()))
Copy the code

It is then executed in the terminal

cat bryant.jpg | python app.py > output.png
Copy the code

The code runs the same as above

The resources

  • Github.com/danielgatis…
  • Arxiv.org/pdf/2005.09…
  • Github.com/NathanUA/U-…
  • Github.com/pymatting/p…
  • pytorch.org/
  • Xugaoxiang.com/2019/12/08/…