@webex/ladon-ts 5.4.6 → 5.5.1

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/build/module/index.d.ts +3 -2
  3. package/build/module/index.js +35 -34
  4. package/build/module/index.js.map +1 -1
  5. package/build/module/lib/config-builder.d.ts +1 -1
  6. package/build/module/lib/config-builder.js +59 -58
  7. package/build/module/lib/config-builder.js.map +1 -1
  8. package/build/module/lib/config-types.d.ts +1 -1
  9. package/build/module/lib/config-types.js.map +1 -1
  10. package/build/module/lib/generator-types.d.ts +25 -0
  11. package/build/module/lib/generator.d.ts +2 -74
  12. package/build/module/lib/pipeline.js +17 -26
  13. package/build/module/lib/pipeline.js.map +1 -1
  14. package/build/module/lib/preload.js +4 -4
  15. package/build/module/lib/preload.js.map +1 -1
  16. package/build/module/lib/resource-loader.d.ts +22 -0
  17. package/build/module/lib/resource-loader.js +33 -0
  18. package/build/module/lib/resource-loader.js.map +1 -0
  19. package/build/module/lib/util.d.ts +1 -2
  20. package/build/module/lib/util.js +63 -82
  21. package/build/module/lib/util.js.map +1 -1
  22. package/build/module/lib/version.d.ts +1 -1
  23. package/build/module/lib/version.js +1 -1
  24. package/build/module/lib/version.js.map +1 -1
  25. package/build/module/lib/worker-generator.d.ts +15 -0
  26. package/build/module/lib/worker-generator.js +93 -0
  27. package/build/module/lib/worker-generator.js.map +1 -0
  28. package/build/module/lib/worker-manager.d.ts +41 -0
  29. package/build/module/lib/worker-manager.js +63 -0
  30. package/build/module/lib/worker-manager.js.map +1 -0
  31. package/build/worker/segmentation.js +791 -791
  32. package/build/worker/segmentation.js.map +1 -1
  33. package/package.json +1 -4
  34. package/build/module/lib/generator.js +0 -224
  35. package/build/module/lib/generator.js.map +0 -1
  36. package/build/module/lib/shared-resource-manager.d.ts +0 -93
  37. package/build/module/lib/shared-resource-manager.js +0 -166
  38. package/build/module/lib/shared-resource-manager.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-generator.js","sources":["../../../src/lib/worker-generator.ts"],"sourcesContent":["import { PipelineConfig } from './config-types';\nimport { LadonOutput, MaskGenerator } from './generator-types';\nimport { workerManager } from './worker-manager';\n\nexport class WorkerGenerator implements MaskGenerator {\n private lastMask: Float32Array;\n private lastResult: LadonOutput;\n private newMask = true;\n private worker?: Worker;\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 workerManager.prepareWorkerConfig(this.config);\n\n this.worker = workerManager.getWorker();\n this.worker ??= await workerManager.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 = workerManager.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 workerManager.terminateWorker();\n }\n}\n"],"names":["WorkerGenerator","config","_","toReturn","workerManager","event","message","severity","msg","workerUrl","resolutionCb","workerConfig","resolve","input","timestamp"],"mappings":";AAIO,MAAMA,EAAyC;AAAA,EAMlD,YAA4BC,GAAwB;AAAxB,SAAA,SAAAA,GAH5B,KAAQ,UAAU,IAId,KAAK,WAAW,IAAI;AAAA,MAChB,KAAK,OAAO,KAAK,WAAW,SAAS,KAAK,OAAO,KAAK,WAAW;AAAA,IACrE,GACA,KAAK,aAAa;AAAA,MACd,MAAM,KAAK;AAAA,MACX,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,EAIJ,MAAa,eAAeC,GAA4C;AACpE,UAAMC,IAAW;AAAA,MACb,MA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AAO;AAAA,MAChB,OAAOA,EAAO;AAAA,MACd,MAAMA,EAAO;AAAA,MACb,SAASA,EAAO;AAAA,MAChB,QAAQ,EAAE,MAAM,cAAc;AAAA,IAClC;AAEA,SAAK,OAAO,YAAY,EAAE,MAAM,QAAQ,QAAQU,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,WAAWC,GAA0BC,GAAkC;AAG5E,QAAA,CAAC,KAAK;AACA,YAAA,IAAI,MAAM,qCAAqC;AAGzD,SAAK,OAAO;AAAA,MACR;AAAA,QACI,MAAM;AAAA,QACN,OAAOD;AAAA,QACP,WAAAC;AAAA,MACJ;AAAA,MACA,CAACD,EAAM,MAAM;AAAA,IACjB;AAAA,EAAA;AAAA,EAGG,UAAgB;AACnB,IAAAT,EAAc,gBAAgB;AAAA,EAAA;AAEtC;"}
