web/removebg: convert to a proper web worker
Some checks are pending
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
Run tests / check lockfile correctness (push) Waiting to run
Run tests / web sanity check (push) Waiting to run
Run tests / api sanity check (push) Waiting to run

no more hanging ui :3
This commit is contained in:
wukko 2025-01-15 17:22:34 +06:00
parent 2f2d39dc4c
commit 28d8927c08
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
3 changed files with 66 additions and 42 deletions

View file

@ -11,23 +11,8 @@ const models = {
} }
} }
export const removeImageBackground = async (file: File) => { export const maskImage = async (source: Blob, mask: RawImage) => {
const image = await RawImage.fromBlob(new Blob([file])); const image = await RawImage.fromBlob(source);
const model_type = "light";
const model = await AutoModel.from_pretrained(models[model_type].id, {
device: "wasm",
dtype: "fp32",
});
const processor = await AutoProcessor.from_pretrained(models[model_type].id, {});
if (model && processor) {
const { pixel_values } = await processor(image);
const { output } = await model({ [models[model_type].input]: pixel_values });
const mask = await RawImage.fromTensor(output[0].mul(255).to('uint8')).resize(image.width, image.height);
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
canvas.width = image.width; canvas.width = image.width;
@ -45,5 +30,30 @@ export const removeImageBackground = async (file: File) => {
ctx.putImageData(pixelData, 0, 0); ctx.putImageData(pixelData, 0, 0);
return canvas; return canvas;
}
export const removeImageBackground = async (file: File) => {
const originalImageBlob = new Blob([file]);
const image = await RawImage.fromBlob(originalImageBlob);
const model_type = "light";
const model = await AutoModel.from_pretrained(models[model_type].id, {
device: "wasm",
dtype: "fp32",
});
const processor = await AutoProcessor.from_pretrained(models[model_type].id, {});
if (model && processor) {
const { pixel_values } = await processor(image);
const { output } = await model({ [models[model_type].input]: pixel_values });
const mask = await RawImage.fromTensor(output[0].mul(255).to('uint8')).resize(image.width, image.height);
self.postMessage({ source: originalImageBlob, mask });
} }
} }
self.onmessage = async (event: MessageEvent) => {
await removeImageBackground(event.data.file);
self.close();
}

View file

@ -1,36 +1,49 @@
<script lang="ts"> <script lang="ts">
import settings from "$lib/state/settings"; import settings from "$lib/state/settings";
import RemoveBgWorker from '$lib/workers/removebg?worker';
import { onMount } from "svelte"; import { onMount } from "svelte";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { downloadFile } from "$lib/download"; import { downloadFile } from "$lib/download";
import { removeImageBackground } from "$lib/workers/removebg"; import { maskImage } from "$lib/workers/removebg";
import DropReceiver from "$components/misc/DropReceiver.svelte"; import DropReceiver from "$components/misc/DropReceiver.svelte";
import FileReceiver from "$components/misc/FileReceiver.svelte"; import FileReceiver from "$components/misc/FileReceiver.svelte";
import Skeleton from "$components/misc/Skeleton.svelte"; import Skeleton from "$components/misc/Skeleton.svelte";
let imageContainer: HTMLElement;
let draggedOver = false; let draggedOver = false;
let file: File | undefined; let file: File | undefined;
let thinking = false;
let done = false;
let result: HTMLCanvasElement; let result: HTMLCanvasElement;
let imageContainer: HTMLElement;
let state: "empty" | "busy" | "done" = "empty";
const worker = new RemoveBgWorker();
const processImage = async () => { const processImage = async () => {
if (file) { if (!file) return;
thinking = true;
const removedBackground = await removeImageBackground(file); state = "busy";
if (removedBackground) {
thinking = false; worker.postMessage({ file });
done = true; worker.onmessage = async (event) => {
result = removedBackground; const maskedCanvas = await maskImage(event.data.source, event.data.mask);
imageContainer.append(removedBackground);
} if (!maskedCanvas) {
state = "empty";
return;
};
state = "done";
result = maskedCanvas;
imageContainer.append(maskedCanvas);
};
worker.onerror = (e) => {
console.error("bg removal worker exploded:", e)
worker.terminate();
} }
}; };
@ -53,7 +66,7 @@
</script> </script>
<DropReceiver bind:file bind:draggedOver id="cutout-container"> <DropReceiver bind:file bind:draggedOver id="cutout-container">
{#if !thinking && !done} {#if state === "empty"}
<FileReceiver <FileReceiver
bind:draggedOver bind:draggedOver
bind:file bind:file
@ -68,27 +81,27 @@
{/if} {/if}
{/if} {/if}
{#if thinking} {#if state === "busy"}
<div>thinking very hard rn...</div> <div>thinking very hard rn...</div>
{/if} {/if}
{#if done} {#if state === "done"}
<div>thought a lot, here's what i got:</div> <div>thought a lot, here's what i got:</div>
{/if} {/if}
{#if thinking || done} {#if ["busy", "done"].includes(state)}
<div id="image-preview" bind:this={imageContainer}> <div id="image-preview" bind:this={imageContainer}>
{#if !done} {#if state === "busy"}
<Skeleton width="100%" height="100%" class="big" /> <Skeleton width="100%" height="100%" class="big" />
{/if} {/if}
</div> </div>
{/if} {/if}
{#if done} {#if state === "done"}
<div id="finished-actions"> <div id="finished-actions">
<button <button
on:click={() => { on:click={() => {
done = false; state = "empty"
file = undefined; file = undefined;
}} }}
> >

View file

@ -0,0 +1 @@
export const ssr = false;