No description
Find a file
Matthew Deville 609afde727 wip
2026-01-22 23:36:35 +01:00
src wip 2026-01-22 23:36:35 +01:00
.envrc direnv 2026-01-22 01:09:39 +01:00
.gitignore direnv 2026-01-22 01:09:39 +01:00
Cargo.lock compiles so far 2026-01-22 00:25:11 +01:00
Cargo.toml compiles so far 2026-01-22 00:25:11 +01:00
flake.lock wip 2026-01-21 22:17:47 +01:00
flake.nix infer input size 2026-01-22 21:48:08 +01:00
README.md compiles so far 2026-01-22 00:25:11 +01:00
rust-toolchain.toml wip 2026-01-21 22:14:29 +01:00

Background Removal Tool

A Rust-based background removal tool using ONNX models (BiRefNet) with GPU support.

Features

  • Download images from URLs
  • Automatic ONNX model download and caching
  • Background removal using BiRefNet-general-lite model
  • Side-by-side comparison display (original vs processed)
  • Checkered background visualization for transparency
  • CLI with flexible options

Building

# Enter the development environment
nix develop

# Build the project
cargo build --release

Without Nix

If you don't use Nix, you'll need to ensure you have the following system dependencies:

  • Rust toolchain
  • OpenSSL development libraries (libssl-dev on Ubuntu/Debian)
  • pkg-config
  • X11 libraries (for window display)
  • Vulkan loader

Usage

Basic usage:

# Download and process an image
cargo run -- https://example.com/image.jpg

# Use custom model
cargo run -- https://example.com/image.jpg --model-path ./custom_model.onnx

# Offline mode (use only cached model)
cargo run -- https://example.com/image.jpg --offline

How It Works

  1. Download: Fetches the image from the provided URL
  2. Model Loading: Auto-downloads BiRefNet model (cached in ~/.cache/remove_background/models/)
  3. Preprocessing: Resizes image to 1024x1024, normalizes pixels, converts to CHW format
  4. Inference: Runs ONNX model to generate foreground mask
  5. Postprocessing: Applies mask to original image, creates RGBA output
  6. Display: Shows side-by-side comparison with checkered background

Project Structure

src/
├── main.rs           - CLI interface and orchestration
├── model.rs          - Model download and ONNX session management
├── preprocessing.rs  - Image preprocessing (resize, normalize, CHW conversion)
└── postprocessing.rs - Mask application and side-by-side display creation

Dependencies

  • ort: ONNX Runtime Rust bindings
  • ndarray: N-dimensional array operations
  • image: Image loading and manipulation
  • reqwest: HTTP client for downloads
  • show-image: Cross-platform window display
  • clap: Command-line argument parsing
  • sha2: Hash verification

Future Enhancements

  • CUDA execution provider support (GPU acceleration)
  • Batch processing support
  • Additional model options (U2-Net, ISNet, etc.)
  • Save output to file
  • Local file input support
  • Progress bars for downloads
  • WebAssembly support

Controls

  • ESC: Close the window and exit

Model Information

Default model: BiRefNet-general-lite

License

See LICENSE file for details.