Last week our free background remover had a problem: on busy photos (trees, walls, clutter) it left bits of background around people's heads. Users had to clean them up by hand, and that's not what "one click" means. Here's what we changed, with numbers, in case you're building something similar in the browser.

The setup: everything runs client-side. No upload, no server GPU. We use @imgly/background-removal (an ISNet model on top of onnxruntime-web), loaded from a CDN only when someone opens the tool.

1. The quantized model was the real problem

We were using isnet_quint8 – the smallest variant – because it downloads fast. It looked fine on clean product shots, but on cluttered portraits the 8-bit quantization showed: leaves and wall texture got classified as "foreground".

Same photo processed by isnet_quint8, isnet_fp16 and isnet_fp16 plus clean-up

Switching to isnet_fp16 fixed most of it. The download is roughly twice as big (we now tell users "about 80 MB, first time only" – the browser caches it), and inference time on CPU was about the same in our tests.

const opts = { model: 'isnet_fp16', device: hasWebGPU ? 'gpu' : 'cpu', output: { format: 'image/png', quality: 1 }, }; let out; try { out = await removeBackground(blob, opts); } catch (e) { // some GPUs fail at runtime if (opts.device !== 'gpu') throw e; opts.device = 'cpu'; out = await removeBackground(blob, opts); } 

hasWebGPU is just !!(navigator.gpu && await navigator.gpu.requestAdapter()), cached. On machines with a usable adapter it's dramatically faster; on our 4-core test laptop there was no adapter, so CPU numbers below are the pessimistic case.

2. A cheap mask clean-up pass (≈0.15 s)

Even a good model leaves the occasional speck or pin-hole. We added a post-process on the alpha channel:

  1. Label solid foreground regions (alpha ≥ 128) with a flood fill and keep only the big ones (≥ 4% of the largest region, or ≥ 0.15% of the image). Two people in a photo both survive; a floating leaf doesn't.
  2. City-block distance transform (two passes) from the kept subject. Semi-transparent pixels far from the subject are haze – set them to 0. Close to the subject they're hair/fur – keep them, fading out over a few pixels.
  3. Fill small holes: background regions that don't touch the image border and are smaller than 0.4% of the image become opaque.

All of it is plain typed-array JS on the ImageData – about 130–170 ms for a 1-megapixel image, which is nothing next to inference.

3. Multi-threaded WASM via COOP/COEP – only on the AI pages

ONNX Runtime Web only uses multiple threads when SharedArrayBuffer is available, which requires the page to be cross-origin isolated. Our site had crossOriginIsolated === false, so the model ran on one core.

We didn't want to isolate the whole site (it can break third-party embeds and ads), so we send the headers only for the two AI photo pages:

if (in_array($path, ['/remove-background/', '/passport-photo/'], true)) { header('Cross-Origin-Opener-Policy: same-origin'); header('Cross-Origin-Embedder-Policy: credentialless'); } 

credentialless instead of require-corp means cross-origin no-cors requests (analytics, fonts, thumbnails) still load – just without cookies – so nothing else on the page had to change.

Result on a 4-core laptop with no WebGPU: a warm run went from ~24 s to ~16 s. Not magic, but noticeable – and on GPU machines it's a few seconds.

4. One more thing users asked for: "make it HD"

We added an Auto enhance (HD) toggle that runs in ~0.1 s: per-channel levels from the 0.5/99.5 percentiles (gentle white balance), a soft S-curve, +7% saturation, and an unsharp mask on luminance with a small noise threshold plus a wide-radius "clarity" pass. It doesn't invent detail like an AI upscaler, but it makes typical phone photos look noticeably crisper.

Original vs Auto enhance

5. Let people talk to the editor

The tool has a small rule-based assistant next to the canvas: "make it HD with a blue background", "remove small spots", "undo". Two bugs we fixed this week are a good lesson in intent parsing:

  • "keep only the shoe" matched our restore intent because of the word "keep". Now keep only / just the maps to remove everything else.
  • Removing a neutral colour ("remove the grey stones") by colour matching also ate the white parts of the product. For white/grey/black/brown we now switch to Tap to remove instead of guessing.

Any background colour

Try it

Both are free, no sign-up, and photos never leave the browser. Happy to answer questions about the pipeline in the comments.

Disclosure: I work on FreeAIEdits. The person in the examples is AI-generated.