@@ -0,0 +1,41 @@
1
+ import { PreloadConfig, WorkerConfig } from './config-types';
2
+ /**
3
+ * Worker manager class to handle the initialization and termination of the segmenation
4
+ * web worker. This class is responsible for loading the worker script, resolving the
5
+ * configuration, and managing the worker lifecycle. It also provides methods to
6
+ * preload the worker and terminate it when no longer needed.
7
+ */
8
+ export declare class WorkerManager {
9
+ private worker?;
10
+ private workerUrl?;
11
+ /**
12
+ * Initialize the worker using the provided configuration.
13
+ *
14
+ * @param config - The configuration object.
15
+ * @returns A promise that resolves when the worker is initialized.
16
+ */
17
+ initializeWorker(config: WorkerConfig): Promise<Worker>;
18
+ /**
19
+ * Terminate the worker.
20
+ */
21
+ terminateWorker(): void;
22
+ /**
23
+ * Preload the worker using the provided configuration.
24
+ *
25
+ * @param config - The configuration object.
26
+ * @returns A promise that resolves when the worker is preloaded.
27
+ */
28
+ preloadWorker(config: PreloadConfig): Promise<void>;
29
+ getWorker(): Worker | undefined;
30
+ getWorkerUrl(): string | undefined;
31
+ /**
32
+ * Prepare the worker configuration by resolving the final URLs and paths to send to the
33
+ * worker. This is necessary because we can't send functions to the worker. We also need to
34
+ * remove the assetUrlResolver function from the configuration.
35
+ *
36
+ * @param config - The preload configuration.
37
+ * @returns The worker configuration.
38
+ */
39
+ prepareWorkerConfig(config: PreloadConfig): Promise<any>;
40
+ }
41
+ export declare const workerManager: WorkerManager;
@@ -0,0 +1,63 @@
1
+ import { resourceLoader as o } from "./resource-loader.js";
2
+ class i {
3
+ /**
4
+ * Initialize the worker using the provided configuration.
5
+ *
6
+ * @param config - The configuration object.
7
+ * @returns A promise that resolves when the worker is initialized.
8
+ */
9
+ async initializeWorker(r) {
10
+ if (typeof Worker > "u")
11
+ throw new Error("[ladon-ts] web workers are not supported in this environment");
12
+ const e = await o.resolveUri(r, r.mask.workerUri), s = await (await fetch(e, { cache: "force-cache" })).text();
13
+ return this.workerUrl = URL.createObjectURL(new Blob([s], { type: "text/javascript" })), new Worker(this.workerUrl, { type: "module" });
14
+ }
15
+ /**
16
+ * Terminate the worker.
17
+ */
18
+ terminateWorker() {
19
+ this.worker && (this.worker.terminate(), this.worker = void 0);
20
+ }
21
+ /**
22
+ * Preload the worker using the provided configuration.
23
+ *
24
+ * @param config - The configuration object.
25
+ * @returns A promise that resolves when the worker is preloaded.
26
+ */
27
+ async preloadWorker(r) {
28
+ const e = await this.prepareWorkerConfig(r);
29
+ this.worker = await this.initializeWorker(e), this.worker.postMessage({ type: "preload", config: e });
30
+ }
31
+ getWorker() {
32
+ return this.worker;
33
+ }
34
+ getWorkerUrl() {
35
+ return this.workerUrl;
36
+ }
37
+ /**
38
+ * Prepare the worker configuration by resolving the final URLs and paths to send to the
39
+ * worker. This is necessary because we can't send functions to the worker. We also need to
40
+ * remove the assetUrlResolver function from the configuration.
41
+ *
42
+ * @param config - The preload configuration.
43
+ * @returns The worker configuration.
44
+ */
45
+ async prepareWorkerConfig(r) {
46
+ const t = { ...{
47
+ ...r,
48
+ mask: {
49
+ ...r.mask,
50
+ modelUri: await o.resolveUri(r, r.mask.modelUri),
51
+ workerUri: await o.resolveUri(r, r.mask.workerUri)
52
+ },
53
+ wasmUri: await o.resolveUri(r, r.wasmUri)
54
+ }, assetUrlResolver: void 0 };
55
+ return JSON.parse(JSON.stringify(t));
56
+ }
57
+ }
58
+ const k = new i();
59
+ export {
60
+ i as WorkerManager,
61
+ k as workerManager
62
+ };
63
+ //# sourceMappingURL=worker-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-manager.js","sources":["../../../src/lib/worker-manager.ts"],"sourcesContent":["import { PreloadConfig, WorkerConfig } from './config-types';\nimport { resourceLoader } from './resource-loader';\n\n/**\n * Worker manager class to handle the initialization and termination of the segmenation\n * web worker. This class is responsible for loading the worker script, resolving the\n * configuration, and managing the worker lifecycle. It also provides methods to\n * preload the worker and terminate it when no longer needed.\n */\nexport class WorkerManager {\n private worker?: Worker;\n private workerUrl?: string;\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 public async initializeWorker(config: WorkerConfig): Promise<Worker> {\n if (typeof Worker === 'undefined') {\n throw new Error('[ladon-ts] web workers are not supported in this environment');\n }\n\n const scriptUri = await resourceLoader.resolveUri(config, config.mask.workerUri);\n const response = await fetch(scriptUri, { cache: 'force-cache' });\n const workerSource = await response.text();\n\n this.workerUrl = URL.createObjectURL(new Blob([workerSource], { type: 'text/javascript' }));\n\n return new Worker(this.workerUrl, { type: 'module' });\n }\n\n /**\n * Terminate the worker.\n */\n public terminateWorker() {\n if (this.worker) {\n this.worker.terminate();\n this.worker = undefined;\n }\n }\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 public async preloadWorker(config: PreloadConfig) {\n const workerConfig = await this.prepareWorkerConfig(config);\n\n this.worker = await this.initializeWorker(workerConfig);\n this.worker.postMessage({ type: 'preload', config: workerConfig });\n }\n\n public getWorker() {\n return this.worker;\n }\n\n public getWorkerUrl() {\n return this.workerUrl;\n }\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 public async prepareWorkerConfig(config: PreloadConfig) {\n // Resolve and cache the URIs before sending the config to the worker. Future calls to the\n // resolveUri method will use the cache.\n const resolvedConfig: PreloadConfig = {\n ...config,\n mask: {\n ...config.mask,\n modelUri: await resourceLoader.resolveUri(config, config.mask.modelUri),\n workerUri: await resourceLoader.resolveUri(config, config.mask.workerUri),\n },\n wasmUri: await resourceLoader.resolveUri(config, config.wasmUri),\n };\n\n // We can't send the assetUrlResolver function to the worker so we remove it from the config.\n const workerConfig = { ...resolvedConfig, assetUrlResolver: undefined };\n\n // This is a workaround to ensure the worker configuration can be sent to the worker.\n return JSON.parse(JSON.stringify(workerConfig));\n }\n}\n\n// Global instance of the shared resource manager.\nexport const workerManager = new WorkerManager();\n"],"names":["WorkerManager","config","scriptUri","resourceLoader","workerSource","workerConfig","workerManager"],"mappings":";AASO,MAAMA,EAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUvB,MAAa,iBAAiBC,GAAuC;AAC7D,QAAA,OAAO,SAAW;AACZ,YAAA,IAAI,MAAM,8DAA8D;AAGlF,UAAMC,IAAY,MAAMC,EAAe,WAAWF,GAAQA,EAAO,KAAK,SAAS,GAEzEG,IAAe,OADJ,MAAM,MAAMF,GAAW,EAAE,OAAO,eAAe,GAC5B,KAAK;AAEzC,gBAAK,YAAY,IAAI,gBAAgB,IAAI,KAAK,CAACE,CAAY,GAAG,EAAE,MAAM,kBAAmB,CAAA,CAAC,GAEnF,IAAI,OAAO,KAAK,WAAW,EAAE,MAAM,UAAU;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMjD,kBAA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