plemeri / transparent-background

This is a background removing tool powered by InSPyReNet (ACCV 2022)
MIT License
732 stars 80 forks source link
background-removal deep-learning dichotomous-image-segmentation image-matting image-processing photo-editing python pytorch remove-background remove-background-image remove-background-video salient-object-detection video-editing

Transparent Background

This is a background removing tool powered by InSPyReNet (ACCV 2022). You can easily remove background from the image or video or bunch of other stuffs when you can make the background transparent!

Image Video Webcam

:rotating_light: Notice

:newspaper: News

:inbox_tray: Installation

Dependencies (python packages)

package version (>=)
pytorch 1.7.1
torchvision 0.8.2
opencv-python 4.6.0.66
timm 0.6.11
tqdm 4.64.1
kornia 0.5.4
gdown 4.5.4
pyvirtualcam (optional) 0.6.0

Note: If you have any problem with pyvirtualcam, please visit their github repository or pypi homepage. Due to the backend workflow for Windows and macOS, we only support Linux for webcam input.

Dependencies

1. Webcam input

We basically follow the virtual camera settings from pyvirtualcam. If you do not choose to install virtual camera, it will visualize real-time output with cv2.imshow.

A. Linux (v4l2loopback)
# Install v4l2loopback for webcam relay
$ git clone https://github.com/umlaeute/v4l2loopback.git && cd v4l2loopback
$ make && sudo make install
$ sudo depmod -a

# Create virtual webcam
$ sudo modprobe v4l2loopback devices=1
B. Windows (OBS)

Install OBS virtual camera from install OBS.

C. macOS (OBS) [not stable]

Follow the steps below.

2. File explorer for GUI

A. Linux

You need to install zenity to open files and directories on Linux

sudo apt install zenity

Install transparent-background

Install from Github

pip install --extra-index-url https://download.pytorch.org/whl/cu118 git+https://github.com/plemeri/transparent-background.git

Install from local

git clone https://github.com/plemeri/transparent-background.git
cd transparent-backbround
pip install --extra-index-url https://download.pytorch.org/whl/cu118 .

Install CPU version only

# On Windows
pip install transparent-background
pip uninstall torch torchvision torchaudio
pip install torch torchvision torchaudio

# On Linux
pip install transparent-background
pip uninstall torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

[New] Configuration

transparent-background now supports external configuration rather than hard coded assets (e.g., checkpoint download url).

fast: url: "https://drive.google.com/file/d/1iRX-0MVbUjvAVns5MtVdng6CQlGOIo3m/view?usp=share_link" md5: NULL # change md5 to NULL if you want to suppress md5 checksum process ckpt_name: "ckpt_fast.pth" http_proxy: "http://192.168.1.80:8080" base_size: [384, 384]


* If you are an advanced user, maybe you can try making `custom` mode by training custom model from [InSPyReNet](https://github.com/plemeri/InSPyReNet.git).

```yaml
custom:
  url: [your google drive url]
  md5: NULL
  ckpt_name: "ckpt_custom.pth"
  http_proxy: "http://192.168.1.81:8080"
  base_size: [768, 768]
$ transparent-background --source test.png --mode custom

:pencil2: Usage

:+1: GUI

You can use gui with following command after installation.

transparent-background-gui

Screenshot 2024-10-05 194115

:computer: Command Line

# for apple silicon mps backend, use "PYTORCH_ENABLE_MPS_FALLBACK=1" before the command (requires torch >= 1.13)
$ transparent-background --source [SOURCE]
$ transparent-background --source [SOURCE] --dest [DEST] --threshold [THRESHOLD] --type [TYPE] --ckpt [CKPT] --mode [MODE] --resize [RESIZE] --format [FORMAT] (--reverse) (--jit)

:crystal_ball: Python API

from PIL import Image from transparent_background import Remover

Load model

remover = Remover() # default setting remover = Remover(mode='fast', jit=True, device='cuda:0', ckpt='~/latest.pth') # custom setting remover = Remover(mode='base-nightly') # nightly release checkpoint remover = Remover(resize='dynamic') # use dynamic resizing instead of static resizing

Usage for image

img = Image.open('samples/aeroplane.jpg').convert('RGB') # read image

out = remover.process(img) # default setting - transparent background out = remover.process(img, type='rgba') # same as above out = remover.process(img, type='map') # object map only out = remover.process(img, type='green') # image matting - green screen out = remover.process(img, type='white') # change backround with white color out = remover.process(img, type=[255, 0, 0]) # change background with color code [255, 0, 0] out = remover.process(img, type='blur') # blur background out = remover.process(img, type='overlay') # overlay object map onto the image out = remover.process(img, type='samples/background.jpg') # use another image as a background

out = remover.process(img, threshold=0.5) # use threhold parameter for hard prediction. out = remover.process(img, reverse=True) # reverse output. background -> foreground

out.save('output.png') # save result out.save('output.jpg') # save as jpg

Usage for video

cap = cv2.VideoCapture('samples/b5.mp4') # video reader for input fps = cap.get(cv2.CAP_PROP_FPS)

writer = None

while cap.isOpened(): ret, frame = cap.read() # read video

if ret is False:
    break

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
img = Image.fromarray(frame).convert('RGB')

if writer is None:
    writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size) # video writer for output

out = remover.process(img, type='map') # same as image, except for 'rgba' which is not for video.
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))

cap.release() writer.release()


## :tv: Tutorial

[rsreetech](https://github.com/rsreetech) shared a tutorial using colab. [[Youtube](https://www.youtube.com/watch?v=jKuQEnKmv4A)]

## :outbox_tray: Uninstall

pip uninstall transparent-background



## :page_facing_up: Licence

See [LICENCE](https://github.com/plemeri/transparent-background/blob/main/LICENSE) for more details.

### Acknowledgement

This work was supported by Institute of Information & communications Technology Planning & Evaluation (IITP) grant funded by the Korea government (MSIT) 
(No.2017-0-00897, Development of Object Detection and Recognition for Intelligent Vehicles) and 
(No.B0101-15-0266, Development of High Performance Visual BigData Discovery Platform for Large-Scale Realtime Data Analysis)