The environment

  • windows 10 64-bit
  • Python 3.8.11
  • Backgroundremover 0.1.9

preface

Backgroundremover is an open-source, free command line tool designed to remove background from images and videos. It is written in Python and the project address is github.com/nadermx/bac… .

Software installation

You can install using PIP

pip install backgroundremover
Copy the code

Of course, download the source code to install is also possible

git clone https://github.com/nadermx/backgroundremover.git
cd backgroundremover
python setup.py install
Copy the code

The latest version is 0.1.9, and the subsequent tests are also based on this version.

Next you need to install FFMPEG, which is used for video processing. Download the compiled binaries from the official website ffmpeg.org/download.ht… , try to download the newer version and add its path to the system environment variables

Install torch. Since the machine has a GPU, install torch of GPU version 1.7 or above

PIP install Torch ==1.7.1+cu101 TorchVision ==0.8.2+ Cu101 Torchaudio ==0.7.2 -f https://download.pytorch.org/whl/torch_stable.htmlCopy the code

Method of use

Prepare an original image with a background

To remove the background, execute the following command

backgroundremover -i original.jpg -o output.png
Copy the code

During the operation, Google Driver will download the required U2NET model, but it cannot be accessed due to the domestic network environment

At this point, you can download it through the link below

u2net.pth u2net_human_seg.pth

Also provide a web disk address

Link: pan.baidu.com/s/1ZYzV0SYE… Extraction code: SRIx

After downloading the model file, put it in the ~/.u2net folder, if the folder does not exist, create your own

When you are ready, repeat the instructions above to remove the background of the image

Backgroundremover can use three models to memorize pictures and videos, which are U2NETp, U2NET and U2net_human_seg

For example, to use u2net_human_seg, use the -m argument

backgroundremover -i original.jpg -m u2net_human_seg -o output.png
Copy the code

If you want to process video, you can do this

backgroundremover -i test.mp4 -tv -o output.mov
Copy the code

The background of the video is removed and superimposed on another video

backgroundremover -i test.mp4 -tov -tv videtobeoverlayed.mp4 -o output.mov
Copy the code

Video goes behind generating GIFs

backgroundremover -i test.mp4 -tg -o output.gif
Copy the code

For more scenarios and parameter Settings, see the official documents

The resources

  • Github.com/nadermx/bac…
  • Github.com/nadermx/bac…
  • Github.com/xuebinqin/U…