{"slug": "image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why", "title": "Image AI in the browser or on a CPU server: what we run where, and why", "summary": "A developer behind the free image-tools site OMyImage described how the service splits AI image processing between the browser and a CPU-only server with four shared virtual CPUs. A 28 MB ONNX inpainting model (MI-GAN) runs in-browser on a single WebAssembly thread, while background removal via rembg and U²-Net runs server-side at roughly 6-7 seconds per image, with routing decided by decoded pixel area rather than file size because Safari silently returns blank canvases above its area limit.", "body_md": "[oMyImage](https://omyimage.com/) is a free set of image tools: compress, resize, convert, and a handful of AI ones like removing a background or an unwanted object. We wanted as much as possible to run in the browser, so photos stay on the user's device. Some of it does. Some of it can't, and the reasons turned out to be more interesting than \"the model is too big\".\n\nOur server has no GPU. It's four virtual CPUs shared with the PDF tools on our sister site. That one fact shaped almost every decision below.\n\n**Key takeaways**\n\n- A 28 MB ONNX model runs fine in the browser on the CPU. You don't need WebGPU for everything.\n- Route by decoded pixels, not file size. Safari silently returns blank canvases above its area limit.\n- On a CPU-only server, check the input size\n*before* starting the job, or one photo can take every core.- Licences can decide architecture. HEIC decoding runs on our server for licensing reasons, not speed.\n\nTo remove an object or watermark you paint over it, and [MI-GAN](https://huggingface.co/andraniksargsyan/migan) fills the hole. It's an MIT-licensed inpainting model, and the ONNX file is 28,079,181 bytes.\n\nOur host, Cloudflare Pages, rejects any single file over 25 MiB. So the model ships as two parts. The browser fetches both in parallel, streams them into one preallocated buffer (which also drives the progress bar), then checks the SHA-256 before trusting it:\n\n``` js\nexport const MODEL = {\n  parts: [\n    { url: \"/models/migan_pipeline_v2.6f1f3530.part1.bin\", bytes: 14_000_000 },\n    { url: \"/models/migan_pipeline_v2.6f1f3530.part2.bin\", bytes: 14_079_181 },\n  ],\n  bytes: 28_079_181,\n  sha256: \"6f1f3530a1a2324b19752018ce756088b07973cda8d7d890034ace5c8a48c40b\",\n} as const;\n```\n\nThe first 8 characters of the hash are in the file name, so the files can be cached for a year with `immutable`. There's no IndexedDB or Cache API code at all. The plain HTTP cache does the job.\n\n[ONNX Runtime Web](https://onnxruntime.ai/docs/tutorials/web/) runs here on WebAssembly, on the CPU. Its WebGPU builds are 27 MB each, again over the per-file limit, and our notes put MI-GAN at about a second on a laptop CPU anyway.\n\nIt also runs on one thread:\n\n```\n// One thread: several need cross-origin isolation (COOP/COEP headers),\n// which would break the Google Drive picker and sign-in popups. MI-GAN is\n// small enough not to need them. The proxy runs inference in ORT's own\n// worker so the page keeps responding.\nort.env.wasm.numThreads = 1;\nort.env.wasm.proxy = true;\n```\n\nMulti-threaded WebAssembly needs `SharedArrayBuffer`, which needs cross-origin isolation headers, which break third-party popups. For a model this small, one thread in a worker was the better trade.\n\nOne small bug shaped the pipeline. MI-GAN works on a 512 px crop around the hole. If you mark two small spots in opposite corners, one crop around both is nearly the whole image, shrunk to 512 px, and the fill comes out blurry. So separate marks are found on a 16 px grid and filled one region at a time, each with some surrounding context.\n\nFace blurring uses MediaPipe's BlazeFace model. The model is only 0.23 MB, but the WebAssembly runtime is 11.8 MB, and we host both ourselves. MediaPipe's examples load from a CDN, but our page says nothing leaves the device, and that should be literally true.\n\nOur first rule copied the PDF site's: files up to 15 MB stay in the browser. For images, that was wrong in both directions.\n\nThe router now decides on decoded pixel area, and probes the canvas instead of trusting it. It paints the far corner and reads it back. Over the limit, Safari hands back zeros. We tested it with a 1,000-megapixel canvas that \"worked\" and returned `[0,0]`. If the probe fails, the tool retries on the server.\n\nBackground removal runs [rembg](https://github.com/danielgatis/rembg) with the U²-Net model on the server. It takes about 6 to 7 seconds per image. We assumed bigger images were slower. They aren't: the time is almost all spent loading the 168 MB model into a fresh process on every request. Image size barely matters, so there's no size cap on this route. (Keeping the model loaded in a long-running worker is the obvious next step.)\n\nUpscaling uses [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN) through its ncnn/Vulkan build. With no GPU, Vulkan falls back to Mesa's software renderer on the same four CPUs.\n\nOn a 512×384 test image, the standard `x4plus` model took 3 minutes 29 seconds and 12 minutes of CPU time. The much smaller `realesr-animevideov3` model took 8 seconds. A normal 12-megapixel phone photo with `x4plus` would have pinned every core for tens of minutes and hit nginx's 300-second timeout while still running.\n\nSo production uses the smaller model (yes, an anime-video model on photos; it does a decent job, and the trade-off is written down), and the size is checked **before** the process starts:\n\n``` js\nconst MAX_UPSCALE_PIXELS = Number(process.env.IMAGE_UPSCALE_MAX_PIXELS ?? 2_000_000);\n```\n\nAnything larger gets a clear `413` instead of a timeout. Two smaller lessons from the same tool:\n\niPhone photos are HEIC, and people want JPG. There are several JavaScript HEIC decoders, but they're all the same Emscripten build of libheif, which is LGPL-3.0. Sending it to the browser counts as distributing it, which brings source and relinking obligations. Running it on our server is just running a service.\n\nWe couldn't use our usual image library either. sharp's prebuilt binaries include libheif with only the AVIF decoder, not HEVC. So HEIC goes to ImageMagick built with libheif, on the server. It's the one tool where the user-facing text says plainly that the file is uploaded.\n\nAll of this shares four CPUs, so the heavy image routes go through a small queue:\n\n`503` with `Retry-After: 5` instead of accepting more.\nThe queue is bounded on purpose. An unbounded queue holds every waiting upload in memory, which is a reliable way to hit the process manager's memory limit and restart the API for everyone.\n\nResults are deleted after an hour. One easy-to-miss detail: scratch folders must be named `omyimage-<label>-…` with that trailing hyphen, because the cleanup only deletes folders matching a strict pattern. Without it, files from crashed jobs would pile up in `/tmp` forever.\n\nYou can try the tools on [oMyImage](https://omyimage.com/): [remove background](https://omyimage.com/remove-background) runs on the server, and [compress image](https://omyimage.com/compress-image) runs entirely in your browser. Questions welcome in the comments.", "url": "https://wpnews.pro/news/image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why", "canonical_source": "https://dev.to/mishrakalpesh9555/image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why-53b8", "published_at": "2026-11-27 08:06:32+00:00", "updated_at": "2026-11-27 08:19:29.540179+00:00", "lang": "en", "topics": ["ai-tools", "machine-learning", "ai-infrastructure", "computer-vision", "developer-tools"], "entities": ["OMyImage", "MI-GAN", "ONNX Runtime Web", "MediaPipe", "BlazeFace", "rembg", "U²-Net", "Cloudflare Pages"], "also_reported_by": [], "takeaway": "Route image jobs by decoded pixel area rather than file size and probe the canvas by painting and reading back a corner, since Safari returns zeros above its area limit; a 28 MB ONNX model runs fine in-browser on one WebAssembly thread.", "recommendation": {"advice": "Probe canvas support by painting and reading back a corner before routing work client-side, and run small ONNX models single-threaded in a worker to avoid cross-origin isolation headers.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why", "markdown": "https://wpnews.pro/news/image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why.md", "text": "https://wpnews.pro/news/image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why.txt", "jsonld": "https://wpnews.pro/news/image-ai-in-the-browser-or-on-a-cpu-server-what-we-run-where-and-why.jsonld"}}