diff options
author | bobzel <zzzman@gmail.com> | 2025-08-18 11:55:29 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2025-08-18 11:55:29 -0400 |
commit | 8f1d7c28739270f40d97258cb4e6fbe5c20a5865 (patch) | |
tree | 03e3bf8130810d3ce21ea952a2ed0ad3bbad4808 /src/workers | |
parent | 1577b92b3d66a26c0df45db9122b47be809ebe83 (diff) |
added image foreground mask function. added ui buttons for removing background, masking foreground.HEADmaster
Diffstat (limited to 'src/workers')
-rw-r--r-- | src/workers/image.worker.ts | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/src/workers/image.worker.ts b/src/workers/image.worker.ts index 48b4e8585..eb878e336 100644 --- a/src/workers/image.worker.ts +++ b/src/workers/image.worker.ts @@ -1,14 +1,45 @@ -import { removeBackground } from '@imgly/background-removal'; +import { removeBackground, segmentForeground, preload, Config } from '@imgly/background-removal'; + +// Preload the model when the worker starts +let modelLoaded = false; +preload() + .then(() => { + modelLoaded = true; + console.log('Background removal model preloaded successfully.'); + }) + .catch(error => { + console.error('Failed to preload background removal model:', error); + }); self.onmessage = async (event: MessageEvent) => { - const { imagePath, options, nativeWidth, nativeHeight, docId } = event.data; + const { imagePath, config, docId } = event.data; + const configWithDefaults: Config = { + ...config, + debug: true, + // You can set default config values here if needed + onProgress: (progress: number) => { + console.log('Progress: ' + progress); + // Send progress updates to the main thread + self.postMessage({ type: 'progress', docId, progress }); + }, + }; try { - // Perform the background removal - const result = await removeBackground(imagePath); + // Ensure the model is preloaded before processing + if (!modelLoaded) { + await preload(configWithDefaults); + modelLoaded = true; + } + // Simulate progress updates (if the library doesn't provide them natively) + self.postMessage({ type: 'progress', docId, progress: 0 }); + + const resultProm = + config.output.type === 'mask' + ? segmentForeground(imagePath, configWithDefaults) // + : removeBackground(imagePath, configWithDefaults); // Send the result back to the main thread - self.postMessage({ success: true, result, options, nativeWidth, nativeHeight, docId }); + self.postMessage({ success: true, result: await resultProm, docId }); } catch (error) { // Send the error back to the main thread // eslint-disable-next-line @typescript-eslint/no-explicit-any |