This repo contains set of scripts for automatic one-shot image background subtraction task using the following strategies:
- the appropriate background subtraction services (mostly human based):
git clone https://github.com/osmr/bgsub.git
cd bgsub
pip install -r requirements.txt
- Launch a script for background subtraction via benzin.io/remove.bg service:
python subtract_bg_service.py --service=<service> --token=<your token> --input=<directory with images> --output=<output directory with binary masks>
Here:
serviceisbenzinioforbenzin.ioservice orremovebgforremove.bg,tokenis a service API token value, which you will receive after registering on the selected service,inputis a directory with processing JPEG images (can contain subdirectories),outputis a directory with resulted PNG binary masks (it is assumed that all original images had unique names).
Optional parameters:
middleis a directory with intermediate images with original masks obtained from the service (PNG with alpha mask),ppdiris a flag for adding extra parrent+parrent directory to the output one (should use as--ppdir).thresholdis a threshold for mask binarization (default value is 127),urlis an optional custom URL for service,jpgis a flag for forced recompression an input image as JPG (should use as--jpg),not-resizeis a flag for suppressing forcible scale the mask to the input image (should use as--not-resize).
- Launch a script for background subtraction via human segmentation network:
python subtract_bg_human.py --input=<directory with images> --output=<output directory with binary masks>
Here:
inputis a directory with processing JPEG images (can contain subdirectories),outputis a directory with resulted PNG binary masks (it is assumed that all original images had unique names).
Optional parameters:
ppdiris a flag for adding extra parrent+parrent directory to the output one (should use as--ppdir).use-cudais a flag for using CUDA for network inference (should use as--use-cuda).
- Launch a script for background subtraction via matting network:
python subtract_bg_matting.py --input=<directory with images> --bg=<background image path> --output=<output directory with binary masks>
Here:
inputis a directory with processing JPEG images (can contain subdirectories),bgis a background image file path,outputis a directory with resulted PNG binary masks (it is assumed that all original images had unique names).
Optional parameters:
thresholdis a threshold for mask binarization (default value is 127),ppdiris a flag for adding extra parrent+parrent directory to the output one (should use as--ppdir).use-cudais a flag for using CUDA for network inference (should use as--use-cuda).
The script does not recalculate the masks if the target images already exist.