Loading portfolio
Skip to content
All posts

Building BGaway, an Offline Background Remover

Kuldeep Rajput
Desktop Engineering
3 min read
BGaway shows the original photo on the left and the cutout on a transparency grid on the right.

Why I built BGaway

The part of background removal I care about is what happens after the first click. The preview might look fine until you zoom in and see that the model took a bit of someone’s sleeve with it. I wanted to fix that in the same window where I removed the background.

  • Keep the photo on the device from import to export.
  • Leave the original photo visible beside the cutout.
  • Make the same workflow work for a single portrait or a whole folder.

How the app is put together

The React editor handles what you see and click. Through Tauri, it calls Rust commands that load the photo, run the model, and write the result.

  1. The Fast profile runs U²-Net-P on a 320 × 320 input. IS-Net, BiRefNet Lite, and full BiRefNet FP16 use 1024 × 1024. The models also have different normalization settings.
  2. ONNX Runtime opens a session the first time a model is needed and reuses it for later photos. If a run fails on the GPU, BGaway retries on the CPU.
  3. Batch processing uses the same extraction code as a single photo. The worker sends an update as each file starts and finishes.

From a small mask to a full-size image

In run_removal_pipeline, the model sees a resized copy of the photo. Its output is a mask. I resize the mask to the original dimensions and use it to set the transparency of the original pixels. That’s how a 320 × 320 model can still produce a cutout the size of the source.

rust
let (width, height) = (image.width(), image.height());
let input_size = spec.input_size;

let model_input = compositor::prepare_model_input(&image, input_size);
let raw_mask = onnx_engine::run_inference(spec, &model_input)?;
let full_res_mask = compositor::resize_mask_to(
    &raw_mask, input_size, width, height,
);

let composited = compositor::apply_alpha_mask(
    &image, &full_res_mask, width, height,
);

Fixing the parts the model missed

The split view is where I can tell whether a rough edge came from the cutout or was already in the photo. The checkerboard makes tiny gaps easier to see.

  • Erase makes part of the cutout transparent.
  • Restore brings a hidden part back. The original colors are still there, so a brush stroke only has to change the alpha channel.

Working through a folder

  • Pick a model for the batch and switch it for any photo that needs a closer look.
  • Keep transparency in PNG or WebP, or add a background before exporting. JPEG uses a background fill.
  • Save the images individually or export the batch as a ZIP.

What I’d carry into the next version

  • Keep the original pixels around. They’re useful for both the full-size export and small corrections.
  • Match the preprocessing to each model. Input size alone doesn’t tell you how the input should be normalized.
  • Give the editor a way to fix the result in place. For one missing edge, opening the brush is faster than starting over.
View source on GitHub

Related posts

“I have learned from everyone who was better than me. The real test came when I had to find my own forma of doing things.”

— Kuldeep Rajput