Sign inSign up

anodev/image-background-remove-tool

By anodev

โ€ขUpdated over 5 years ago

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

Image
0

298

anodev/image-background-remove-tool repository overview

โ DEPRECATED - see anodev/carvekit

โ ๐Ÿฅง Image Background Remove Tool ๐Ÿฅง Test

Tool for removing background from image using neural networks.


โ ๐Ÿ“„ Description:

The program removes the background from photos.


โ ๐ŸŽ† Features:

  • GUI
  • Removes background from hair
  • Significantly improved output image quality
  • Removes background from image without loss of image resolution
  • All models support processing both on the video card and on the processor
  • Added support for new neural networks (U^2-NETโ , BASNetโ ) on PyTorch
  • Updated DeepLabv3 core and moved to PyTorch implementation with ResNet 101 backbone.
  • 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
  • Added flask http api, fully compatible with remove.bg api libraries. Just change the endpoint url and voila!

โ โ›ฑ Try this program yourself on Google Colabโ 


โ ๐ŸŽ“ Implemented Neural Networks:


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

โ ๐Ÿ” Preprocessing methods:
  • None (default) - No preprocessing methods used.
  • bbd-fastrcnn - 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.
  • fba (default) - This algorithm improves the borders of the image when removing the background from images with hair, etc. using FBA Mattingโ  neural network. This method gives the best result in combination with u2net without any preprocessing methods.
  • rtb-bnb - 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.

โ ๐Ÿงท Dependencies:

  • See requirements.txt
  • See requirements_http.txt, if you need http api.

Note: 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 (torch, mxnet, gluoncv and others) designed to work with your video card.


โ ๐Ÿท Setup for Windows:

  • Clone this repository
  • Install all the dependencies from requirements.txt via pip3 install -r requirements.txt
  • Run python3 setup.py

This setup.bat script loads the trained model.
The install script also supports installing models using arguments. For more information, run python3 setup.py --help.
The program was tested on python version 3.7.3


โ ๐Ÿท 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 python3 setup.py and select the model you need.

This setup.py script loads the pre-trained model.
The install script also supports installing models using arguments. For more information, run python3 setup.py --help.
The program was tested on python version 3.7.3


โ ๐Ÿ–ผ๏ธ GUI screenshot:


โ ๐Ÿ–ต Running the GUI app:

python3 gui.py


โ ๐Ÿ“ฆ Running the HTTP API server:

โ ๐Ÿงฒ With defaults:

python3 http_api.py

โ ๐Ÿงฒ With custom arguments:

python3 http_api.py -auth false -port 5000 -host 0.0.0.0 -m u2net -pre None -post fba

โ โฉ Example usage with curl:
curl -H 'X-API-Key: test'                                   \
       -F 'image_file=@/home/user/test.jpg'                 \
       -F 'size=auto'                                       \ # oneOf 'preview', 'medium', 'hd', 'auto'
       -f http://localhost:5000/api/removebg -o no-bg.png

โ ๐Ÿ“ฆ Running the HTTP API server via docker:

Using the API via docker is a fast and non-complex way to have a working API.
The docker image uses u2net as default and runs without authentication.

โ ๐Ÿ’ป Using an already built image from DockerHub:
docker run -d --restart unless-stopped \
 --name image-background-remove-tool \
 -p 5000:5000 \
 -e HOST='0.0.0.0'   \
 -e PORT='5000'  \
 -e AUTH='false'  \
 -e MODEL='u2net'  \
 -e PREPROCESSING='None'  \
 -e POSTPROCESSING='fba'  \
 -e ADMIN_TOKEN='admin'  \
 -e ALLOWED_TOKENS_PYTHON_ARR='["test"]'  \
 -e IS_DOCKER_CONTAINER='true'  \
docker.io/anodev/image-background-remove-tool:release 
โ ๐Ÿ”จ Building your own image:
  • Build the docker image
docker build --tag image-background-remove-tool:latest .
  • Start a container from the image
docker run -d --restart unless-stopped \
 --name image-background-remove-tool \
 -p 5000:5000 \
 -e HOST='0.0.0.0'   \
 -e PORT='5000'  \
 -e AUTH='false'  \
 -e MODEL='u2net'  \
 -e PREPROCESSING='None'  \
 -e POSTPROCESSING='fba'  \
 -e ADMIN_TOKEN='admin'  \
 -e ALLOWED_TOKENS_PYTHON_ARR='["test"]'  \
 -e IS_DOCKER_CONTAINER='true'  \
image-background-remove-tool:latest

โ ๐Ÿงฐ Running the script:

  • python3 main.py -i <input_path> -o <output_path> -m <model_type> -pre <preprocessing_method> -post <postprocessing_method> --recursive
โ Explanation of args:
  • -i <input_path> - path to input file or dir.
  • -o <output_path> - path to output file or dir.
  • -pre <preprocessing_method> - Preprocessing method. Can be bbd-fastrcnn or bbmd-maskrcnn or None. None is better to use.
  • -post <postprocessing_method> - Postprocessing method. Can be fba or rtb-bnb or rtb-bnb2 or No. fba is better to use.
  • -m <model_type> - can be u2net or basnet or u2netp or deeplabv3. u2net is better to use.
  • --recursive - Enables recursive search for images in a folder
    DeepLabV3 model designed to remove the background from PORTRAIT photos or PHOTOS WITH ANIMALS!
    More info about models.โ 

Note: See example scripts in docs/shell_examples/ for more information on using the program.


โ โณ TODO:

1) Check TODOs in code.
2) Implement support for Mask RCNN. (90% done)
3) Add an algorithm for automatic color correction at image borders. (0% 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/None/fba):
  • Output
  • Output(deeplabv3/None/fba):
  • Output
  • Output(basnet/None/fba):
  • Output
  • Output(u2netp/None/fba):
  • Output

Tag summary

Content type

Image

Digest

Size

2.6 GB

Last updated

over 5 years ago

docker pull anodev/image-background-remove-tool:merge-to-nontested