transparent-background

repository·main·Indexed 22 days ago

https://github.com/plemeri/transparent-background

A tool powered by InSPyReNet for removing backgrounds from images, videos, and webcam feeds to make them transparent. It provides a CLI, a GUI, and a Python API via the Remover class. Features include support for GPU acceleration, custom model checkpoints via YAML configuration, and various output types such as RGBA, green screen, white background, or custom colors.

Tokens
2.2K
Snippets
6
Records
9
Agent score
29%

What's inside transparent-background

  1. Install transparent-background via pip

    main

    Install the core package using pip. If you want to use GPU acceleration (especially on Windows), you should specify the appropriate PyTorch extra-index-url for your CUDA version (e.g., cu118 or cu128).

    To include optional features, use extra dependency brackets:

    • [webcam]: For webcam input support (note: webcam support is currently only stable on Linux).
    • [gui]: For GUI mode support (requires flet).

    To install the CPU-only version on Linux, install the package and then reinstall torch, torchvision, and torchaudio using the PyTorch CPU index.

  2. Configure webcam relay on Linux using v4l2loopback

    main

    To use a virtual webcam for relaying output on Linux, you must install and configure v4l2loopback:

    1. Clone and install v4l2loopback.
    2. Load the module and create a virtual device.

    Note: For Windows and macOS, use OBS Virtual Camera instead.

    # 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
  3. Configure transparent-background via YAML

    main

    You can customize asset downloads (like checkpoint URLs and MD5 checksums) using an external configuration file. By default, this file is located at ~/.transparent-background/config.yaml. You can override this location by setting the TRANSPARENT_BACKGROUND_FILE_PATH environment variable.

    The configuration supports different modes (base, fast, base-nightly, or custom) and allows you to specify:

    • url: A Google Drive download link for the checkpoint.
    • md5: The file's MD5 checksum (set to NULL to skip verification).
    • ckpt_name: The filename for the checkpoint.
    • http_proxy: A proxy address if your connection requires one.
    • base_size: The target resolution (e.g., [1024, 1024]).
    base:
      url: "https://drive.google.com/file/d/13oBl5MTVcWER3YU4fSxW3ATlVfueFQPY/view?usp=share_link"
      md5: "d692e3dd5fa1b9658949d452bebf1cda"
      ckpt_name: "ckpt_base.pth"
      http_proxy: NULL
      base_size: [1024, 1024]
    
    fast:
      url: "https://drive.google.com/file/d/1iRX-0MVbUjvAVns5MtVdng6CQlGOIo3m/view?usp=share_link"
      md5: NULL
      ckpt_name: "ckpt_fast.pth"
      http_proxy: "http://192.168.1.80:8080"
      base_size: [384, 384]
    
    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]
  4. Use the Remover Python API

    main

    The Remover class is the primary entry point for programmatic access. You can initialize it with various settings and process images or video frames.

    Initialization: remover = Remover(mode='fast', jit=True, device='cuda:0', ckpt='~/latest.pth')

    Processing Images: out = remover.process(img, type='rgba', threshold=0.5)

    Processing Video: When processing video frames, avoid using type='rgba'. Use types like map, green, blur, etc.

    Supported type values in API:

    • 'rgba'
    • 'map'
    • 'green'
    • 'white'
    • [R, G, B] (e.g., [255, 0, 0])
    • 'blur'
    • 'overlay'
    • 'path/to/background.jpg'
    import cv2
    import numpy as np
    from PIL import Image
    from transparent_background import Remover
    
    # 1. Initialize the remover
    remover = Remover(mode='fast', jit=True, device='cuda:0')
    
    # 2. Process an image
    img = Image.open('samples/aeroplane.jpg').convert('RGB')
    out = remover.process(img, type='rgba')
    out.save('output.png')
    
    # 3. Process a video
    cap = cv2.VideoCapture('samples/b5.mp4')
    fps = cap.get(cv2.CAP_PROP_FPS)
    writer = None
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret: break
        
        # Convert BGR to RGB for PIL
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb).convert('RGB')
    
        if writer is None:
            writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
    
        # Process frame (use 'map' for video instead of 'rgba')
        out = remover.process(img, type='map')
        
        # Convert back to BGR for OpenCV writer
        writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
    
    cap.release()
    if writer:
        writer.release()
  5. Reference CLI flags for transparent-background

    main

    The following flags are available for the transparent-background CLI:

    • --source [SOURCE]: Input data. Supports single image (image.png), folder of images, single video (video.mp4), folder of videos, or an integer for webcam address (e.g., 0).
    • --dest [DEST]: (Optional) Destination folder. Defaults to the current directory.
    • --threshold [THRESHOLD]: (Optional) Value from 0.0 to 1.0 for hard prediction. Omit for soft prediction.
    • --type [TYPE]: (Optional) Output type. Defaults to rgba.
      • rgba: Generates RGBA output using saliency score as alpha map. Uses pymatting for foreground extraction if no threshold is set. (Does not work for video/webcam).
      • map: Outputs saliency map only.
      • green: Changes background to green screen.
      • white: Changes background to white.
      • '[R, G, B]': Changes background to a specific color code (e.g., '[255, 0, 0]'). Use single quotes.
      • blur: Blurs the background.
      • overlay: Covers the salient object with translucent green and highlights edges.
      • [FILE]: Use another image file as the background.
    • --ckpt [CKPT]: (Optional) Path to a specific checkpoint file.
    • --mode [MODE]: (Optional) Choose from base, base-nightly, or fast.
    • --resize [RESIZE]: (Optional) Choose static (default) or dynamic (sharper edges but potentially unstable).
    • --format [FORMAT]: (Optional) Output format. Defaults to input format.
    • --reverse: (Optional) Removes the foreground instead of the background.
    • --jit: (Optional) Enables Torchscript mode to reduce inference time and GPU memory usage (may delay initialization).
  6. Use the transparent-background CLI

    main

    The command line interface allows you to process images, folders of images, videos, folders of videos, or webcams.

    Note for Apple Silicon (MPS): Use PYTORCH_ENABLE_MPS_FALLBACK=1 before the command (requires torch >= 1.13).

    Basic usage: transparent-background --source [SOURCE]

    Full command structure: transparent-background --source [SOURCE] --dest [DEST] --threshold [THRESHOLD] --type [TYPE] --ckpt [CKPT] --mode [MODE] --resize [RESIZE] --format [FORMAT] (--reverse) (--jit)