@webex/ladon-ts 5.4.5 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/build/module/index.d.ts +3 -2
- package/build/module/index.js +35 -34
- package/build/module/index.js.map +1 -1
- package/build/module/lib/config-builder.d.ts +1 -1
- package/build/module/lib/config-builder.js +59 -58
- package/build/module/lib/config-builder.js.map +1 -1
- package/build/module/lib/config-types.d.ts +1 -1
- package/build/module/lib/config-types.js.map +1 -1
- package/build/module/lib/generator-types.d.ts +25 -0
- package/build/module/lib/generator.d.ts +2 -74
- package/build/module/lib/pipeline.js +16 -26
- package/build/module/lib/pipeline.js.map +1 -1
- package/build/module/lib/preload.js +4 -4
- package/build/module/lib/preload.js.map +1 -1
- package/build/module/lib/resource-loader.d.ts +22 -0
- package/build/module/lib/resource-loader.js +33 -0
- package/build/module/lib/resource-loader.js.map +1 -0
- package/build/module/lib/util.d.ts +1 -2
- package/build/module/lib/util.js +63 -82
- package/build/module/lib/util.js.map +1 -1
- package/build/module/lib/version.d.ts +1 -1
- package/build/module/lib/version.js +1 -1
- package/build/module/lib/version.js.map +1 -1
- package/build/module/lib/worker-generator.d.ts +15 -0
- package/build/module/lib/worker-generator.js +93 -0
- package/build/module/lib/worker-generator.js.map +1 -0
- package/build/module/lib/worker-manager.d.ts +41 -0
- package/build/module/lib/worker-manager.js +63 -0
- package/build/module/lib/worker-manager.js.map +1 -0
- package/build/worker/segmentation.js +791 -791
- package/build/worker/segmentation.js.map +1 -1
- package/package.json +1 -4
- package/build/module/lib/generator.js +0 -224
- package/build/module/lib/generator.js.map +0 -1
- package/build/module/lib/shared-resource-manager.d.ts +0 -93
- package/build/module/lib/shared-resource-manager.js +0 -166
- package/build/module/lib/shared-resource-manager.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"segmentation.js","sources":["../src/shim.ts","../src/console-overrides.ts","../../build/module/lib/util.js","../../build/module/lib/shared-resource-manager.js","../../build/module/lib/generator.js","../src/segmentation.ts"],"sourcesContent":["/**\n * This file is used to provide a shim for the document object in a web worker.\n * The document object is not available in a web worker, so we need to provide a shim\n * to prevent errors when running the code in a web worker.\n * \n * See: https://github.com/vitejs/vite/issues/9879#issuecomment-1372112651\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\n(self.document as any) = {\n baseURI: location.origin\n};","/**\n * Override console methods to send logs to the main thread for display in the UI.\n */\n\nconst originalConsoleLog = console.log;\nconst originalConsoleWarn = console.warn;\nconst originalConsoleError = console.error;\n\nconsole.log = (...args) => {\n postMessage({ type: 'log', message: args.join(' '), severity: 'log' });\n originalConsoleLog(...args);\n};\n\nconsole.warn = (...args) => {\n postMessage({ type: 'log', message: args.join(' '), severity: 'warn' });\n originalConsoleWarn(...args);\n};\n\nconsole.error = (...args) => {\n postMessage({ type: 'log', message: args.join(' '), severity: 'error' });\n originalConsoleError(...args);\n};","var C = /* @__PURE__ */ ((o) => (o.Nearest = \"nearest\", o))(C || {});\nconst E = (o, r, s, n, t) => {\n const a = new Uint8ClampedArray(n * t * 4), m = r / n, M = s / t, i = r * 4;\n let c = 0;\n for (let e = 0; e < t; e++) {\n const l = Math.floor(e * M) * i;\n for (let f = 0; f < n; f++) {\n const u = l + Math.floor(f * m) * 4;\n a[c++] = o[u], a[c++] = o[u + 1], a[c++] = o[u + 2], a[c++] = o[u + 3];\n }\n }\n return a;\n};\nfunction I(o, r, s, n, t) {\n const a = new Uint8ClampedArray(n * t);\n if (r === n && s === t) {\n for (let i = 0; i < o.length; i++)\n a[i] = Math.min(Math.max(o[i], 0), 255);\n return a;\n }\n const m = (r - 1) / (n - 1), M = (s - 1) / (t - 1);\n for (let i = 0; i < t; i++)\n for (let c = 0; c < n; c++) {\n const e = m * c, l = M * i, f = Math.floor(e), u = Math.floor(l), d = Math.min(Math.ceil(e), r - 1), S = Math.min(Math.ceil(l), s - 1), y = e - f, h = l - u, p = o[u * r + f], w = o[u * r + d], P = o[S * r + f], x = o[S * r + d], k = p * (1 - y) * (1 - h) + w * y * (1 - h) + P * (1 - y) * h + x * y * h;\n a[i * n + c] = Math.min(Math.max(k, 0), 255);\n }\n return a;\n}\nfunction R(o, r, s, n, t) {\n const a = new Uint8ClampedArray(n * t), m = r / n, M = s / t, i = (e, l) => {\n if (e === 0) return 1;\n if (e < 0 && (e = -e), e < l) {\n const f = Math.PI * e;\n return l * Math.sin(f) * Math.sin(f / l) / (f * f);\n }\n return 0;\n }, c = 3;\n for (let e = 0; e < t; e++)\n for (let l = 0; l < n; l++) {\n const f = l * m, u = e * M, d = Math.floor(f), S = Math.floor(u);\n let y = 0, h = 0;\n for (let p = S - c + 1; p <= S + c; p++)\n for (let w = d - c + 1; w <= d + c; w++)\n if (w >= 0 && w < r && p >= 0 && p < s) {\n const P = o[p * r + w], x = i(f - w, c) * i(u - p, c);\n y += P * x, h += x;\n }\n a[e * n + l] = h !== 0 ? Math.round(y / h) : 0;\n }\n return a;\n}\nconst A = (o, r) => !r || o.startsWith(\"http\") ? new URL(o).href : o ? r.replace(/\\/+$/, \"\") + \"/\" + o.replace(/^\\/+/, \"\") : r;\nfunction L(o, r, s) {\n const n = o.createShader(r);\n if (!n)\n throw new Error(\"[ladon-ts] - error creating shader\");\n if (o.shaderSource(n, s), o.compileShader(n), !o.getShaderParameter(n, o.COMPILE_STATUS)) {\n const t = o.getShaderInfoLog(n) ?? \"unknown error compiling shader.\";\n throw new Error(\"[ladon-ts] - \" + t);\n }\n return n;\n}\nfunction T(o, r, s) {\n const n = o.createProgram();\n if (!n)\n throw new Error(\"[ladon-ts] - error creating GPU program\");\n if (o.attachShader(n, r), o.attachShader(n, s), o.linkProgram(n), !o.getProgramParameter(n, o.LINK_STATUS)) {\n const t = o.getProgramInfoLog(n) ?? \"unknown error linking program.\";\n throw new Error(\"[ladon-ts] - \" + t);\n }\n return n;\n}\nfunction b(o) {\n if (o.startsWith(\"blob:\"))\n return !0;\n try {\n return new URL(o), !0;\n } catch {\n return !1;\n }\n}\nasync function U(o) {\n const r = await caches.open(\"ladon-cache\"), s = await r.match(o);\n if (s)\n return s.arrayBuffer();\n const n = await fetch(o);\n if (!n.ok)\n throw new Error(`Failed to fetch file: ${o}`);\n const t = n.headers.get(\"Content-Type\") || \"application/octet-stream\";\n if (!t.includes(\"application/wasm\") && !t.includes(\"binary/octet-stream\"))\n throw new Error(`Unexpected MIME type: ${t} for ${o}`);\n const a = await n.arrayBuffer(), m = new Response(a, {\n headers: {\n \"Content-Type\": t,\n \"Content-Length\": a.byteLength.toString()\n }\n });\n return await r.put(o, m), a;\n}\nexport {\n C as InterpolationMethod,\n I as bilinearUpsample,\n L as compileShader,\n b as isResolvedUri,\n R as lanczos3Upsample,\n T as linkProgram,\n U as loadAndCacheFile,\n E as resizeImage,\n A as toAbsoluteURI\n};\n//# sourceMappingURL=util.js.map\n","import * as i from \"onnxruntime-web\";\nimport { loadAndCacheFile as h, isResolvedUri as c, toAbsoluteURI as m } from \"./util.js\";\nconst k = \"ort-wasm-simd-threaded.wasm\";\nclass U {\n constructor() {\n this.modelSessions = {}, this.resolvedUriCache = /* @__PURE__ */ new Map();\n }\n /**\n * Initialize the worker using the provided configuration.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the worker is initialized.\n */\n async initializeWorker(e) {\n if (typeof Worker > \"u\")\n throw new Error(\"[ladon-ts] web workers are not supported in this environment\");\n const r = await this.resolveUri(e, e.mask.workerUri), s = await (await fetch(r, { cache: \"force-cache\" })).text();\n return this.workerUrl = URL.createObjectURL(new Blob([s], { type: \"text/javascript\" })), new Worker(this.workerUrl, { type: \"module\" });\n }\n /**\n * Terminate the worker.\n */\n terminateWorker() {\n this.worker && (this.worker.terminate(), this.worker = void 0);\n }\n /**\n * Load the model using the provided configuration. This gets called by the worker when using\n * the worker generator.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the model is preloaded.\n */\n async loadModel(e) {\n const { mask: r, executionProviders: o } = e;\n i.env.wasm.numThreads = 1;\n const s = await this.resolveUri(e, e.wasmUri);\n if (s)\n try {\n i.env.wasm.wasmBinary = await h(s + k);\n } catch (w) {\n console.warn(\"Failed to load or cache WASM file. Using WASM paths.\", w), i.env.wasm.wasmPaths = s;\n }\n const a = await this.resolveUri(e, r.modelUri);\n let t = this.modelSessions[a];\n if (!t) {\n const n = { executionProviders: o != null && o.length ? o : [\"wasm\"] };\n try {\n const l = await h(a);\n t = await i.InferenceSession.create(l, n);\n } catch (l) {\n console.warn(\"Failed to load or cache model file. Using model URL.\", l), t = await i.InferenceSession.create(a, n);\n }\n }\n return this.modelSessions[a] = t, await this.warmupModel(t, r.inputSize, r.modelRank), t;\n }\n /**\n * Preload the worker using the provided configuration.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the worker is preloaded.\n */\n async preloadWorker(e) {\n const r = await this.prepareWorkerConfig(e);\n this.worker = await this.initializeWorker(r), this.worker.postMessage({ type: \"preload\", config: r });\n }\n /**\n * Warmup the model session with a dummy input to avoid latency when running the model for the first time.\n *\n * @param {ort.InferenceSession} session The model session.\n * @param {ResolutionConfig} inputSize The input size of the model.\n * @param {number} inputChannels The number of input channels (e.g., RGB is 3).\n * @param {ort.Tensor.Type} inputType The input tensor type.\n * @returns {Promise<void>} A promise that resolves when the model is warmed up.\n * @example\n * await warmupModel(session, inputSize, inputChannels, inputType);\n */\n async warmupModel(e, r = { width: 1280, height: 720 }, o = 3, s = \"float32\") {\n const a = r.height * r.width, t = new Float32Array(o * a).fill(0), w = new i.Tensor(s, t, [\n 1,\n o,\n r.height,\n r.width\n ]), n = { [e.inputNames[0]]: w };\n if (o === 3) {\n const l = new Float32Array(a).fill(0), d = new i.Tensor(s, l, [\n 1,\n 1,\n r.height,\n r.width\n ]);\n n[e.inputNames[1]] = d;\n }\n await e.run(n);\n }\n /**\n * Get the model session for the provided model URL.\n *\n * @param {string} modelUrl The model URL.\n * @returns {ort.InferenceSession} The model session.\n * @example\n * const session = getModelSession(modelUrl);\n */\n getModelSession(e) {\n return this.modelSessions[e];\n }\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths\n * and resolved using the `toAbsoluteURI` method.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved\n * using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in\n * cache, it will be returned directly. If an assetUrlResolver function is provided in the\n * configuration, it will be used to resolve the URL if the assetUri is not fully qualified,\n * a blob URL, or in the cache.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n async resolveUri(e, r) {\n if (!r)\n throw new Error(\"Asset URI is undefined.\");\n if (this.resolvedUriCache.has(r))\n return this.resolvedUriCache.get(r);\n let o;\n if (c(r) ? o = r : r.match(/\\.[a-z0-9]+$/i) ? typeof e.assetUrlResolver == \"function\" && (o = await e.assetUrlResolver(r, e)) : o = m(r, e.baseUrl), !o)\n throw new Error(`Failed to resolve asset URI: ${r}`);\n return this.resolvedUriCache.set(r, o), o;\n }\n getWorker() {\n return this.worker;\n }\n getWorkerUrl() {\n return this.workerUrl;\n }\n /**\n * Prepare the worker configuration by resolving the final URLs and paths to send to the\n * worker. This is necessary because we can't send functions to the worker. We also need to\n * remove the assetUrlResolver function from the configuration.\n *\n * @param config - The preload configuration.\n * @returns The worker configuration.\n */\n async prepareWorkerConfig(e) {\n const o = { ...{\n ...e,\n mask: {\n ...e.mask,\n modelUri: await this.resolveUri(e, e.mask.modelUri),\n workerUri: await this.resolveUri(e, e.mask.workerUri)\n },\n wasmUri: await this.resolveUri(e, e.wasmUri)\n }, assetUrlResolver: void 0 };\n return JSON.parse(JSON.stringify(o));\n }\n}\nconst p = new U();\nexport {\n p as sharedResourceManager\n};\n//# sourceMappingURL=shared-resource-manager.js.map\n","import * as c from \"onnxruntime-web\";\nimport { sharedResourceManager as d } from \"./shared-resource-manager.js\";\nclass R {\n constructor(t) {\n this.config = t, this.lastMaskGenerationTime = 0;\n const { height: a, width: e } = this.config.mask.outputSize;\n this.lastMask = new c.Tensor(\"float32\", new Float32Array(a * e), [\n 1,\n 1,\n a,\n e\n ]), this.lastResult = {\n data: this.lastMask.data,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: !1\n };\n }\n /**\n * This method preprocesses an input Uint8ClampedArray to a Float32Array\n * while maintaining memory efficiency by reusing a class-scoped output buffer.\n * If the model rank is 4 (e.g., newer tiny model), it sets an alpha channel value.\n *\n * @param {Uint8ClampedArray} input - The input data to be preprocessed\n * @param {Float32Array} alpha - The alpha channel data\n * @returns {Float32Array} - The processed data\n */\n preprocess(t, a) {\n const {\n modelRank: e,\n inputSize: l,\n outputSize: s\n } = this.config.mask, { height: r, width: i } = l, o = r * i;\n this.output ?? (this.output = new Float32Array(o * e));\n const k = t.length / 4;\n for (let n = 0; n < 3; n++)\n for (let m = 0; m < k; m++)\n this.output[m + n * o] = t[m * 4 + (2 - n)];\n const w = s.width / i, g = s.height / r;\n for (let n = 0; n < i; n++) {\n const m = Math.floor(n * w);\n for (let h = 0; h < r; h++) {\n const f = Math.floor(h * g), p = m + f * s.width, M = n + h * i + 3 * o, y = Math.min(Math.max(a[p], 0), 255);\n this.output[M] = y;\n }\n }\n return this.output;\n }\n getTimeSinceLastGeneration() {\n return Date.now() - this.lastMaskGenerationTime;\n }\n async getLadonOutput(t, a) {\n var s, r, i, o;\n if (!this.maskModel)\n throw new Error(\"[ladon-ts] - mask model not loaded.\");\n if (this.getTimeSinceLastGeneration() < this.config.mask.generationIntervalMs)\n return {\n data: this.lastMask.data,\n facesAndLandmarks: this.lastResult.facesAndLandmarks,\n gesture: this.lastResult.gesture,\n motion: this.lastResult.motion,\n timestamp: this.lastResult.timestamp,\n warm: !1\n };\n this.lastMaskGenerationTime = Date.now();\n const e = new c.Tensor(\"float32\", this.preprocess(t, this.lastMask.data), [\n 1,\n this.config.mask.modelRank,\n this.config.mask.inputSize.height,\n this.config.mask.inputSize.width\n ]), l = await this.maskModel.run({ input: e });\n return this.lastMask = l.segmentation, this.lastResult = {\n data: this.lastMask.data ?? new Float32Array(0),\n facesAndLandmarks: {\n data: ((s = l.faces_and_landmarks) == null ? void 0 : s.data) ?? new Float32Array(0),\n dims: ((r = l.faces_and_landmarks) == null ? void 0 : r.dims) ?? []\n },\n gesture: ((i = l.gesture) == null ? void 0 : i.data) ?? new Float32Array(0),\n motion: ((o = l.motion) == null ? void 0 : o.data[0]) ?? 0,\n timestamp: a,\n warm: !0\n }, this.lastResult;\n }\n async load() {\n const t = await d.resolveUri(\n this.config,\n this.config.mask.modelUri\n );\n if (this.maskModel = d.getModelSession(t), this.maskModel ?? (this.maskModel = await d.loadModel(this.config)), !this.maskModel)\n throw new Error(\"Failed to load the ONNX model.\");\n }\n isLoaded() {\n return !!this.maskModel;\n }\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n async postRender(t) {\n }\n destroy() {\n }\n}\nclass L {\n constructor(t) {\n this.config = t, this.lastMask = new Float32Array(\n this.config.mask.outputSize.height * this.config.mask.outputSize.width\n );\n }\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n async getLadonOutput(t) {\n return {\n data: this.lastMask,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: !1\n };\n }\n async load() {\n }\n isLoaded() {\n return !0;\n }\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n async postRender(t) {\n }\n destroy() {\n }\n}\nclass F {\n constructor(t) {\n this.config = t, this.newMask = !0, this.lastMask = new Float32Array(\n this.config.mask.outputSize.height * this.config.mask.outputSize.width\n ), this.lastResult = {\n data: this.lastMask,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: !1\n };\n }\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n async getLadonOutput(t) {\n const a = {\n data: this.lastMask,\n facesAndLandmarks: this.lastResult.facesAndLandmarks,\n gesture: this.lastResult.gesture,\n motion: this.lastResult.motion,\n timestamp: this.lastResult.timestamp,\n warm: this.newMask\n };\n return this.newMask = !1, a;\n }\n async load() {\n const t = await d.prepareWorkerConfig(this.config);\n if (this.worker = d.getWorker(), this.worker ?? (this.worker = await d.initializeWorker(t)), !this.worker)\n throw new Error(\"[ladon-ts] - worker is not defined.\");\n this.worker.onmessage = (s) => {\n if (s.data.type === \"log\") {\n const { message: r, severity: i } = s.data, o = `[ladon-ts] Message received from worker: ${r}`;\n switch (i) {\n case \"warn\":\n console.warn(o);\n break;\n case \"error\":\n console.error(o);\n break;\n default:\n console.log(o);\n }\n } else if (s.data.status === \"generated\")\n this.lastMask = s.data.mask, this.lastResult = {\n data: s.data.mask,\n facesAndLandmarks: s.data.facesAndLandmarks,\n gesture: s.data.gesture,\n motion: s.data.motion,\n timestamp: s.data.timestamp,\n warm: !0\n }, this.newMask = !0;\n else {\n const r = d.getWorkerUrl();\n r && URL.revokeObjectURL(r), e == null || e();\n }\n };\n const a = {\n baseUrl: t.baseUrl,\n input: t.input,\n mask: t.mask,\n wasmUri: t.wasmUri,\n render: { type: \"passthrough\" }\n };\n this.worker.postMessage({ type: \"init\", config: a });\n let e;\n return new Promise((s) => {\n e = s;\n });\n }\n isLoaded() {\n return !!this.worker;\n }\n async postRender(t, a) {\n if (!this.worker)\n throw new Error(\"[ladon-ts] - worker is not defined.\");\n this.worker.postMessage(\n {\n type: \"generate\",\n image: t,\n timestamp: a\n },\n [t.buffer]\n );\n }\n destroy() {\n d.terminateWorker();\n }\n}\nexport {\n R as LocalGenerator,\n L as NullGenerator,\n F as WorkerGenerator\n};\n//# sourceMappingURL=generator.js.map\n","import './shim';\nimport './console-overrides';\n\nimport {\n LocalGenerator,\n PipelineConfig,\n PreloadConfig,\n sharedResourceManager,\n} from '@webex/ladon-ts';\n\nlet generator: LocalGenerator;\nlet busy = false;\n\n// Message handler for worker\nself.addEventListener('message', async (event) => {\n const { type, config, image, timestamp } = event.data;\n\n try {\n switch (type) {\n case 'preload':\n if (config) {\n await handlePreload(config as PreloadConfig);\n }\n break;\n\n case 'init':\n if (config) {\n await handleInit(config as PipelineConfig);\n }\n break;\n\n case 'generate':\n if (image) {\n await handleGenerate(image, timestamp);\n }\n break;\n\n default:\n console.warn(`[ladon-ts] - worker generator - unknown message: ${JSON.stringify(event.data)}`);\n }\n } catch (error) {\n console.error(`[ladon-ts] - worker generator - error handling message: ${error}`);\n }\n});\n\n// Handle preload event\nasync function handlePreload(config: PreloadConfig): Promise<void> {\n await sharedResourceManager.loadModel(config);\n postMessage({ status: 'preloaded' });\n}\n\n// Handle initialization\nasync function handleInit(config: PipelineConfig): Promise<void> {\n generator = new LocalGenerator(config);\n await generator.load();\n postMessage({ status: 'initialized' });\n}\n\n// Handle generate event with SharedArrayBuffer support\nasync function handleGenerate(image: Uint8ClampedArray | null, timestamp: number): Promise<void> {\n if (busy) {\n return;\n }\n\n if (!generator || !generator.isLoaded()) {\n console.warn('[ladon-ts] - Generator is not initialized');\n return;\n }\n\n busy = true;\n\n if (!image) {\n console.warn('[ladon-ts] - No valid input provided for generation');\n busy = false;\n return;\n }\n\n // Run generator\n const maskResult = await generator.getLadonOutput(image, timestamp);\n\n if (maskResult.warm) {\n postMessage({\n facesAndLandmarks: maskResult.facesAndLandmarks,\n gesture: maskResult.gesture,\n mask: maskResult.data, // Transfer mask directly if TypedArray\n motion: maskResult.motion,\n timestamp: maskResult.timestamp,\n status: 'generated',\n });\n }\n\n setTimeout(() => {\n busy = false;\n }, 0);\n}\n"],"names":["args","o","U","s","n","t","a","m","e","r","i.env","h","w","l","i.InferenceSession","i.Tensor","d","c","c.Tensor","i","k","g","f","p","M","y","event","type","config","image","timestamp","error","sharedResourceManager","LocalGenerator","maskResult"],"mappings":"oGAQC,KAAK,SAAoB,CACtB,QAAS,SAAS,MACtB,ECNA,MAAM,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K,OACGA,GACA,MAAM,WAAWA,CAAwB,EAE7C,MAEJ,IAAK,WACGC,GACM,MAAA,eAAeA,EAAOC,CAAS,EAEzC,MAEJ,QACI,QAAQ,KAAK,oDAAoD,KAAK,UAAUJ,EAAM,IAAI,CAAC,EAAE,CAAA,QAEhGK,EAAO,CACJ,QAAA,MAAM,2DAA2DA,CAAK,EAAE,CAAA,CAExF,CAAC,EAGD,eAAe,cAAcH,EAAsC,CACzD,MAAAI,EAAsB,UAAUJ,CAAM,EAChC,YAAA,CAAE,OAAQ,YAAa,CACvC,CAGA,eAAe,WAAWA,EAAuC,CACjD,UAAA,IAAIK,EAAeL,CAAM,EACrC,MAAM,UAAU,KAAK,EACT,YAAA,CAAE,OAAQ,cAAe,CACzC,CAGA,eAAe,eAAeC,EAAiCC,EAAkC,CAC7F,GAAI,KACA,OAGJ,GAAI,CAAC,WAAa,CAAC,UAAU,WAAY,CACrC,QAAQ,KAAK,2CAA2C,EACxD,MAAA,CAKJ,GAFO,KAAA,GAEH,CAACD,EAAO,CACR,QAAQ,KAAK,qDAAqD,EAC3D,KAAA,GACP,MAAA,CAIJ,MAAMK,EAAa,MAAM,UAAU,eAAeL,EAAOC,CAAS,EAE9DI,EAAW,MACC,YAAA,CACR,kBAAmBA,EAAW,kBAC9B,QAASA,EAAW,QACpB,KAAMA,EAAW,KACjB,OAAQA,EAAW,OACnB,UAAWA,EAAW,UACtB,OAAQ,WAAA,CACX,EAGL,WAAW,IAAM,CACN,KAAA,IACR,CAAC,CACR"}
|
|
1
|
+
{"version":3,"file":"segmentation.js","sources":["../src/shim.ts","../src/console-overrides.ts","../../build/module/lib/util.js","../../build/module/lib/resource-loader.js","../src/util.ts","../src/model-manager.ts","../src/ladon-generator.ts","../src/segmentation.ts"],"sourcesContent":["/**\n * This file is used to provide a shim for the document object in a web worker.\n * The document object is not available in a web worker, so we need to provide a shim\n * to prevent errors when running the code in a web worker.\n * \n * See: https://github.com/vitejs/vite/issues/9879#issuecomment-1372112651\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\n(self.document as any) = {\n baseURI: location.origin\n};","/**\n * Override console methods to send logs to the main thread for display in the UI.\n */\n\nconst originalConsoleLog = console.log;\nconst originalConsoleWarn = console.warn;\nconst originalConsoleError = console.error;\n\nconsole.log = (...args) => {\n postMessage({ type: 'log', message: args.join(' '), severity: 'log' });\n originalConsoleLog(...args);\n};\n\nconsole.warn = (...args) => {\n postMessage({ type: 'log', message: args.join(' '), severity: 'warn' });\n originalConsoleWarn(...args);\n};\n\nconsole.error = (...args) => {\n postMessage({ type: 'log', message: args.join(' '), severity: 'error' });\n originalConsoleError(...args);\n};","var U = /* @__PURE__ */ ((o) => (o.Nearest = \"nearest\", o))(U || {});\nconst A = (o, n, u, r, e) => {\n const l = new Uint8ClampedArray(r * e * 4), p = n / r, y = u / e, s = n * 4;\n let a = 0;\n for (let t = 0; t < e; t++) {\n const f = Math.floor(t * y) * s;\n for (let c = 0; c < r; c++) {\n const m = f + Math.floor(c * p) * 4;\n l[a++] = o[m], l[a++] = o[m + 1], l[a++] = o[m + 2], l[a++] = o[m + 3];\n }\n }\n return l;\n};\nfunction d(o, n, u, r, e) {\n const l = new Uint8ClampedArray(r * e);\n if (n === r && u === e) {\n for (let s = 0; s < o.length; s++)\n l[s] = Math.min(Math.max(o[s], 0), 255);\n return l;\n }\n const p = (n - 1) / (r - 1), y = (u - 1) / (e - 1);\n for (let s = 0; s < e; s++)\n for (let a = 0; a < r; a++) {\n const t = p * a, f = y * s, c = Math.floor(t), m = Math.floor(f), S = Math.min(Math.ceil(t), n - 1), x = Math.min(Math.ceil(f), u - 1), w = t - c, i = f - m, M = o[m * n + c], h = o[m * n + S], k = o[x * n + c], P = o[x * n + S], I = M * (1 - w) * (1 - i) + h * w * (1 - i) + k * (1 - w) * i + P * w * i;\n l[s * r + a] = Math.min(Math.max(I, 0), 255);\n }\n return l;\n}\nfunction E(o, n, u, r, e) {\n const l = new Uint8ClampedArray(r * e), p = n / r, y = u / e, s = (t, f) => {\n if (t === 0) return 1;\n if (t < 0 && (t = -t), t < f) {\n const c = Math.PI * t;\n return f * Math.sin(c) * Math.sin(c / f) / (c * c);\n }\n return 0;\n }, a = 3;\n for (let t = 0; t < e; t++)\n for (let f = 0; f < r; f++) {\n const c = f * p, m = t * y, S = Math.floor(c), x = Math.floor(m);\n let w = 0, i = 0;\n for (let M = x - a + 1; M <= x + a; M++)\n for (let h = S - a + 1; h <= S + a; h++)\n if (h >= 0 && h < n && M >= 0 && M < u) {\n const k = o[M * n + h], P = s(c - h, a) * s(m - M, a);\n w += k * P, i += P;\n }\n l[t * r + f] = i !== 0 ? Math.round(w / i) : 0;\n }\n return l;\n}\nfunction L(o, n, u) {\n const r = o.createShader(n);\n if (!r)\n throw new Error(\"[ladon-ts] - error creating shader\");\n if (o.shaderSource(r, u), o.compileShader(r), !o.getShaderParameter(r, o.COMPILE_STATUS)) {\n const e = o.getShaderInfoLog(r) ?? \"unknown error compiling shader.\";\n throw new Error(\"[ladon-ts] - \" + e);\n }\n return r;\n}\nfunction R(o, n, u) {\n const r = o.createProgram();\n if (!r)\n throw new Error(\"[ladon-ts] - error creating GPU program\");\n if (o.attachShader(r, n), o.attachShader(r, u), o.linkProgram(r), !o.getProgramParameter(r, o.LINK_STATUS)) {\n const e = o.getProgramInfoLog(r) ?? \"unknown error linking program.\";\n throw new Error(\"[ladon-ts] - \" + e);\n }\n return r;\n}\nfunction C(o) {\n if (o.startsWith(\"blob:\"))\n return !0;\n try {\n return new URL(o), !0;\n } catch {\n return !1;\n }\n}\nconst T = (o, n) => !n || o.startsWith(\"http\") ? new URL(o).href : o ? n.replace(/\\/+$/, \"\") + \"/\" + o.replace(/^\\/+/, \"\") : n;\nexport {\n U as InterpolationMethod,\n d as bilinearUpsample,\n L as compileShader,\n C as isResolvedUri,\n E as lanczos3Upsample,\n R as linkProgram,\n A as resizeImage,\n T as toAbsoluteURI\n};\n//# sourceMappingURL=util.js.map\n","import { isResolvedUri as l, toAbsoluteURI as s } from \"./util.js\";\nclass t {\n constructor() {\n this.resolvedUriCache = /* @__PURE__ */ new Map();\n }\n /**\n * Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved\n * using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in\n * cache, it will be returned directly. If an assetUrlResolver function is provided in the\n * configuration, it will be used to resolve the URL if the assetUri is not fully qualified,\n * a blob URL, or in the cache.\n *\n * @param config - The preload configuration.\n * @param assetUri - The asset URI from the config.\n * @returns The resolved full URL.\n */\n async resolveUri(o, e) {\n if (!e)\n throw new Error(\"Asset URI is undefined.\");\n if (this.resolvedUriCache.has(e))\n return this.resolvedUriCache.get(e);\n let r;\n if (l(e) ? r = e : e.match(/\\.[a-z0-9]+$/i) ? typeof o.assetUrlResolver == \"function\" && (r = await o.assetUrlResolver(e, o)) : r = s(e, o.baseUrl), !r)\n throw new Error(`Failed to resolve asset URI: ${e}`);\n return this.resolvedUriCache.set(e, r), r;\n }\n}\nconst d = new t();\nexport {\n t as ResourceLoader,\n d as resourceLoader\n};\n//# sourceMappingURL=resource-loader.js.map\n","export { isResolvedUri, toAbsoluteURI } from '@webex/ladon-ts'\n\nexport async function loadAndCacheFile(fileUrl: string): Promise<ArrayBufferLike | Uint8Array> {\n const cache = await caches.open('ladon-cache');\n\n // Try to get the file from cache using its full URL\n const cachedResponse = await cache.match(fileUrl);\n if (cachedResponse) {\n return cachedResponse.arrayBuffer();\n }\n\n // If not in cache, fetch and cache the file\n const response = await fetch(fileUrl);\n\n // Ensure the response is valid\n if (!response.ok) {\n throw new Error(`Failed to fetch file: ${fileUrl}`);\n }\n\n const contentType = response.headers.get('Content-Type') || 'binary/octet-stream';\n\n // Validate expected MIME types\n if (!contentType.includes('application/wasm') && !contentType.includes('binary/octet-stream')) {\n throw new Error(`Unexpected MIME type: ${contentType} for ${fileUrl}`);\n }\n\n const fileBinary = await response.arrayBuffer();\n\n // Cache the response with proper headers\n const cachedResponseWithHeaders = new Response(fileBinary, {\n headers: {\n 'Content-Type': contentType,\n 'Content-Length': fileBinary.byteLength.toString(),\n },\n });\n\n // Store the response in the cache using the full URL as the key\n await cache.put(fileUrl, cachedResponseWithHeaders);\n\n return fileBinary;\n}\n","import * as ort from 'onnxruntime-web';\nimport { PreloadConfig, ResolutionConfig, resourceLoader } from '@webex/ladon-ts';\n\nimport { loadAndCacheFile } from './util';\n\n// There is only a single WASM file for all WebAssembly binaries now.\nconst ONNX_WASM_FILENAME = 'ort-wasm-simd-threaded.wasm';\n\n/**\n * Resource manager used to load resources for the segmentation worker.\n */\nexport class ModelManager {\n private modelSessions: Record<string, ort.InferenceSession> = {};\n\n /**\n * Load the model using the provided configuration. This gets called by the worker when using\n * the worker generator.\n *\n * @param config - The configuration object.\n * @returns A promise that resolves when the model is preloaded.\n */\n public async loadModel(config: PreloadConfig) {\n const { mask, executionProviders: providers } = config;\n\n // Lock to a single thread to avoid performance issues.\n ort.env.wasm.numThreads = 1;\n\n // Get the resolved paths for the WASM binary.\n const wasmPaths = await resourceLoader.resolveUri(config, config.wasmUri);\n\n if (wasmPaths) {\n try {\n ort.env.wasm.wasmBinary = await loadAndCacheFile(wasmPaths + ONNX_WASM_FILENAME);\n } catch (error) {\n console.warn('Failed to load or cache WASM file. Using WASM paths.', error);\n ort.env.wasm.wasmPaths = wasmPaths;\n }\n }\n\n // Resolve the model URL.\n const modelUrl = await resourceLoader.resolveUri(config, mask.modelUri);\n let modelSession = this.modelSessions[modelUrl];\n\n // If the model session is not already cached, create a new session and cache.\n if (!modelSession) {\n // Allows the user to specify the execution providers to use. The default is the WASM provider.\n // TODO: Add and test support for other execution providers.\n const executionProviders = providers?.length ? providers : ['wasm'];\n const sessionOptions = { executionProviders };\n\n // First try to get the model binary either from cache or by fetching and cacheing it.\n try {\n const modelBinary = await loadAndCacheFile(modelUrl);\n modelSession = await ort.InferenceSession.create(modelBinary, sessionOptions);\n } catch (error) {\n console.warn('Failed to load or cache model file. Using model URL.', error);\n modelSession = await ort.InferenceSession.create(modelUrl, sessionOptions);\n }\n }\n\n // Cache the model session.\n this.modelSessions[modelUrl] = modelSession;\n\n // Warmup the model with a dummy input to avoid latency when running the model for the first time.\n await this.warmupModel(modelSession, mask.inputSize, mask.modelRank);\n\n return modelSession;\n }\n\n /**\n * Warmup the model session with a dummy input to avoid latency when running the model for the first time.\n *\n * @param {ort.InferenceSession} session The model session.\n * @param {ResolutionConfig} inputSize The input size of the model.\n * @param {number} inputChannels The number of input channels (e.g., RGB is 3).\n * @param {ort.Tensor.Type} inputType The input tensor type.\n * @returns {Promise<void>} A promise that resolves when the model is warmed up.\n * @example\n * await warmupModel(session, inputSize, inputChannels, inputType);\n */\n public async warmupModel(\n session: ort.InferenceSession,\n inputSize: ResolutionConfig = { width: 1280, height: 720 },\n inputChannels = 3,\n inputType: ort.Tensor.Type = 'float32'\n ): Promise<void> {\n const numPixels = inputSize.height * inputSize.width;\n const dummyInputData = new Float32Array(inputChannels * numPixels).fill(0);\n const dummyInput = new ort.Tensor(inputType, dummyInputData, [\n 1,\n inputChannels,\n inputSize.height,\n inputSize.width,\n ]);\n const feed = { [session.inputNames[0]]: dummyInput };\n\n // Use an additional dummy tensor if the model expects a previous mask.\n if (inputChannels === 3) {\n const dummyMaskData = new Float32Array(numPixels).fill(0);\n const prevMaskTensor = new ort.Tensor(inputType, dummyMaskData, [\n 1,\n 1,\n inputSize.height,\n inputSize.width,\n ]);\n feed[session.inputNames[1]] = prevMaskTensor;\n }\n\n // Run the model with the dummy input to warm it up.\n await session.run(feed);\n }\n\n /**\n * Get the model session for the provided model URL.\n *\n * @param {string} modelUrl The model URL.\n * @returns {ort.InferenceSession} The model session.\n * @example\n * const session = getModelSession(modelUrl);\n */\n public getModelSession(modelUrl: string) {\n return this.modelSessions[modelUrl];\n }\n \n}\n\nexport const modelManager = new ModelManager();","import * as ort from 'onnxruntime-web';\n\nimport { LadonOutput, MaskGenerator, PipelineConfig, resourceLoader } from '@webex/ladon-ts';\n\nimport { modelManager } from './model-manager';\n\n\nexport class LadonGenerator implements MaskGenerator {\n private lastResult: LadonOutput;\n private lastMask: ort.Tensor;\n private maskModel?: ort.InferenceSession;\n private lastMaskGenerationTime = 0;\n private output?: Float32Array;\n\n constructor(public readonly config: PipelineConfig) {\n const { height, width } = this.config.mask.outputSize;\n this.lastMask = new ort.Tensor('float32', new Float32Array(height * width), [\n 1,\n 1,\n height,\n width,\n ]);\n this.lastResult = {\n data: this.lastMask.data,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: false,\n };\n }\n\n /**\n * This method preprocesses an input Uint8ClampedArray to a Float32Array\n * while maintaining memory efficiency by reusing a class-scoped output buffer.\n * If the model rank is 4 (e.g., newer tiny model), it sets an alpha channel value.\n *\n * @param {Uint8ClampedArray} input - The input data to be preprocessed\n * @param {Float32Array} alpha - The alpha channel data\n * @returns {Float32Array} - The processed data\n */\n private preprocess(input: Uint8ClampedArray, alpha: Float32Array): Float32Array {\n const {\n modelRank: model_rank,\n inputSize: input_size,\n outputSize: output_size,\n } = this.config.mask;\n const { height, width } = input_size;\n const numImagePixels = height * width;\n\n // Initialize the output buffer if it is not already set\n this.output ??= new Float32Array(numImagePixels * model_rank);\n\n const numInputPixels = input.length / 4; // Divide by 4 for RGBA\n\n // Directly copy channels (assumes fixed order like BGR or RGB)\n for (let channel = 0; channel < 3; channel++) {\n for (let pixel = 0; pixel < numInputPixels; pixel++) {\n // Map input channels directly to output\n this.output[pixel + channel * numImagePixels] = input[pixel * 4 + (2 - channel)];\n }\n }\n\n // Scaling factors for width and height\n const widthScale = output_size.width / width;\n const heightScale = output_size.height / height;\n\n // Process alpha channel\n for (let x = 0; x < width; x++) {\n const nearestNeighborX = Math.floor(x * widthScale);\n for (let y = 0; y < height; y++) {\n const nearestNeighborY = Math.floor(y * heightScale);\n const maskIdx = nearestNeighborX + nearestNeighborY * output_size.width;\n const outputIdx = x + y * width + 3 * numImagePixels;\n\n // Normalize and scale alpha to [0, 255]\n const normalizedValue = Math.min(Math.max(alpha[maskIdx], 0), 255);\n this.output[outputIdx] = normalizedValue;\n }\n }\n\n return this.output;\n }\n\n public getTimeSinceLastGeneration(): number {\n return Date.now() - this.lastMaskGenerationTime;\n }\n\n public async getLadonOutput(input: Uint8ClampedArray, timestamp: number): Promise<LadonOutput> {\n if (!this.maskModel) {\n throw new Error('[ladon-ts] - mask model not loaded.');\n }\n\n if (this.getTimeSinceLastGeneration() < this.config.mask.generationIntervalMs) {\n return {\n data: this.lastMask.data,\n facesAndLandmarks: this.lastResult.facesAndLandmarks,\n gesture: this.lastResult.gesture,\n motion: this.lastResult.motion,\n timestamp: this.lastResult.timestamp,\n warm: false,\n };\n }\n\n this.lastMaskGenerationTime = Date.now();\n\n const imageTensor = new ort.Tensor('float32', this.preprocess(input, this.lastMask.data), [\n 1,\n this.config.mask.modelRank,\n this.config.mask.inputSize.height,\n this.config.mask.inputSize.width,\n ]);\n\n const results = await this.maskModel.run({ input: imageTensor });\n this.lastMask = results.segmentation;\n\n this.lastResult = {\n data: this.lastMask.data ?? new Float32Array(0),\n facesAndLandmarks: {\n data: results.faces_and_landmarks?.data ?? new Float32Array(0),\n dims: results.faces_and_landmarks?.dims ?? ([] as readonly number[]),\n },\n gesture: results.gesture?.data ?? new Float32Array(0),\n motion: results.motion?.data[0] ?? 0,\n timestamp,\n warm: true,\n };\n\n return this.lastResult;\n }\n\n async load() {\n const modelUrl = await resourceLoader.resolveUri(\n this.config,\n this.config.mask.modelUri\n );\n\n // Attempt to retrieve a preloaded InferenceSession from the shared resource manager.\n this.maskModel = modelManager.getModelSession(modelUrl);\n // If the model is not preloaded, load it from the provided model URL.\n this.maskModel ??= await modelManager.loadModel(this.config);\n\n if (!this.maskModel) {\n throw new Error('Failed to load the ONNX model.');\n }\n }\n\n public isLoaded(): boolean {\n return !!this.maskModel;\n }\n\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n public async postRender(_: Uint8ClampedArray): Promise<void> {\n // empty for local generator\n }\n\n public destroy(): void {\n // empty for local generator\n }\n}","import './shim';\nimport './console-overrides';\n\nimport { PipelineConfig, PreloadConfig } from '@webex/ladon-ts';\n\nimport { LadonGenerator } from './ladon-generator';\nimport { modelManager } from './model-manager';\n\nlet preloadPromise: Promise<void> | null = null;\nlet generator: LadonGenerator;\nlet busy = false;\n\n// Message handler for worker\nself.addEventListener('message', async (event) => {\n const { type, config, image, timestamp } = event.data;\n\n try {\n switch (type) {\n case 'preload':\n if (config) {\n preloadPromise = handlePreload(config as PreloadConfig);\n await preloadPromise;\n }\n break;\n\n case 'init':\n if (config) {\n // Wait for preload to complete before initializing\n if (preloadPromise) {\n await preloadPromise;\n }\n await handleInit(config as PipelineConfig);\n }\n break;\n\n case 'generate':\n if (image) {\n await handleGenerate(image, timestamp);\n }\n break;\n\n default:\n console.warn(`[ladon-ts] - worker generator - unknown message: ${JSON.stringify(event.data)}`);\n }\n } catch (error) {\n console.error(`[ladon-ts] - worker generator - error handling message: ${error}`);\n postMessage({ status: 'error', error: error.message });\n }\n});\n\n// Handle preload event\nasync function handlePreload(config: PreloadConfig): Promise<void> {\n await modelManager.loadModel(config);\n postMessage({ status: 'preloaded' });\n}\n\n// Handle initialization\nasync function handleInit(config: PipelineConfig): Promise<void> {\n generator = new LadonGenerator(config);\n await generator.load();\n postMessage({ status: 'initialized' });\n}\n\n// Handle generate event with SharedArrayBuffer support\nasync function handleGenerate(image: Uint8ClampedArray | null, timestamp: number): Promise<void> {\n if (busy) {\n return;\n }\n\n if (!generator || !generator.isLoaded()) {\n console.warn('[ladon-ts] - Generator is not initialized');\n return;\n }\n\n busy = true;\n\n if (!image) {\n console.warn('[ladon-ts] - No valid input provided for generation');\n busy = false;\n return;\n }\n\n // Run generator\n const maskResult = await generator.getLadonOutput(image, timestamp);\n\n if (maskResult.warm) {\n postMessage({\n facesAndLandmarks: maskResult.facesAndLandmarks,\n gesture: maskResult.gesture,\n mask: maskResult.data, // Transfer mask directly if TypedArray\n motion: maskResult.motion,\n timestamp: maskResult.timestamp,\n status: 'generated',\n });\n }\n\n setTimeout(() => {\n busy = false;\n }, 0);\n}\n"],"names":["args","o","e","r","l","s","fileUrl","cache","cachedResponse","response","contentType","fileBinary","cachedResponseWithHeaders","config","mask","providers","ort.env","wasmPaths","resourceLoader","error","modelUrl","modelSession","sessionOptions","modelBinary","ort.InferenceSession","session","inputSize","inputChannels","inputType","numPixels","dummyInputData","dummyInput","ort.Tensor","feed","dummyMaskData","prevMaskTensor","height","width","input","alpha","model_rank","input_size","output_size","numImagePixels","numInputPixels","channel","pixel","widthScale","heightScale","x","nearestNeighborX","y","nearestNeighborY","maskIdx","outputIdx","normalizedValue","timestamp","imageTensor","results","_a","_b","_c","_d","_","event","type","image","maskResult"],"mappings":"oGAQC,KAAK,SAAoB,CACtB,QAAS,SAAS,MACtB,ECNA,MAAM,mBAAqB,QAAQ,IAC7B,oBAAsB,QAAQ,KAC9B,qBAAuB,QAAQ,MAErC,QAAQ,IAAM,IAAIA,IAAS,CACX,YAAA,CAAE,KAAM,MAAO,QAASA,EAAK,KAAK,GAAG,EAAG,SAAU,MAAO,EACrE,mBAAmB,GAAGA,CAAI,CAC9B,EAEA,QAAQ,KAAO,IAAIA,IAAS,CACZ,YAAA,CAAE,KAAM,MAAO,QAASA,EAAK,KAAK,GAAG,EAAG,SAAU,OAAQ,EACtE,oBAAoB,GAAGA,CAAI,CAC/B,EAEA,QAAQ,MAAQ,IAAIA,IAAS,CACb,YAAA,CAAE,KAAM,MAAO,QAASA,EAAK,KAAK,GAAG,EAAG,SAAU,QAAS,EACvE,qBAAqB,GAAGA,CAAI,CAChC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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B,YAAA,CAAE,OAAQ,YAAa,CACvC,CAGA,eAAe,WAAWA,EAAuC,CACjD,UAAA,IAAI,eAAeA,CAAM,EACrC,MAAM,UAAU,KAAK,EACT,YAAA,CAAE,OAAQ,cAAe,CACzC,CAGA,eAAe,eAAeqD,EAAiCV,EAAkC,CAC7F,GAAI,KACA,OAGJ,GAAI,CAAC,WAAa,CAAC,UAAU,WAAY,CACrC,QAAQ,KAAK,2CAA2C,EACxD,MAAA,CAKJ,GAFO,KAAA,GAEH,CAACU,EAAO,CACR,QAAQ,KAAK,qDAAqD,EAC3D,KAAA,GACP,MAAA,CAIJ,MAAMC,EAAa,MAAM,UAAU,eAAeD,EAAOV,CAAS,EAE9DW,EAAW,MACC,YAAA,CACR,kBAAmBA,EAAW,kBAC9B,QAASA,EAAW,QACpB,KAAMA,EAAW,KACjB,OAAQA,EAAW,OACnB,UAAWA,EAAW,UACtB,OAAQ,WAAA,CACX,EAGL,WAAW,IAAM,CACN,KAAA,IACR,CAAC,CACR"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webex/ladon-ts",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "WebexIntelligence computer vision library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "build/module/index.js",
|
|
@@ -74,9 +74,6 @@
|
|
|
74
74
|
"optionalDependencies": {
|
|
75
75
|
"@rollup/rollup-linux-x64-gnu": "4.36.0"
|
|
76
76
|
},
|
|
77
|
-
"dependencies": {
|
|
78
|
-
"onnxruntime-web": "1.20.1"
|
|
79
|
-
},
|
|
80
77
|
"files": [
|
|
81
78
|
"build/main",
|
|
82
79
|
"build/module",
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import * as c from "onnxruntime-web";
|
|
2
|
-
import { sharedResourceManager as d } from "./shared-resource-manager.js";
|
|
3
|
-
class R {
|
|
4
|
-
constructor(t) {
|
|
5
|
-
this.config = t, this.lastMaskGenerationTime = 0;
|
|
6
|
-
const { height: a, width: e } = this.config.mask.outputSize;
|
|
7
|
-
this.lastMask = new c.Tensor("float32", new Float32Array(a * e), [
|
|
8
|
-
1,
|
|
9
|
-
1,
|
|
10
|
-
a,
|
|
11
|
-
e
|
|
12
|
-
]), this.lastResult = {
|
|
13
|
-
data: this.lastMask.data,
|
|
14
|
-
facesAndLandmarks: { data: new Float32Array(0), dims: [] },
|
|
15
|
-
gesture: new Float32Array(0),
|
|
16
|
-
motion: 0,
|
|
17
|
-
timestamp: 0,
|
|
18
|
-
warm: !1
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* This method preprocesses an input Uint8ClampedArray to a Float32Array
|
|
23
|
-
* while maintaining memory efficiency by reusing a class-scoped output buffer.
|
|
24
|
-
* If the model rank is 4 (e.g., newer tiny model), it sets an alpha channel value.
|
|
25
|
-
*
|
|
26
|
-
* @param {Uint8ClampedArray} input - The input data to be preprocessed
|
|
27
|
-
* @param {Float32Array} alpha - The alpha channel data
|
|
28
|
-
* @returns {Float32Array} - The processed data
|
|
29
|
-
*/
|
|
30
|
-
preprocess(t, a) {
|
|
31
|
-
const {
|
|
32
|
-
modelRank: e,
|
|
33
|
-
inputSize: l,
|
|
34
|
-
outputSize: s
|
|
35
|
-
} = this.config.mask, { height: r, width: i } = l, o = r * i;
|
|
36
|
-
this.output ?? (this.output = new Float32Array(o * e));
|
|
37
|
-
const k = t.length / 4;
|
|
38
|
-
for (let n = 0; n < 3; n++)
|
|
39
|
-
for (let m = 0; m < k; m++)
|
|
40
|
-
this.output[m + n * o] = t[m * 4 + (2 - n)];
|
|
41
|
-
const w = s.width / i, g = s.height / r;
|
|
42
|
-
for (let n = 0; n < i; n++) {
|
|
43
|
-
const m = Math.floor(n * w);
|
|
44
|
-
for (let h = 0; h < r; h++) {
|
|
45
|
-
const f = Math.floor(h * g), p = m + f * s.width, M = n + h * i + 3 * o, y = Math.min(Math.max(a[p], 0), 255);
|
|
46
|
-
this.output[M] = y;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return this.output;
|
|
50
|
-
}
|
|
51
|
-
getTimeSinceLastGeneration() {
|
|
52
|
-
return Date.now() - this.lastMaskGenerationTime;
|
|
53
|
-
}
|
|
54
|
-
async getLadonOutput(t, a) {
|
|
55
|
-
var s, r, i, o;
|
|
56
|
-
if (!this.maskModel)
|
|
57
|
-
throw new Error("[ladon-ts] - mask model not loaded.");
|
|
58
|
-
if (this.getTimeSinceLastGeneration() < this.config.mask.generationIntervalMs)
|
|
59
|
-
return {
|
|
60
|
-
data: this.lastMask.data,
|
|
61
|
-
facesAndLandmarks: this.lastResult.facesAndLandmarks,
|
|
62
|
-
gesture: this.lastResult.gesture,
|
|
63
|
-
motion: this.lastResult.motion,
|
|
64
|
-
timestamp: this.lastResult.timestamp,
|
|
65
|
-
warm: !1
|
|
66
|
-
};
|
|
67
|
-
this.lastMaskGenerationTime = Date.now();
|
|
68
|
-
const e = new c.Tensor("float32", this.preprocess(t, this.lastMask.data), [
|
|
69
|
-
1,
|
|
70
|
-
this.config.mask.modelRank,
|
|
71
|
-
this.config.mask.inputSize.height,
|
|
72
|
-
this.config.mask.inputSize.width
|
|
73
|
-
]), l = await this.maskModel.run({ input: e });
|
|
74
|
-
return this.lastMask = l.segmentation, this.lastResult = {
|
|
75
|
-
data: this.lastMask.data ?? new Float32Array(0),
|
|
76
|
-
facesAndLandmarks: {
|
|
77
|
-
data: ((s = l.faces_and_landmarks) == null ? void 0 : s.data) ?? new Float32Array(0),
|
|
78
|
-
dims: ((r = l.faces_and_landmarks) == null ? void 0 : r.dims) ?? []
|
|
79
|
-
},
|
|
80
|
-
gesture: ((i = l.gesture) == null ? void 0 : i.data) ?? new Float32Array(0),
|
|
81
|
-
motion: ((o = l.motion) == null ? void 0 : o.data[0]) ?? 0,
|
|
82
|
-
timestamp: a,
|
|
83
|
-
warm: !0
|
|
84
|
-
}, this.lastResult;
|
|
85
|
-
}
|
|
86
|
-
async load() {
|
|
87
|
-
const t = await d.resolveUri(
|
|
88
|
-
this.config,
|
|
89
|
-
this.config.mask.modelUri
|
|
90
|
-
);
|
|
91
|
-
if (this.maskModel = d.getModelSession(t), this.maskModel ?? (this.maskModel = await d.loadModel(this.config)), !this.maskModel)
|
|
92
|
-
throw new Error("Failed to load the ONNX model.");
|
|
93
|
-
}
|
|
94
|
-
isLoaded() {
|
|
95
|
-
return !!this.maskModel;
|
|
96
|
-
}
|
|
97
|
-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
98
|
-
async postRender(t) {
|
|
99
|
-
}
|
|
100
|
-
destroy() {
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
class L {
|
|
104
|
-
constructor(t) {
|
|
105
|
-
this.config = t, this.lastMask = new Float32Array(
|
|
106
|
-
this.config.mask.outputSize.height * this.config.mask.outputSize.width
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
110
|
-
async getLadonOutput(t) {
|
|
111
|
-
return {
|
|
112
|
-
data: this.lastMask,
|
|
113
|
-
facesAndLandmarks: { data: new Float32Array(0), dims: [] },
|
|
114
|
-
gesture: new Float32Array(0),
|
|
115
|
-
motion: 0,
|
|
116
|
-
timestamp: 0,
|
|
117
|
-
warm: !1
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
async load() {
|
|
121
|
-
}
|
|
122
|
-
isLoaded() {
|
|
123
|
-
return !0;
|
|
124
|
-
}
|
|
125
|
-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
126
|
-
async postRender(t) {
|
|
127
|
-
}
|
|
128
|
-
destroy() {
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
class F {
|
|
132
|
-
constructor(t) {
|
|
133
|
-
this.config = t, this.newMask = !0, this.lastMask = new Float32Array(
|
|
134
|
-
this.config.mask.outputSize.height * this.config.mask.outputSize.width
|
|
135
|
-
), this.lastResult = {
|
|
136
|
-
data: this.lastMask,
|
|
137
|
-
facesAndLandmarks: { data: new Float32Array(0), dims: [] },
|
|
138
|
-
gesture: new Float32Array(0),
|
|
139
|
-
motion: 0,
|
|
140
|
-
timestamp: 0,
|
|
141
|
-
warm: !1
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
|
145
|
-
async getLadonOutput(t) {
|
|
146
|
-
const a = {
|
|
147
|
-
data: this.lastMask,
|
|
148
|
-
facesAndLandmarks: this.lastResult.facesAndLandmarks,
|
|
149
|
-
gesture: this.lastResult.gesture,
|
|
150
|
-
motion: this.lastResult.motion,
|
|
151
|
-
timestamp: this.lastResult.timestamp,
|
|
152
|
-
warm: this.newMask
|
|
153
|
-
};
|
|
154
|
-
return this.newMask = !1, a;
|
|
155
|
-
}
|
|
156
|
-
async load() {
|
|
157
|
-
const t = await d.prepareWorkerConfig(this.config);
|
|
158
|
-
if (this.worker = d.getWorker(), this.worker ?? (this.worker = await d.initializeWorker(t)), !this.worker)
|
|
159
|
-
throw new Error("[ladon-ts] - worker is not defined.");
|
|
160
|
-
this.worker.onmessage = (s) => {
|
|
161
|
-
if (s.data.type === "log") {
|
|
162
|
-
const { message: r, severity: i } = s.data, o = `[ladon-ts] Message received from worker: ${r}`;
|
|
163
|
-
switch (i) {
|
|
164
|
-
case "warn":
|
|
165
|
-
console.warn(o);
|
|
166
|
-
break;
|
|
167
|
-
case "error":
|
|
168
|
-
console.error(o);
|
|
169
|
-
break;
|
|
170
|
-
default:
|
|
171
|
-
console.log(o);
|
|
172
|
-
}
|
|
173
|
-
} else if (s.data.status === "generated")
|
|
174
|
-
this.lastMask = s.data.mask, this.lastResult = {
|
|
175
|
-
data: s.data.mask,
|
|
176
|
-
facesAndLandmarks: s.data.facesAndLandmarks,
|
|
177
|
-
gesture: s.data.gesture,
|
|
178
|
-
motion: s.data.motion,
|
|
179
|
-
timestamp: s.data.timestamp,
|
|
180
|
-
warm: !0
|
|
181
|
-
}, this.newMask = !0;
|
|
182
|
-
else {
|
|
183
|
-
const r = d.getWorkerUrl();
|
|
184
|
-
r && URL.revokeObjectURL(r), e == null || e();
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
const a = {
|
|
188
|
-
baseUrl: t.baseUrl,
|
|
189
|
-
input: t.input,
|
|
190
|
-
mask: t.mask,
|
|
191
|
-
wasmUri: t.wasmUri,
|
|
192
|
-
render: { type: "passthrough" }
|
|
193
|
-
};
|
|
194
|
-
this.worker.postMessage({ type: "init", config: a });
|
|
195
|
-
let e;
|
|
196
|
-
return new Promise((s) => {
|
|
197
|
-
e = s;
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
isLoaded() {
|
|
201
|
-
return !!this.worker;
|
|
202
|
-
}
|
|
203
|
-
async postRender(t, a) {
|
|
204
|
-
if (!this.worker)
|
|
205
|
-
throw new Error("[ladon-ts] - worker is not defined.");
|
|
206
|
-
this.worker.postMessage(
|
|
207
|
-
{
|
|
208
|
-
type: "generate",
|
|
209
|
-
image: t,
|
|
210
|
-
timestamp: a
|
|
211
|
-
},
|
|
212
|
-
[t.buffer]
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
destroy() {
|
|
216
|
-
d.terminateWorker();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
export {
|
|
220
|
-
R as LocalGenerator,
|
|
221
|
-
L as NullGenerator,
|
|
222
|
-
F as WorkerGenerator
|
|
223
|
-
};
|
|
224
|
-
//# sourceMappingURL=generator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sources":["../../../src/lib/generator.ts"],"sourcesContent":["import * as ort from 'onnxruntime-web';\n\nimport { PipelineConfig } from './config-types';\nimport { sharedResourceManager } from './shared-resource-manager';\n\ndeclare global {\n interface ImportMeta {\n url: string;\n }\n}\n\nexport type FrameData = Uint8ClampedArray | HTMLVideoElement;\n\nexport interface FacesAndLandmarks {\n data: Float32Array;\n dims: readonly number[];\n}\nexport interface LadonOutput {\n data: Float32Array; // 1x1x144x256 or 1x1x360x640\n facesAndLandmarks: FacesAndLandmarks; // 1x1x9x16x1439\n gesture: Float32Array; // 1x9\n motion: number; // 1x1x1x1\n timestamp: number;\n warm: boolean;\n}\n\nexport interface MaskGenerator {\n getLadonOutput(input: Uint8ClampedArray, timestamp: number): Promise<LadonOutput>;\n load(): Promise<void>;\n isLoaded(): boolean;\n postRender(input: Uint8ClampedArray, timestamp: number): Promise<void>;\n destroy(): void;\n}\n\nexport class LocalGenerator implements MaskGenerator {\n private lastResult: LadonOutput;\n private lastMask: ort.Tensor;\n private maskModel?: ort.InferenceSession;\n private lastMaskGenerationTime = 0;\n private output?: Float32Array;\n\n constructor(public readonly config: PipelineConfig) {\n const { height, width } = this.config.mask.outputSize;\n this.lastMask = new ort.Tensor('float32', new Float32Array(height * width), [\n 1,\n 1,\n height,\n width,\n ]);\n this.lastResult = {\n data: this.lastMask.data,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: false,\n };\n }\n\n /**\n * This method preprocesses an input Uint8ClampedArray to a Float32Array\n * while maintaining memory efficiency by reusing a class-scoped output buffer.\n * If the model rank is 4 (e.g., newer tiny model), it sets an alpha channel value.\n *\n * @param {Uint8ClampedArray} input - The input data to be preprocessed\n * @param {Float32Array} alpha - The alpha channel data\n * @returns {Float32Array} - The processed data\n */\n private preprocess(input: Uint8ClampedArray, alpha: Float32Array): Float32Array {\n const {\n modelRank: model_rank,\n inputSize: input_size,\n outputSize: output_size,\n } = this.config.mask;\n const { height, width } = input_size;\n const numImagePixels = height * width;\n\n // Initialize the output buffer if it is not already set\n this.output ??= new Float32Array(numImagePixels * model_rank);\n\n const numInputPixels = input.length / 4; // Divide by 4 for RGBA\n\n // Directly copy channels (assumes fixed order like BGR or RGB)\n for (let channel = 0; channel < 3; channel++) {\n for (let pixel = 0; pixel < numInputPixels; pixel++) {\n // Map input channels directly to output\n this.output[pixel + channel * numImagePixels] = input[pixel * 4 + (2 - channel)];\n }\n }\n\n // Scaling factors for width and height\n const widthScale = output_size.width / width;\n const heightScale = output_size.height / height;\n\n // Process alpha channel\n for (let x = 0; x < width; x++) {\n const nearestNeighborX = Math.floor(x * widthScale);\n for (let y = 0; y < height; y++) {\n const nearestNeighborY = Math.floor(y * heightScale);\n const maskIdx = nearestNeighborX + nearestNeighborY * output_size.width;\n const outputIdx = x + y * width + 3 * numImagePixels;\n\n // Normalize and scale alpha to [0, 255]\n const normalizedValue = Math.min(Math.max(alpha[maskIdx], 0), 255);\n this.output[outputIdx] = normalizedValue;\n }\n }\n\n return this.output;\n }\n\n public getTimeSinceLastGeneration(): number {\n return Date.now() - this.lastMaskGenerationTime;\n }\n\n public async getLadonOutput(input: Uint8ClampedArray, timestamp: number): Promise<LadonOutput> {\n if (!this.maskModel) {\n throw new Error('[ladon-ts] - mask model not loaded.');\n }\n\n if (this.getTimeSinceLastGeneration() < this.config.mask.generationIntervalMs) {\n return {\n data: this.lastMask.data,\n facesAndLandmarks: this.lastResult.facesAndLandmarks,\n gesture: this.lastResult.gesture,\n motion: this.lastResult.motion,\n timestamp: this.lastResult.timestamp,\n warm: false,\n };\n }\n\n this.lastMaskGenerationTime = Date.now();\n\n const imageTensor = new ort.Tensor('float32', this.preprocess(input, this.lastMask.data), [\n 1,\n this.config.mask.modelRank,\n this.config.mask.inputSize.height,\n this.config.mask.inputSize.width,\n ]);\n\n const results = await this.maskModel.run({ input: imageTensor });\n this.lastMask = results.segmentation;\n\n this.lastResult = {\n data: this.lastMask.data ?? new Float32Array(0),\n facesAndLandmarks: {\n data: results.faces_and_landmarks?.data ?? new Float32Array(0),\n dims: results.faces_and_landmarks?.dims ?? ([] as readonly number[]),\n },\n gesture: results.gesture?.data ?? new Float32Array(0),\n motion: results.motion?.data[0] ?? 0,\n timestamp,\n warm: true,\n };\n\n return this.lastResult;\n }\n\n async load() {\n const modelUrl = await sharedResourceManager.resolveUri(\n this.config,\n this.config.mask.modelUri\n );\n\n // Attempt to retrieve a preloaded InferenceSession from the shared resource manager.\n this.maskModel = sharedResourceManager.getModelSession(modelUrl);\n // If the model is not preloaded, load it from the provided model URL.\n this.maskModel ??= await sharedResourceManager.loadModel(this.config);\n\n if (!this.maskModel) {\n throw new Error('Failed to load the ONNX model.');\n }\n }\n\n public isLoaded(): boolean {\n return !!this.maskModel;\n }\n\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n public async postRender(_: Uint8ClampedArray): Promise<void> {\n // empty for local generator\n }\n\n public destroy(): void {\n // empty for local generator\n }\n}\n\nexport class NullGenerator implements MaskGenerator {\n private lastMask: Float32Array;\n\n constructor(public readonly config: PipelineConfig) {\n this.lastMask = new Float32Array(\n this.config.mask.outputSize.height * this.config.mask.outputSize.width\n );\n }\n\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n public async getLadonOutput(_: Uint8ClampedArray): Promise<LadonOutput> {\n return {\n data: this.lastMask,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: false,\n };\n }\n\n public async load(): Promise<void> {\n // empty for null generator\n }\n\n public isLoaded(): boolean {\n return true;\n }\n\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n public async postRender(_: Uint8ClampedArray): Promise<void> {\n // empty for null generator\n }\n\n public destroy(): void {\n // empty for null generator\n }\n}\n\nexport class WorkerGenerator implements MaskGenerator {\n private lastMask: Float32Array;\n private newMask = true;\n private worker?: Worker;\n private lastResult: LadonOutput;\n\n constructor(public readonly config: PipelineConfig) {\n this.lastMask = new Float32Array(\n this.config.mask.outputSize.height * this.config.mask.outputSize.width\n );\n this.lastResult = {\n data: this.lastMask,\n facesAndLandmarks: { data: new Float32Array(0), dims: [] },\n gesture: new Float32Array(0),\n motion: 0,\n timestamp: 0,\n warm: false,\n };\n }\n\n /* eslint-disable-next-line @typescript-eslint/no-unused-vars */\n public async getLadonOutput(_: Uint8ClampedArray): Promise<LadonOutput> {\n const toReturn = {\n data: this.lastMask,\n facesAndLandmarks: this.lastResult.facesAndLandmarks,\n gesture: this.lastResult.gesture,\n motion: this.lastResult.motion,\n timestamp: this.lastResult.timestamp,\n warm: this.newMask,\n };\n\n this.newMask = false;\n\n return toReturn;\n }\n\n public async load(): Promise<void> {\n // Prepare the worker configuration by resolving the final URLs and paths to send to the worker.\n const config = await sharedResourceManager.prepareWorkerConfig(this.config);\n\n this.worker = sharedResourceManager.getWorker();\n this.worker ??= await sharedResourceManager.initializeWorker(config);\n\n if (!this.worker) {\n throw new Error('[ladon-ts] - worker is not defined.');\n }\n\n this.worker.onmessage = (event) => {\n if (event.data.type === 'log') {\n // Forward log messages from the worker to the console.\n const { message, severity } = event.data;\n const msg = `[ladon-ts] Message received from worker: ${message}`;\n switch (severity) {\n case 'warn':\n console.warn(msg);\n break;\n case 'error':\n console.error(msg);\n break;\n default:\n console.log(msg);\n }\n } else if (event.data.status === 'generated') {\n // Update the last mask and result with the new data from the worker.\n this.lastMask = event.data.mask;\n this.lastResult = {\n data: event.data.mask,\n facesAndLandmarks: event.data.facesAndLandmarks,\n gesture: event.data.gesture,\n motion: event.data.motion,\n timestamp: event.data.timestamp,\n warm: true,\n };\n this.newMask = true;\n } else {\n // Clean up the worker and revoke the URL when the worker is finished\n const workerUrl = sharedResourceManager.getWorkerUrl();\n if (workerUrl) {\n URL.revokeObjectURL(workerUrl);\n }\n resolutionCb?.();\n }\n };\n\n const workerConfig: PipelineConfig = {\n baseUrl: config.baseUrl,\n input: config.input,\n mask: config.mask,\n wasmUri: config.wasmUri,\n render: { type: 'passthrough' },\n };\n\n this.worker.postMessage({ type: 'init', config: workerConfig });\n\n let resolutionCb: () => void;\n const result = new Promise<void>((resolve) => {\n resolutionCb = resolve;\n });\n\n return result;\n }\n\n public isLoaded(): boolean {\n return !!this.worker;\n }\n\n public async postRender(input: Uint8ClampedArray, timestamp: number): Promise<void> {\n // claim ownership of the input tensor's buffer and forward to worker\n\n if (!this.worker) {\n throw new Error('[ladon-ts] - worker is not defined.');\n }\n\n this.worker.postMessage(\n {\n type: 'generate',\n image: input,\n timestamp,\n },\n [input.buffer]\n );\n }\n\n public destroy(): void {\n sharedResourceManager.terminateWorker();\n }\n}\n"],"names":["LocalGenerator","config","height","width","ort","input","alpha","model_rank","input_size","output_size","numImagePixels","numInputPixels","channel","pixel","widthScale","heightScale","x","nearestNeighborX","y","nearestNeighborY","maskIdx","outputIdx","normalizedValue","timestamp","imageTensor","results","_a","_b","_c","_d","modelUrl","sharedResourceManager","_","NullGenerator","WorkerGenerator","toReturn","event","message","severity","msg","workerUrl","resolutionCb","workerConfig","resolve"],"mappings":";;AAkCO,MAAMA,EAAwC;AAAA,EAOjD,YAA4BC,GAAwB;AAAxB,SAAA,SAAAA,GAH5B,KAAQ,yBAAyB;AAI7B,UAAM,EAAE,QAAAC,GAAQ,OAAAC,EAAA,IAAU,KAAK,OAAO,KAAK;AACtC,SAAA,WAAW,IAAIC,EAAI,OAAO,WAAW,IAAI,aAAaF,IAASC,CAAK,GAAG;AAAA,MACxE;AAAA,MACA;AAAA,MACAD;AAAA,MACAC;AAAA,IAAA,CACH,GACD,KAAK,aAAa;AAAA,MACd,MAAM,KAAK,SAAS;AAAA,MACpB,mBAAmB,EAAE,MAAM,IAAI,aAAa,CAAC,GAAG,MAAM,GAAG;AAAA,MACzD,SAAS,IAAI,aAAa,CAAC;AAAA,MAC3B,QAAQ;AAAA,MACR,WAAW;AAAA,MACX,MAAM;AAAA,IACV;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYI,WAAWE,GAA0BC,GAAmC;AACtE,UAAA;AAAA,MACF,WAAWC;AAAA,MACX,WAAWC;AAAA,MACX,YAAYC;AAAA,IAAA,IACZ,KAAK,OAAO,MACV,EAAE,QAAAP,GAAQ,OAAAC,EAAA,IAAUK,GACpBE,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,SAAK,OAAO,YAAY,EAAE,MAAM,QAAQ,QAAQyC,GAAc;AAE1D,QAAAD;AAKG,WAJQ,IAAI,QAAc,CAACE,MAAY;AAC3B,MAAAF,IAAAE;AAAA,IAAA,CAClB;AAAA,EAEM;AAAA,EAGJ,WAAoB;AAChB,WAAA,CAAC,CAAC,KAAK;AAAA,EAAA;AAAA,EAGlB,MAAa,WAAWtC,GAA0BkB,GAAkC;AAG5E,QAAA,CAAC,KAAK;AACA,YAAA,IAAI,MAAM,qCAAqC;AAGzD,SAAK,OAAO;AAAA,MACR;AAAA,QACI,MAAM;AAAA,QACN,OAAOlB;AAAA,QACP,WAAAkB;AAAA,MACJ;AAAA,MACA,CAAClB,EAAM,MAAM;AAAA,IACjB;AAAA,EAAA;AAAA,EAGG,UAAgB;AACnB,IAAA0B,EAAsB,gBAAgB;AAAA,EAAA;AAE9C;"}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { PreloadConfig, ResolutionConfig, WorkerConfig } from './config-types';
|
|
2
|
-
import * as ort from 'onnxruntime-web';
|
|
3
|
-
/**
|
|
4
|
-
* Shared resource manager used to preload and share resources across different pipelines.
|
|
5
|
-
* This is useful when multiple pipelines are using the same model or when the model needs
|
|
6
|
-
* to be preloaded before the pipeline is created.
|
|
7
|
-
*/
|
|
8
|
-
declare class SharedResourceManager {
|
|
9
|
-
private modelSessions;
|
|
10
|
-
private worker?;
|
|
11
|
-
private workerUrl?;
|
|
12
|
-
private resolvedUriCache;
|
|
13
|
-
/**
|
|
14
|
-
* Initialize the worker using the provided configuration.
|
|
15
|
-
*
|
|
16
|
-
* @param config - The configuration object.
|
|
17
|
-
* @returns A promise that resolves when the worker is initialized.
|
|
18
|
-
*/
|
|
19
|
-
initializeWorker(config: WorkerConfig): Promise<Worker>;
|
|
20
|
-
/**
|
|
21
|
-
* Terminate the worker.
|
|
22
|
-
*/
|
|
23
|
-
terminateWorker(): void;
|
|
24
|
-
/**
|
|
25
|
-
* Load the model using the provided configuration. This gets called by the worker when using
|
|
26
|
-
* the worker generator.
|
|
27
|
-
*
|
|
28
|
-
* @param config - The configuration object.
|
|
29
|
-
* @returns A promise that resolves when the model is preloaded.
|
|
30
|
-
*/
|
|
31
|
-
loadModel(config: PreloadConfig): Promise<ort.InferenceSession>;
|
|
32
|
-
/**
|
|
33
|
-
* Preload the worker using the provided configuration.
|
|
34
|
-
*
|
|
35
|
-
* @param config - The configuration object.
|
|
36
|
-
* @returns A promise that resolves when the worker is preloaded.
|
|
37
|
-
*/
|
|
38
|
-
preloadWorker(config: PreloadConfig): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Warmup the model session with a dummy input to avoid latency when running the model for the first time.
|
|
41
|
-
*
|
|
42
|
-
* @param {ort.InferenceSession} session The model session.
|
|
43
|
-
* @param {ResolutionConfig} inputSize The input size of the model.
|
|
44
|
-
* @param {number} inputChannels The number of input channels (e.g., RGB is 3).
|
|
45
|
-
* @param {ort.Tensor.Type} inputType The input tensor type.
|
|
46
|
-
* @returns {Promise<void>} A promise that resolves when the model is warmed up.
|
|
47
|
-
* @example
|
|
48
|
-
* await warmupModel(session, inputSize, inputChannels, inputType);
|
|
49
|
-
*/
|
|
50
|
-
warmupModel(session: ort.InferenceSession, inputSize?: ResolutionConfig, inputChannels?: number, inputType?: ort.Tensor.Type): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Get the model session for the provided model URL.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} modelUrl The model URL.
|
|
55
|
-
* @returns {ort.InferenceSession} The model session.
|
|
56
|
-
* @example
|
|
57
|
-
* const session = getModelSession(modelUrl);
|
|
58
|
-
*/
|
|
59
|
-
getModelSession(modelUrl: string): ort.InferenceSession;
|
|
60
|
-
/**
|
|
61
|
-
* Resolves the full URL for a given asset. Non-file URIs are treated as paths
|
|
62
|
-
* and resolved using the `toAbsoluteURI` method.
|
|
63
|
-
*
|
|
64
|
-
* @param config - The preload configuration.
|
|
65
|
-
* @param assetUri - The asset URI from the config.
|
|
66
|
-
* @returns The resolved full URL.
|
|
67
|
-
*/
|
|
68
|
-
/**
|
|
69
|
-
* Resolves the full URL for a given asset. Non-file URIs are treated as paths and resolved
|
|
70
|
-
* using the `toAbsoluteURI` method. If the assetUri is already resolved, or if it's found in
|
|
71
|
-
* cache, it will be returned directly. If an assetUrlResolver function is provided in the
|
|
72
|
-
* configuration, it will be used to resolve the URL if the assetUri is not fully qualified,
|
|
73
|
-
* a blob URL, or in the cache.
|
|
74
|
-
*
|
|
75
|
-
* @param config - The preload configuration.
|
|
76
|
-
* @param assetUri - The asset URI from the config.
|
|
77
|
-
* @returns The resolved full URL.
|
|
78
|
-
*/
|
|
79
|
-
resolveUri(config: PreloadConfig, assetUri?: string): Promise<string>;
|
|
80
|
-
getWorker(): Worker | undefined;
|
|
81
|
-
getWorkerUrl(): string | undefined;
|
|
82
|
-
/**
|
|
83
|
-
* Prepare the worker configuration by resolving the final URLs and paths to send to the
|
|
84
|
-
* worker. This is necessary because we can't send functions to the worker. We also need to
|
|
85
|
-
* remove the assetUrlResolver function from the configuration.
|
|
86
|
-
*
|
|
87
|
-
* @param config - The preload configuration.
|
|
88
|
-
* @returns The worker configuration.
|
|
89
|
-
*/
|
|
90
|
-
prepareWorkerConfig(config: PreloadConfig): Promise<any>;
|
|
91
|
-
}
|
|
92
|
-
export declare const sharedResourceManager: SharedResourceManager;
|
|
93
|
-
export {};
|