/image-background-remove-tool

๐Ÿฅง A tool for removing background from photos with neural networks ๐Ÿฅง

Primary LanguagePythonApache License 2.0Apache-2.0

๐Ÿฅง Image Background Remove Tool ๐Ÿฅง Test release version

Tool for removing background from image using neural networks


๐Ÿ“„ Description:

The program removes the background from photos


๐ŸŽ† Features:

  • Added support for new neural networks (U^2-NET, BASNet) on PyTorch
  • Significantly improved output image quality
  • Added GUI by @Munawwar
  • Tensorflow 2.0 compatible
  • All models support processing both on the video card and on the processor
  • tqdm progress bar
  • The program has a lot of methods for image preprocessing and post-processing, which allows you to configure the quality and speed of image processing for your needs
  • Removes background from image without loss of image resolution
  • The script not only processes a single file, but can also process all images from the input folder and save them in the output folder with the same name
  • Implemented support for the neural network from this script and improved the result of its work

โ›ฑ Try this program yourself on Google Colab


๐ŸŽ“ Implemented Neural Networks:

More info about models.


๐Ÿ–ผ๏ธ Image pre-processing and post-processing methods:

๐Ÿ” Preprocessing methods:

  • None - No preprocessing methods used.
  • bbd-fastrcnn (default) - This image pre-processing technique uses two neural networks ($used_model and Fast RCNN) to first detect the boundaries of objects in a photograph, cut them out, sequentially remove the background from each object in turn and subsequently collect the entire image from separate parts.
  • bbmd-maskrcnn - This image pre-processing technique uses two neural networks ($used_model and Mask RCNN) to first detect the boundaries and masks of objects in a photograph, cut them out, expand the masks by a certain number of pixels, apply them and remove the background from each object in turn and subsequently collect the entire image from separate parts. So far it works very poorly!

โœ‚ Post-processing methods:

  • No - No post-processing methods used.
  • rtb-bnb (default) - This algorithm improves the boundaries of the image obtained from the neural network. It is based on the principle of removing too transparent pixels and smoothing the borders after removing too transparent pixels.
  • rtb-bnb2 - This algorithm improves the boundaries of the image obtained from the neural network. It is based on the principle of removing too transparent pixels and smoothing the borders after removing too transparent pixels. The algorithm performs this procedure twice. For the first time, the algorithm processes the image from the neural network, then sends the processed image back to the neural network, and then processes it again and returns it to the user. This method gives the best result in combination with u2net without any preprocessing methods.

๐Ÿงท Dependencies:

  • See requirements.txt

Note: You can choose what to install PyTorch or TensorFlow, based on which model you want to use.
PyTorch for u2net, u2netp
TensorFlow for xception_model, mobile_net_model
Mxnet and Gluoncv are used for image preprocessing methods and are installed optionally.
Also, to speed up image processing by performing all the calculations on the video card, install separately special versions of the dependencies (tensorflow, torch, mxnet, gluoncv and others) designed to work with your video card.
TensorFlow models are not recommended for use, since these models have much worse quality and lower image processing speed, also these models are designed solely to remove the background from portrait photos and photos with animals.


๐Ÿท Setup for Windows:

  • Clone this repository
  • Install all the dependencies from requirements.txt via pip3 install -r requirements.txt
  • Run ./setup.bat This setup.bat script loads the trained model.

๐Ÿท Setup for Linux:

  • Clone repository: git clone https://github.com/OPHoperHPO/image-background-remove-tool
  • Install all the dependencies from requirements.txt: pip3 install -r requirements.txt
  • Run ./setup.sh and select the model you need. This setup.sh script loads the pre-trained model.

๐Ÿ–ต Running GUI app:

python3 gui.py


๐Ÿงฐ Running the script:

  • python3 main.py -i <input_path> -o <output_path> -m <model_type> -prep <preprocessing_method> -postp <postprocessing_method>

Explanation of args:

  • -i <input_path> - path to input file or dir.
  • -o <output_path> - path to output file or dir.
  • -prep <preprocessing_method> - Preprocessing method. Can be bbd-fastrcnn or bbmd-maskrcnn or None . bbd-fastrcnn is better to use.
  • -postp <postprocessing_method> - Postprocessing method. Can be rtb-bnb or rtb-bnb2 or No . rtb-bnb is better to use.
  • -m <model_type> - can be u2net or basnet or u2netp or xception_model or mobile_net_model. u2net is better to use.
    DeepLab models (xception_model or mobile_net_model) are outdated and designed to remove the background from PORTRAIT photos or PHOTOS WITH ANIMALS!
    More info about models.

Note: See example scripts for more information on using the program.


โณ TODO:

1) Check TODOs in code.
2) Implement support for Mask RCNN. (90% done)

๐Ÿ‘ช Credits: More info


๐Ÿ’ต Support me:

You can thank me for developing any of my projects, provide financial support for developing new projects and buy me a small cup of coffee.โ˜•
Just support me on these platforms:


๐Ÿ˜€ Sample Result:

  • More sample images in docs/imgs/input/ and docs/imgs/examples/ folders.
    Examples of images from the background are contained in folders in the following format: {model_name}/{preprocessing_method_name}/{postprocessing_method_name}
  • Input:
  • Input
  • Output(u2net/bbd-fastrcnn/rtb-bnb):
  • Output
  • Output(basnet/bbd-fastrcnn/rtb-bnb):
  • Output
  • Output(u2netp/bbd-fastrcnn/rtb-bnb):
  • Output
  • Output(xception_model/bbd-fastrcnn/rtb-bnb):
  • Output
  • Output(mobile_net_model/bbd-fastrcnn/rtb-bnb):
  • Output