# How I made a background remover that runs entirely in the browser

> Source: <https://dev.to/gentil_m/how-i-made-a-background-remover-that-runs-entirely-in-the-browser-105c>
> Published: 2026-11-19 07:35:27+00:00

**Recommendation:** Enable the ONNX WASM proxy for browser-side BiRefNet inference to move heavy work off the main thread, and budget for a ~473 MB first-load model download with fp32 fallback.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

Most “free background remover” sites work the same way: you upload a photo, a server runs a segmentation model, and a PNG comes back. That is convenient, and it is also a privacy problem. A CV portrait, a product shot, or a scan of a document should not have to sit on a stranger’s GPU for a simple cut-out.

I wanted a tool that does the same job in the browser tab. The result is [Free Background Remover](https://free-background-remover.web-free.site/). It is a free, ad-supported site. Your photo is not sent to our servers for processing. This post describes how the page is put together, what it borrows, and where the trade-offs are.

I did not train a new model, and I did not write a new inference engine. The site is a fork of [addyosmani/bg-remove](https://github.com/addyosmani/bg-remove) (MIT), which itself follows the Hugging Face Transformers.js background-removal demo.

The useful parts of that codebase were already in the right place:

`@huggingface/transformers`
My work was to ship it as a small public site: branding, a privacy page that states the model download clearly, a few how-to guides, and ads to keep the tool free. The application code is MIT. The repository is [web-free-site/free-background-remover](https://github.com/web-free-site/free-background-remover).

License matters as much as quality if the site shows ads. I did not use BRIA RMBG models, which are limited to non-commercial use, and I did not pull in an AGPL background-removal library.

The default model is [`onnx-community/BiRefNet_512x512-ONNX`](https://huggingface.co/onnx-community/BiRefNet_512x512-ONNX) (MIT). It is an ONNX export of [ZhengPeng7/BiRefNet](https://github.com/ZhengPeng7/BiRefNet). On a first visit the loader tries **fp16** weights, which are about **473 MB**. If that load fails, it falls back to **fp32**.

If the browser exposes WebGPU, and the visitor is not on iOS, there is an optional second model: [`Xenova/modnet`](https://huggingface.co/Xenova/modnet) (Apache-2.0), run with `device: "webgpu"`. If that initialization fails, the page falls back to BiRefNet in WebAssembly. iOS is steered to the WASM path on purpose.

Inference uses `onnxruntime-web` under Transformers.js. The photo itself never goes to Hugging Face. What *does* go to Hugging Face, on first use, is the model: the browser fetches the weights directly, not through our application servers. Later visits can reuse the cached files. I set `env.allowLocalModels = false` because the weights are not bundled with the site; they are too large to ship as a Cloudflare Worker static asset.

That first download is the main trade-off. A 473 MB model is a long wait on a slow connection. I would rather say that up front than pretend the tool is instant and fully offline on a cold visit.

Once the model and processor are in memory, a chosen file is read locally (`RawImage.fromURL` on an object URL). The processor produces a tensor; for BiRefNet that input is 512×512. The model returns a mask. In the BiRefNet path the mask is passed through a sigmoid, scaled to 0–255, and cast to `uint8`. The mask is then resized to the original width and height.

A canvas draws the original image, the mask is copied into the alpha channel of the pixel data, and the result is exported as a PNG (`name-no-bg.png`). The original file is not uploaded, and the PNG is created in the tab.

Because the network sees a 512×512 input, a 4,000-pixel portrait does not get a 4,000-pixel “understanding” of the edge. The alpha channel is only an upscaled mask. Hair, glass, and thin straps are the usual weak spots. I would zoom in before using the file on a CV or a product listing. Automatic removal is a starting point, not a guarantee of a perfect cut-out.

Transformers.js talks to `onnxruntime-web`. For the BiRefNet WASM path I turn the ONNX WASM **proxy** on, so the heavy work can sit in a worker rather than on the main thread. For the optional MODNet WebGPU path the proxy is turned off, the page asks for a GPU adapter, and the model is constructed with `device: "webgpu"` and a small config that names the MODNet architecture. If `navigator.gpu` is missing, or `requestAdapter()` returns nothing, that path is skipped.

The load and the prediction are ordinary async functions. In simplified form, the default path looks like this:

```
state.model = await AutoModel.from_pretrained(DEFAULT_MODEL_ID, { dtype: "fp16" });
state.processor = await AutoProcessor.from_pretrained(DEFAULT_MODEL_ID);

const img = await RawImage.fromURL(URL.createObjectURL(image));
const { pixel_values } = await state.processor(img);
const { output_image } = await state.model({ input_image: pixel_values });
const maskTensor = output_image[0].sigmoid().mul(255).to("uint8");
```

The mask is then resized with `RawImage.fromTensor(maskTensor).resize(img.width, img.height)`, drawn into the canvas alpha channel, and saved with `canvas.toBlob(..., "image/png")`. There is also a `processImages()` helper that runs that function over a list of files, so a few portraits can be queued one after another in the same tab.

None of this requires a backend of our own for inference. The site is a static front end. The large pieces that must still come from the network are the JavaScript bundle, the ads, and — on first use — the model weights.

It does not store the photo on our servers. It does not require an account. It does not claim a user count I cannot defend. Standard host logs (IP address, user agent, URL) and third-party ad cookies can still exist — the privacy page says so. Advertising is how the tool stays free.

On a technical level it is also not a custom training setup and not a new architecture. The interesting part is packaging: a large ONNX model, a WASM or WebGPU runtime, and a canvas composite, all inside a static front end.

Open [https://free-background-remover.web-free.site/](https://free-background-remover.web-free.site/), wait for the model on the first visit, and drop in a photo. If you only need the idea, the upstream projects above are the right place to start.
