@webex/ladon-ts 5.7.1 → 5.9.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 +14 -0
- package/build/module/index.d.ts +1 -0
- package/build/module/lib/config-builder.d.ts +2 -2
- package/build/module/lib/config-builder.js +1 -1
- package/build/module/lib/config-builder.js.map +1 -1
- package/build/module/lib/config-types.d.ts +2 -1
- package/build/module/lib/config-types.js.map +1 -1
- package/build/module/lib/generator-types.d.ts +1 -1
- package/build/module/lib/pipeline-types.d.ts +34 -0
- package/build/module/lib/pipeline.d.ts +72 -40
- package/build/module/lib/pipeline.js +111 -52
- package/build/module/lib/pipeline.js.map +1 -1
- package/build/module/lib/renderer/replacement-renderer.d.ts +67 -5
- package/build/module/lib/renderer/replacement-renderer.js +99 -49
- package/build/module/lib/renderer/replacement-renderer.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 +1 -1
- package/build/module/lib/worker-generator.js +12 -12
- package/build/module/lib/worker-generator.js.map +1 -1
- package/build/worker/segmentation.js +3 -3
- package/build/worker/segmentation.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## 5.9.0 (2025-07-01)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* upload background directly to GPU texture ([#74](https://github.com/beyonddunbar/ladon-ts/issues/74)) ([a05b494](https://github.com/beyonddunbar/ladon-ts/commit/a05b4947ae024a430b6006a4a468b5107fd15fb5))
|
|
11
|
+
|
|
12
|
+
## 5.8.0 (2025-07-01)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* add inference stats to frame result ([#73](https://github.com/beyonddunbar/ladon-ts/issues/73)) ([5b02f51](https://github.com/beyonddunbar/ladon-ts/commit/5b02f51e92fe5dc305e5fa3ca9036fb718a2a1f7))
|
|
18
|
+
|
|
5
19
|
### 5.7.1 (2025-06-17)
|
|
6
20
|
|
|
7
21
|
|
package/build/module/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './lib/config-builder';
|
|
|
3
3
|
export * from './lib/config-types';
|
|
4
4
|
export * from './lib/generator';
|
|
5
5
|
export * from './lib/pipeline';
|
|
6
|
+
export * from './lib/pipeline-types';
|
|
6
7
|
export * from './lib/preload';
|
|
7
8
|
export * from './lib/renderer';
|
|
8
9
|
export * from './lib/resource-loader';
|
|
@@ -71,9 +71,9 @@ export declare class ConfigurationBuilder {
|
|
|
71
71
|
setBlurSigma(sigma: number): ConfigurationBuilder;
|
|
72
72
|
/**
|
|
73
73
|
* Sets the replacement background.
|
|
74
|
-
* @param background The background to use for replacement.
|
|
74
|
+
* @param background The background canvas or video element to use for replacement.
|
|
75
75
|
*/
|
|
76
|
-
setReplacementBackground(background: HTMLCanvasElement): ConfigurationBuilder;
|
|
76
|
+
setReplacementBackground(background: HTMLCanvasElement | HTMLVideoElement): ConfigurationBuilder;
|
|
77
77
|
/**
|
|
78
78
|
* Sets the replacement static property.
|
|
79
79
|
* @param isStatic Boolean indicating if the replacement is static.
|
|
@@ -104,7 +104,7 @@ class a {
|
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* Sets the replacement background.
|
|
107
|
-
* @param background The background to use for replacement.
|
|
107
|
+
* @param background The background canvas or video element to use for replacement.
|
|
108
108
|
*/
|
|
109
109
|
setReplacementBackground(e) {
|
|
110
110
|
if (!this.config.render || this.config.render.type !== "replacement")
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config-builder.js","sources":["../../../src/lib/config-builder.ts"],"sourcesContent":["import rawConfig from '../config.json';\n\nimport {\n AssetUrlResolver,\n BlurConfig,\n BlurStrength,\n Generator,\n InputConfig,\n MaskConfig,\n PassthroughConfig,\n PipelineConfig,\n PreloadConfig,\n Quality,\n RawConfig,\n RenderConfig,\n} from './config-types';\nimport { LIB_VERSION } from './version';\n\n/**\n * Internal mutable versions of the configurations used within the builder.\n */\ninterface MutableSizeConfig {\n height: number;\n width: number;\n}\n\ninterface MutableRenderConfig {\n type: 'blur' | 'passthrough' | 'replacement';\n horizontalMirror?: boolean;\n}\n\ninterface MutableBlurConfig extends MutableRenderConfig {\n type: 'blur';\n kernelSize: number;\n sigma: number;\n}\n\ninterface MutableReplacementConfig extends MutableRenderConfig {\n type: 'replacement';\n background: HTMLCanvasElement;\n static: boolean;\n // TODO: add reposition args for presentation mode\n}\n\ninterface MutableBaseMaskConfig {\n generator: Generator;\n generationIntervalMs: number;\n modelRank: number;\n modelUri: string;\n workerUri: string;\n inputSize?: MutableSizeConfig;\n outputSize?: MutableSizeConfig;\n upscaleSize?: MutableSizeConfig;\n}\n\ninterface MutableMaskConfig extends MutableBaseMaskConfig {\n inputSize: MutableSizeConfig;\n outputSize: MutableSizeConfig;\n upscaleSize: MutableSizeConfig;\n qualityKey?: keyof RawConfig['modelQualities'];\n}\n\ninterface MutablePipelineConfig {\n baseUrl?: string;\n wasmUri?: string;\n executionProviders?: string[];\n render?: MutableBlurConfig | MutableRenderConfig | MutableReplacementConfig;\n input?: MutableSizeConfig;\n mask?: MutableMaskConfig;\n assetUrlResolver?: AssetUrlResolver;\n}\n\n/**\n * A builder class for creating PipelineConfig instances in a flexible and intuitive way.\n */\nexport class ConfigurationBuilder {\n private config: MutablePipelineConfig = {};\n\n constructor(private env: 'int' | 'prod' = 'prod') {}\n\n /**\n * Sets the base URL for the configuration.\n * @param baseUrl The base URL to use.\n */\n public setBaseUrl(baseUrl: string): ConfigurationBuilder {\n // Remove trailing slashes from the base URL\n this.config.baseUrl = baseUrl.replace(/\\/+$/, '');\n return this;\n }\n\n /**\n * Sets the WASM URI for the configuration.\n * @param wasmUri The WASM URI to use.\n */\n public setWasmUri(wasmUri: string): ConfigurationBuilder {\n this.config.wasmUri = wasmUri;\n return this;\n }\n\n /**\n * Sets the asset URL resolver.\n * @param assetUrlResolver The asset URL resolver to use\n * @returns The current ConfigurationBuilder instance.\n */\n public setAssetUrlResolver(assetUrlResolver: AssetUrlResolver): ConfigurationBuilder {\n this.config.assetUrlResolver = assetUrlResolver;\n return this;\n }\n\n /**\n * Sets the execution providers for the ONNX runtime.\n *\n * @param executionProviders The execution providers to use.\n */\n public setExecutionProviders(executionProviders: string[]): ConfigurationBuilder {\n // Allow a single string to be passed as well. This is useful for setting a single provider.\n // This will convert the string to an array for consistency.\n if (typeof executionProviders === 'string') {\n executionProviders = [executionProviders];\n }\n this.config.executionProviders = executionProviders;\n return this;\n }\n\n /**\n * Sets the input size for the configuration.\n * @param height The height of the input.\n * @param width The width of the input.\n */\n public setInputSize(height: number, width: number): ConfigurationBuilder {\n this.config.input = { height, width };\n return this;\n }\n\n /**\n * Sets the input configuration.\n * @param inputConfig The input configuration to use.\n */\n public setInputConfig(inputConfig: InputConfig): ConfigurationBuilder {\n this.config.input = { ...inputConfig };\n return this;\n }\n\n /**\n * Sets the render configuration.\n * @param renderConfig The render configuration to use.\n */\n public setRenderConfig(renderConfig: RenderConfig): ConfigurationBuilder {\n this.config.render = { ...renderConfig } as MutableRenderConfig;\n return this;\n }\n\n /**\n * Sets the render type.\n * @param type The render type ('blur', 'passthrough', 'replacement').\n */\n public setRenderType(type: 'blur' | 'passthrough' | 'replacement'): ConfigurationBuilder {\n if (!this.config.render) {\n this.config.render = { type };\n } else {\n this.config.render.type = type;\n }\n return this;\n }\n\n /**\n * Sets the horizontal mirror property for the render configuration.\n * @param horizontalMirror Boolean indicating if horizontal mirror should be applied.\n */\n public setRenderHorizontalMirror(horizontalMirror: boolean): ConfigurationBuilder {\n if (!this.config.render) {\n throw new Error('Render config must be set before setting horizontal mirror.');\n }\n this.config.render.horizontalMirror = horizontalMirror;\n return this;\n }\n\n /**\n * Sets the blur strength for the render configuration.\n * @param blurStrength The blur strength to use.\n */\n public setBlurStrength(blurStrength: BlurStrength): ConfigurationBuilder {\n const blurConfig = ConfigurationBuilder.getBlurConfig(\n blurStrength,\n this.config.render?.horizontalMirror\n );\n this.config.render = { ...this.config.render, ...blurConfig };\n return this;\n }\n\n /**\n * Sets the blur kernel size.\n * @param kernelSize The kernel size for the blur effect.\n */\n public setBlurKernelSize(kernelSize: number): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'blur') {\n throw new Error('Render config must be of type blur to set blur properties.');\n }\n (this.config.render as MutableBlurConfig).kernelSize = kernelSize;\n return this;\n }\n\n /**\n * Sets the blur sigma value.\n * @param sigma The sigma value for the blur effect.\n */\n public setBlurSigma(sigma: number): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'blur') {\n throw new Error('Render config must be of type blur to set blur properties.');\n }\n (this.config.render as MutableBlurConfig).sigma = sigma;\n return this;\n }\n\n /**\n * Sets the replacement background.\n * @param background The background to use for replacement.\n */\n public setReplacementBackground(background: HTMLCanvasElement): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'replacement') {\n throw new Error(\n 'Render config must be of type replacement to set replacement properties.'\n );\n }\n (this.config.render as MutableReplacementConfig).background = background;\n return this;\n }\n\n /**\n * Sets the replacement static property.\n * @param isStatic Boolean indicating if the replacement is static.\n */\n public setReplacementStatic(isStatic: boolean): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'replacement') {\n throw new Error(\n 'Render config must be of type replacement to set replacement properties.'\n );\n }\n (this.config.render as MutableReplacementConfig).static = isStatic;\n return this;\n }\n\n /**\n * Sets the mask configuration.\n * @param maskConfig The mask configuration to use.\n */\n public setMaskConfig(maskConfig: MaskConfig): ConfigurationBuilder {\n this.config.mask = { ...maskConfig } as MutableMaskConfig;\n return this;\n }\n\n /**\n * Sets the mask generator.\n * @param generator The generator type (most be 'worker'; 'local' is deprecated).\n */\n public setMaskGenerator(generator: Generator): ConfigurationBuilder {\n if (generator !== 'worker') {\n console.warn('[ladon-ts] Mask generator must be \"worker\". \"local\" is deprecated.');\n }\n const mask = this.getMaskConfig();\n mask.generator = generator;\n return this;\n }\n\n /**\n * Sets the mask generation interval in milliseconds.\n * @param intervalMs The generation interval in milliseconds.\n */\n public setMaskGenerationIntervalMs(intervalMs: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.generationIntervalMs = intervalMs;\n return this;\n }\n\n /**\n * Sets the mask model rank.\n * @param modelRank The model rank to use.\n */\n public setMaskModelRank(modelRank: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.modelRank = modelRank;\n return this;\n }\n\n /**\n * Sets the mask model URI.\n * @param modelUri The model URI to use.\n */\n public setMaskModelUri(modelUri: string): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.modelUri = modelUri;\n return this;\n }\n\n /**\n * Sets the mask worker URI.\n * @param workerUri The worker URI to use.\n */\n public setMaskWorkerUri(workerUri: string): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.workerUri = workerUri;\n return this;\n }\n\n /**\n * Sets the mask input size.\n * @param height The height of the mask input.\n * @param width The width of the mask input.\n */\n public setMaskInputSize(height: number, width: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.inputSize = { height, width };\n return this;\n }\n\n /**\n * Sets the mask output size.\n * @param height The height of the mask output.\n * @param width The width of the mask output.\n */\n public setMaskOutputSize(height: number, width: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.outputSize = { height, width };\n return this;\n }\n\n /**\n * Sets the mask upscaled size.\n * @param height The height of the upscaled mask.\n * @param width The width of the upscaled mask.\n */\n public setMaskUpscaleSize(height: number, width: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.upscaleSize = { height, width };\n return this;\n }\n\n /**\n * Sets the quality level and updates mask configurations accordingly.\n * @param quality The desired quality level.\n */\n public setMaskQuality(quality: Quality): ConfigurationBuilder {\n const { modelQualities } = rawConfig;\n\n const mask = this.getMaskConfig();\n const qualityKey = quality.toLowerCase() as keyof RawConfig['modelQualities'];\n const qualityConfig = modelQualities[qualityKey];\n\n if (!qualityConfig) {\n throw new Error(`[ladon-ts] unknown quality option: ${quality}`);\n }\n\n // Store the quality key for dynamic resolution later\n mask.qualityKey = qualityKey;\n\n // Copy other quality-specific properties into the mask config\n Object.assign(mask, qualityConfig);\n\n return this;\n }\n\n /**\n * Builds and returns the PreloadConfig or PipelineConfig object.\n *\n * @param requireInputConfig Whether to require the input configuration to be set before building.\n * Input size is not available at preload so this needs to be optional. The default value is true.\n *\n */\n public build(requireInputConfig = true): PipelineConfig | PreloadConfig {\n const baseUrl = this.resolveBaseUrl();\n const modelUri = this.resolveModelUri();\n const wasmUri = this.resolveWasmUri();\n const workerUri = this.resolveWorkerUri();\n\n const input = this.config.input;\n const render = this.config.render;\n const maskConfig = this.config.mask;\n const executionProviders = this.config.executionProviders;\n const assetUrlResolver = this.config.assetUrlResolver;\n\n if (!maskConfig) {\n throw new Error('Mask configuration must be set before building the configuration.');\n }\n\n const mask: MaskConfig = {\n generator: maskConfig.generator,\n generationIntervalMs: maskConfig.generationIntervalMs,\n modelRank: maskConfig.modelRank,\n inputSize: maskConfig.inputSize,\n outputSize: maskConfig.outputSize,\n upscaleSize: maskConfig.upscaleSize,\n modelUri,\n workerUri,\n };\n\n // Build the preload configuration, which is valid for both preload and pipeline.\n const preloadConfig = {\n assetUrlResolver,\n executionProviders,\n baseUrl,\n wasmUri,\n mask,\n };\n\n if (requireInputConfig) {\n if (!input) {\n throw new Error('Input must be set before building the configuration.');\n }\n\n if (!render) {\n throw new Error('Render must be set before building the configuration.');\n }\n\n // Build the full PipelineConfig\n const pipelineConfig = {\n ...preloadConfig,\n input,\n render,\n } as PipelineConfig;\n\n return pipelineConfig;\n }\n\n return preloadConfig as PreloadConfig;\n }\n\n /**\n * Returns the mask configuration, initializing it if necessary.\n */\n private getMaskConfig(): MutableMaskConfig {\n if (!this.config.mask) {\n this.config.mask = {} as MutableMaskConfig;\n }\n return this.config.mask;\n }\n\n /**\n * Returns the base URL for the configuration.\n */\n public resolveBaseUrl(): string {\n // TODO: Do we want to publish the base URL in the raw config? It seems like the models\n // auth service should be responsible for this, or the user should provide the base url.\n return this.config.baseUrl || rawConfig.baseUrls[this.env];\n }\n\n /**\n * Returns the base URL for the configuration.\n *\n * @returns The base URL for the configuration.\n */\n public resolveModelUri(): string {\n // First check if the property is explicitly set in the config, then fallback to rawConfig\n if (this.config.mask?.modelUri) {\n return this.config.mask.modelUri;\n }\n\n const mask = this.getMaskConfig();\n if (!mask.qualityKey) {\n throw new Error('[ladon-ts] No quality set, cannot infer modelFile.');\n }\n\n return this.resolveTemplate(rawConfig.modelUrlTemplate, {\n BASE_PATH_MODELS: rawConfig.basePaths.models,\n MODEL_VERSION: rawConfig.modelVersion,\n MODEL_FILE: rawConfig.modelQualities[mask.qualityKey].modelFile,\n });\n }\n\n public resolveWasmUri(): string {\n // First check if the property is explicitly set in the config, then fallback to rawConfig\n if (this.config.wasmUri) {\n return this.config.wasmUri;\n }\n\n return this.resolveTemplate(rawConfig.wasmPathTemplate, {\n BASE_PATH_WASM: rawConfig.basePaths.wasm,\n ONNX_VERSION: rawConfig.onnxVersion,\n });\n }\n\n public resolveWorkerUri(): string {\n // First check if the property is explicitly set in the config, then fallback to rawConfig\n if (this.config.mask?.workerUri) {\n return this.config.mask.workerUri;\n }\n\n return this.resolveTemplate(rawConfig.workerUrlTemplate, {\n BASE_PATH_WORKERS: rawConfig.basePaths.workers,\n LIB_VERSION: ConfigurationBuilder.getLibVersion(),\n WORKER_FILE: rawConfig.workerFile,\n });\n }\n\n /**\n * Resolves a template string with the specified values.\n *\n * @param template The template string to resolve.\n * @param values The values to use for resolution.\n * @returns The resolved template string.\n */\n private resolveTemplate(template: string, values: Record<string, string>): string {\n return template.replace(/\\$\\{([A-Z_]+)\\}/g, (_, key) => {\n if (!values[key]) {\n throw new Error(`Missing value for template variable: ${key}`);\n }\n return values[key];\n });\n }\n\n /**\n * Generates a blur configuration based on the specified blur strength.\n * @param blurStrength The desired blur strength.\n * @param horizontalMirror Whether to apply horizontal mirroring.\n */\n public static getBlurConfig(\n blurStrength: BlurStrength,\n horizontalMirror?: boolean\n ): BlurConfig {\n const strengthKey = blurStrength.toLowerCase() as keyof RawConfig['blurStrengths'];\n const blurConfig = rawConfig.blurStrengths[strengthKey];\n\n if (!blurConfig) {\n throw new Error(`[ladon-ts] Unknown blur strength: ${blurStrength}`);\n }\n\n return {\n type: 'blur',\n horizontalMirror,\n ...blurConfig,\n };\n }\n\n public static getPassthroughConfig(horizontalMirror?: boolean): PassthroughConfig {\n return {\n type: 'passthrough',\n horizontalMirror,\n };\n }\n\n /**\n * Returns the library version.\n *\n * @returns The library version.\n */\n public static getLibVersion(): string {\n return LIB_VERSION;\n }\n}\n"],"names":["ConfigurationBuilder","env","baseUrl","wasmUri","assetUrlResolver","executionProviders","height","width","inputConfig","renderConfig","type","horizontalMirror","blurStrength","blurConfig","_a","kernelSize","sigma","background","isStatic","maskConfig","generator","mask","intervalMs","modelRank","modelUri","workerUri","quality","modelQualities","rawConfig","qualityKey","qualityConfig","requireInputConfig","input","render","preloadConfig","template","values","_","key","strengthKey","LIB_VERSION"],"mappings":";;AA2EO,MAAMA,EAAqB;AAAA,EAG9B,YAAoBC,IAAsB,QAAQ;AAA9B,SAAA,MAAAA,GAFpB,KAAQ,SAAgC,CAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQlC,WAAWC,GAAuC;AAErD,gBAAK,OAAO,UAAUA,EAAQ,QAAQ,QAAQ,EAAE,GACzC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOJ,WAAWC,GAAuC;AACrD,gBAAK,OAAO,UAAUA,GACf;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQJ,oBAAoBC,GAA0D;AACjF,gBAAK,OAAO,mB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AE;AAGhE,WAAA;AAAA,MACH,MAAM;AAAA,MACN,kBAAAD;AAAA,MACA,GAAGE;AAAA,IACP;AAAA,EAAA;AAAA,EAGJ,OAAc,qBAAqBF,GAA+C;AACvE,WAAA;AAAA,MACH,MAAM;AAAA,MACN,kBAAAA;AAAA,IACJ;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQJ,OAAc,gBAAwB;AAC3B,WAAA6B;AAAA,EAAA;AAEf;"}
|
|
1
|
+
{"version":3,"file":"config-builder.js","sources":["../../../src/lib/config-builder.ts"],"sourcesContent":["import rawConfig from '../config.json';\n\nimport {\n AssetUrlResolver,\n BlurConfig,\n BlurStrength,\n Generator,\n InputConfig,\n MaskConfig,\n PassthroughConfig,\n PipelineConfig,\n PreloadConfig,\n Quality,\n RawConfig,\n RenderConfig,\n} from './config-types';\nimport { LIB_VERSION } from './version';\n\n/**\n * Internal mutable versions of the configurations used within the builder.\n */\ninterface MutableSizeConfig {\n height: number;\n width: number;\n}\n\ninterface MutableRenderConfig {\n type: 'blur' | 'passthrough' | 'replacement';\n horizontalMirror?: boolean;\n}\n\ninterface MutableBlurConfig extends MutableRenderConfig {\n type: 'blur';\n kernelSize: number;\n sigma: number;\n}\n\ninterface MutableReplacementConfig extends MutableRenderConfig {\n type: 'replacement';\n background: HTMLCanvasElement | HTMLVideoElement;\n static: boolean;\n}\ninterface MutableBaseMaskConfig {\n generator: Generator;\n generationIntervalMs: number;\n modelRank: number;\n modelUri: string;\n workerUri: string;\n inputSize?: MutableSizeConfig;\n outputSize?: MutableSizeConfig;\n upscaleSize?: MutableSizeConfig;\n}\n\ninterface MutableMaskConfig extends MutableBaseMaskConfig {\n inputSize: MutableSizeConfig;\n outputSize: MutableSizeConfig;\n upscaleSize: MutableSizeConfig;\n qualityKey?: keyof RawConfig['modelQualities'];\n}\n\ninterface MutablePipelineConfig {\n baseUrl?: string;\n wasmUri?: string;\n executionProviders?: string[];\n render?: MutableBlurConfig | MutableRenderConfig | MutableReplacementConfig;\n input?: MutableSizeConfig;\n mask?: MutableMaskConfig;\n assetUrlResolver?: AssetUrlResolver;\n}\n\n/**\n * A builder class for creating PipelineConfig instances in a flexible and intuitive way.\n */\nexport class ConfigurationBuilder {\n private config: MutablePipelineConfig = {};\n\n constructor(private env: 'int' | 'prod' = 'prod') {}\n\n /**\n * Sets the base URL for the configuration.\n * @param baseUrl The base URL to use.\n */\n public setBaseUrl(baseUrl: string): ConfigurationBuilder {\n // Remove trailing slashes from the base URL\n this.config.baseUrl = baseUrl.replace(/\\/+$/, '');\n return this;\n }\n\n /**\n * Sets the WASM URI for the configuration.\n * @param wasmUri The WASM URI to use.\n */\n public setWasmUri(wasmUri: string): ConfigurationBuilder {\n this.config.wasmUri = wasmUri;\n return this;\n }\n\n /**\n * Sets the asset URL resolver.\n * @param assetUrlResolver The asset URL resolver to use\n * @returns The current ConfigurationBuilder instance.\n */\n public setAssetUrlResolver(assetUrlResolver: AssetUrlResolver): ConfigurationBuilder {\n this.config.assetUrlResolver = assetUrlResolver;\n return this;\n }\n\n /**\n * Sets the execution providers for the ONNX runtime.\n *\n * @param executionProviders The execution providers to use.\n */\n public setExecutionProviders(executionProviders: string[]): ConfigurationBuilder {\n // Allow a single string to be passed as well. This is useful for setting a single provider.\n // This will convert the string to an array for consistency.\n if (typeof executionProviders === 'string') {\n executionProviders = [executionProviders];\n }\n this.config.executionProviders = executionProviders;\n return this;\n }\n\n /**\n * Sets the input size for the configuration.\n * @param height The height of the input.\n * @param width The width of the input.\n */\n public setInputSize(height: number, width: number): ConfigurationBuilder {\n this.config.input = { height, width };\n return this;\n }\n\n /**\n * Sets the input configuration.\n * @param inputConfig The input configuration to use.\n */\n public setInputConfig(inputConfig: InputConfig): ConfigurationBuilder {\n this.config.input = { ...inputConfig };\n return this;\n }\n\n /**\n * Sets the render configuration.\n * @param renderConfig The render configuration to use.\n */\n public setRenderConfig(renderConfig: RenderConfig): ConfigurationBuilder {\n this.config.render = { ...renderConfig } as MutableRenderConfig;\n return this;\n }\n\n /**\n * Sets the render type.\n * @param type The render type ('blur', 'passthrough', 'replacement').\n */\n public setRenderType(type: 'blur' | 'passthrough' | 'replacement'): ConfigurationBuilder {\n if (!this.config.render) {\n this.config.render = { type };\n } else {\n this.config.render.type = type;\n }\n return this;\n }\n\n /**\n * Sets the horizontal mirror property for the render configuration.\n * @param horizontalMirror Boolean indicating if horizontal mirror should be applied.\n */\n public setRenderHorizontalMirror(horizontalMirror: boolean): ConfigurationBuilder {\n if (!this.config.render) {\n throw new Error('Render config must be set before setting horizontal mirror.');\n }\n this.config.render.horizontalMirror = horizontalMirror;\n return this;\n }\n\n /**\n * Sets the blur strength for the render configuration.\n * @param blurStrength The blur strength to use.\n */\n public setBlurStrength(blurStrength: BlurStrength): ConfigurationBuilder {\n const blurConfig = ConfigurationBuilder.getBlurConfig(\n blurStrength,\n this.config.render?.horizontalMirror\n );\n this.config.render = { ...this.config.render, ...blurConfig };\n return this;\n }\n\n /**\n * Sets the blur kernel size.\n * @param kernelSize The kernel size for the blur effect.\n */\n public setBlurKernelSize(kernelSize: number): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'blur') {\n throw new Error('Render config must be of type blur to set blur properties.');\n }\n (this.config.render as MutableBlurConfig).kernelSize = kernelSize;\n return this;\n }\n\n /**\n * Sets the blur sigma value.\n * @param sigma The sigma value for the blur effect.\n */\n public setBlurSigma(sigma: number): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'blur') {\n throw new Error('Render config must be of type blur to set blur properties.');\n }\n (this.config.render as MutableBlurConfig).sigma = sigma;\n return this;\n }\n\n /**\n * Sets the replacement background.\n * @param background The background canvas or video element to use for replacement.\n */\n public setReplacementBackground(\n background: HTMLCanvasElement | HTMLVideoElement\n ): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'replacement') {\n throw new Error(\n 'Render config must be of type replacement to set replacement properties.'\n );\n }\n (this.config.render as MutableReplacementConfig).background = background;\n return this;\n }\n\n /**\n * Sets the replacement static property.\n * @param isStatic Boolean indicating if the replacement is static.\n */\n public setReplacementStatic(isStatic: boolean): ConfigurationBuilder {\n if (!this.config.render || this.config.render.type !== 'replacement') {\n throw new Error(\n 'Render config must be of type replacement to set replacement properties.'\n );\n }\n (this.config.render as MutableReplacementConfig).static = isStatic;\n return this;\n }\n\n /**\n * Sets the mask configuration.\n * @param maskConfig The mask configuration to use.\n */\n public setMaskConfig(maskConfig: MaskConfig): ConfigurationBuilder {\n this.config.mask = { ...maskConfig } as MutableMaskConfig;\n return this;\n }\n\n /**\n * Sets the mask generator.\n * @param generator The generator type (most be 'worker'; 'local' is deprecated).\n */\n public setMaskGenerator(generator: Generator): ConfigurationBuilder {\n if (generator !== 'worker') {\n console.warn('[ladon-ts] Mask generator must be \"worker\". \"local\" is deprecated.');\n }\n const mask = this.getMaskConfig();\n mask.generator = generator;\n return this;\n }\n\n /**\n * Sets the mask generation interval in milliseconds.\n * @param intervalMs The generation interval in milliseconds.\n */\n public setMaskGenerationIntervalMs(intervalMs: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.generationIntervalMs = intervalMs;\n return this;\n }\n\n /**\n * Sets the mask model rank.\n * @param modelRank The model rank to use.\n */\n public setMaskModelRank(modelRank: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.modelRank = modelRank;\n return this;\n }\n\n /**\n * Sets the mask model URI.\n * @param modelUri The model URI to use.\n */\n public setMaskModelUri(modelUri: string): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.modelUri = modelUri;\n return this;\n }\n\n /**\n * Sets the mask worker URI.\n * @param workerUri The worker URI to use.\n */\n public setMaskWorkerUri(workerUri: string): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.workerUri = workerUri;\n return this;\n }\n\n /**\n * Sets the mask input size.\n * @param height The height of the mask input.\n * @param width The width of the mask input.\n */\n public setMaskInputSize(height: number, width: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.inputSize = { height, width };\n return this;\n }\n\n /**\n * Sets the mask output size.\n * @param height The height of the mask output.\n * @param width The width of the mask output.\n */\n public setMaskOutputSize(height: number, width: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.outputSize = { height, width };\n return this;\n }\n\n /**\n * Sets the mask upscaled size.\n * @param height The height of the upscaled mask.\n * @param width The width of the upscaled mask.\n */\n public setMaskUpscaleSize(height: number, width: number): ConfigurationBuilder {\n const mask = this.getMaskConfig();\n mask.upscaleSize = { height, width };\n return this;\n }\n\n /**\n * Sets the quality level and updates mask configurations accordingly.\n * @param quality The desired quality level.\n */\n public setMaskQuality(quality: Quality): ConfigurationBuilder {\n const { modelQualities } = rawConfig;\n\n const mask = this.getMaskConfig();\n const qualityKey = quality.toLowerCase() as keyof RawConfig['modelQualities'];\n const qualityConfig = modelQualities[qualityKey];\n\n if (!qualityConfig) {\n throw new Error(`[ladon-ts] unknown quality option: ${quality}`);\n }\n\n // Store the quality key for dynamic resolution later\n mask.qualityKey = qualityKey;\n\n // Copy other quality-specific properties into the mask config\n Object.assign(mask, qualityConfig);\n\n return this;\n }\n\n /**\n * Builds and returns the PreloadConfig or PipelineConfig object.\n *\n * @param requireInputConfig Whether to require the input configuration to be set before building.\n * Input size is not available at preload so this needs to be optional. The default value is true.\n *\n */\n public build(requireInputConfig = true): PipelineConfig | PreloadConfig {\n const baseUrl = this.resolveBaseUrl();\n const modelUri = this.resolveModelUri();\n const wasmUri = this.resolveWasmUri();\n const workerUri = this.resolveWorkerUri();\n\n const input = this.config.input;\n const render = this.config.render;\n const maskConfig = this.config.mask;\n const executionProviders = this.config.executionProviders;\n const assetUrlResolver = this.config.assetUrlResolver;\n\n if (!maskConfig) {\n throw new Error('Mask configuration must be set before building the configuration.');\n }\n\n const mask: MaskConfig = {\n generator: maskConfig.generator,\n generationIntervalMs: maskConfig.generationIntervalMs,\n modelRank: maskConfig.modelRank,\n inputSize: maskConfig.inputSize,\n outputSize: maskConfig.outputSize,\n upscaleSize: maskConfig.upscaleSize,\n modelUri,\n workerUri,\n };\n\n // Build the preload configuration, which is valid for both preload and pipeline.\n const preloadConfig = {\n assetUrlResolver,\n executionProviders,\n baseUrl,\n wasmUri,\n mask,\n };\n\n if (requireInputConfig) {\n if (!input) {\n throw new Error('Input must be set before building the configuration.');\n }\n\n if (!render) {\n throw new Error('Render must be set before building the configuration.');\n }\n\n // Build the full PipelineConfig\n const pipelineConfig = {\n ...preloadConfig,\n input,\n render,\n } as PipelineConfig;\n\n return pipelineConfig;\n }\n\n return preloadConfig as PreloadConfig;\n }\n\n /**\n * Returns the mask configuration, initializing it if necessary.\n */\n private getMaskConfig(): MutableMaskConfig {\n if (!this.config.mask) {\n this.config.mask = {} as MutableMaskConfig;\n }\n return this.config.mask;\n }\n\n /**\n * Returns the base URL for the configuration.\n */\n public resolveBaseUrl(): string {\n // TODO: Do we want to publish the base URL in the raw config? It seems like the models\n // auth service should be responsible for this, or the user should provide the base url.\n return this.config.baseUrl || rawConfig.baseUrls[this.env];\n }\n\n /**\n * Returns the base URL for the configuration.\n *\n * @returns The base URL for the configuration.\n */\n public resolveModelUri(): string {\n // First check if the property is explicitly set in the config, then fallback to rawConfig\n if (this.config.mask?.modelUri) {\n return this.config.mask.modelUri;\n }\n\n const mask = this.getMaskConfig();\n if (!mask.qualityKey) {\n throw new Error('[ladon-ts] No quality set, cannot infer modelFile.');\n }\n\n return this.resolveTemplate(rawConfig.modelUrlTemplate, {\n BASE_PATH_MODELS: rawConfig.basePaths.models,\n MODEL_VERSION: rawConfig.modelVersion,\n MODEL_FILE: rawConfig.modelQualities[mask.qualityKey].modelFile,\n });\n }\n\n public resolveWasmUri(): string {\n // First check if the property is explicitly set in the config, then fallback to rawConfig\n if (this.config.wasmUri) {\n return this.config.wasmUri;\n }\n\n return this.resolveTemplate(rawConfig.wasmPathTemplate, {\n BASE_PATH_WASM: rawConfig.basePaths.wasm,\n ONNX_VERSION: rawConfig.onnxVersion,\n });\n }\n\n public resolveWorkerUri(): string {\n // First check if the property is explicitly set in the config, then fallback to rawConfig\n if (this.config.mask?.workerUri) {\n return this.config.mask.workerUri;\n }\n\n return this.resolveTemplate(rawConfig.workerUrlTemplate, {\n BASE_PATH_WORKERS: rawConfig.basePaths.workers,\n LIB_VERSION: ConfigurationBuilder.getLibVersion(),\n WORKER_FILE: rawConfig.workerFile,\n });\n }\n\n /**\n * Resolves a template string with the specified values.\n *\n * @param template The template string to resolve.\n * @param values The values to use for resolution.\n * @returns The resolved template string.\n */\n private resolveTemplate(template: string, values: Record<string, string>): string {\n return template.replace(/\\$\\{([A-Z_]+)\\}/g, (_, key) => {\n if (!values[key]) {\n throw new Error(`Missing value for template variable: ${key}`);\n }\n return values[key];\n });\n }\n\n /**\n * Generates a blur configuration based on the specified blur strength.\n * @param blurStrength The desired blur strength.\n * @param horizontalMirror Whether to apply horizontal mirroring.\n */\n public static getBlurConfig(\n blurStrength: BlurStrength,\n horizontalMirror?: boolean\n ): BlurConfig {\n const strengthKey = blurStrength.toLowerCase() as keyof RawConfig['blurStrengths'];\n const blurConfig = rawConfig.blurStrengths[strengthKey];\n\n if (!blurConfig) {\n throw new Error(`[ladon-ts] Unknown blur strength: ${blurStrength}`);\n }\n\n return {\n type: 'blur',\n horizontalMirror,\n ...blurConfig,\n };\n }\n\n public static getPassthroughConfig(horizontalMirror?: boolean): PassthroughConfig {\n return {\n type: 'passthrough',\n horizontalMirror,\n };\n }\n\n /**\n * Returns the library version.\n *\n * @returns The library version.\n */\n public static getLibVersion(): string {\n return LIB_VERSION;\n }\n}\n"],"names":["ConfigurationBuilder","env","baseUrl","wasmUri","assetUrlResolver","executionProviders","height","width","inputConfig","renderConfig","type","horizontalMirror","blurStrength","blurConfig","_a","kernelSize","sigma","background","isStatic","maskConfig","generator","mask","intervalMs","modelRank","modelUri","workerUri","quality","modelQualities","rawConfig","qualityKey","qualityConfig","requireInputConfig","input","render","preloadConfig","template","values","_","key","strengthKey","LIB_VERSION"],"mappings":";;AAyEO,MAAMA,EAAqB;AAA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C;AAC9E,WAAOD,EAAS,QAAQ,oBAAoB,CAACE,GAAGC,MAAQ;AAChD,UAAA,CAACF,EAAOE,CAAG;AACX,cAAM,IAAI,MAAM,wCAAwCA,CAAG,EAAE;AAEjE,aAAOF,EAAOE,CAAG;AAAA,IAAA,CACpB;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQL,OAAc,cACV1B,GACAD,GACU;AACJ,UAAA4B,IAAc3B,EAAa,YAAY,GACvCC,IAAae,EAAU,cAAcW,CAAW;AAEtD,QAAI,CAAC1B;AACD,YAAM,IAAI,MAAM,qCAAqCD,CAAY,EAAE;AAGhE,WAAA;AAAA,MACH,MAAM;AAAA,MACN,kBAAAD;AAAA,MACA,GAAGE;AAAA,IACP;AAAA,EAAA;AAAA,EAGJ,OAAc,qBAAqBF,GAA+C;AACvE,WAAA;AAAA,MACH,MAAM;AAAA,MACN,kBAAAA;AAAA,IACJ;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQJ,OAAc,gBAAwB;AAC3B,WAAA6B;AAAA,EAAA;AAEf;"}
|
|
@@ -41,9 +41,10 @@ export interface BlurConfig extends RenderConfig {
|
|
|
41
41
|
readonly sigma: number;
|
|
42
42
|
}
|
|
43
43
|
export interface ReplacementConfig extends RenderConfig {
|
|
44
|
+
readonly background: HTMLCanvasElement | HTMLVideoElement;
|
|
44
45
|
readonly type: 'replacement';
|
|
45
|
-
readonly background: HTMLCanvasElement;
|
|
46
46
|
readonly static: boolean;
|
|
47
|
+
readonly horizontalMirror?: boolean;
|
|
47
48
|
}
|
|
48
49
|
export interface PassthroughConfig extends RenderConfig {
|
|
49
50
|
readonly type: 'passthrough';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config-types.js","sources":["../../../src/lib/config-types.ts"],"sourcesContent":["export enum Quality {\n TINY = 'TINY',\n LOW = 'LOW',\n MEDIUM = 'MEDIUM',\n HIGH = 'HIGH',\n ULTRA = 'ULTRA',\n}\n\nexport enum BlurStrength {\n WEAK = 'WEAK',\n MODERATE = 'MODERATE',\n STRONG = 'STRONG',\n STRONGER = 'STRONGER',\n STRONGEST = 'STRONGEST',\n}\n\nexport type ResolutionConfig = {\n readonly height: number;\n readonly width: number;\n};\n\nexport interface SizeConfig {\n readonly inputSize: ResolutionConfig;\n readonly outputSize: ResolutionConfig;\n readonly upscaleSize: ResolutionConfig;\n}\n\nexport interface ModelQualityConfig {\n readonly modelFile: string;\n readonly modelRank: number;\n readonly generationIntervalMs: number;\n readonly inputSize: ResolutionConfig;\n readonly outputSize: ResolutionConfig;\n readonly upscaleSize: ResolutionConfig;\n}\n\nexport type InputConfig = ResolutionConfig;\n\nexport type AssetUrlResolver = (assetUri: string, config: PreloadConfig) => Promise<string>;\n\nexport interface RenderConfig {\n readonly type: 'blur' | 'passthrough' | 'replacement';\n readonly horizontalMirror?: boolean;\n}\n\nexport interface BlurConfig extends RenderConfig {\n readonly type: 'blur';\n readonly kernelSize: number;\n readonly sigma: number;\n}\n\nexport interface ReplacementConfig extends RenderConfig {\n readonly
|
|
1
|
+
{"version":3,"file":"config-types.js","sources":["../../../src/lib/config-types.ts"],"sourcesContent":["export enum Quality {\n TINY = 'TINY',\n LOW = 'LOW',\n MEDIUM = 'MEDIUM',\n HIGH = 'HIGH',\n ULTRA = 'ULTRA',\n}\n\nexport enum BlurStrength {\n WEAK = 'WEAK',\n MODERATE = 'MODERATE',\n STRONG = 'STRONG',\n STRONGER = 'STRONGER',\n STRONGEST = 'STRONGEST',\n}\n\nexport type ResolutionConfig = {\n readonly height: number;\n readonly width: number;\n};\n\nexport interface SizeConfig {\n readonly inputSize: ResolutionConfig;\n readonly outputSize: ResolutionConfig;\n readonly upscaleSize: ResolutionConfig;\n}\n\nexport interface ModelQualityConfig {\n readonly modelFile: string;\n readonly modelRank: number;\n readonly generationIntervalMs: number;\n readonly inputSize: ResolutionConfig;\n readonly outputSize: ResolutionConfig;\n readonly upscaleSize: ResolutionConfig;\n}\n\nexport type InputConfig = ResolutionConfig;\n\nexport type AssetUrlResolver = (assetUri: string, config: PreloadConfig) => Promise<string>;\n\nexport interface RenderConfig {\n readonly type: 'blur' | 'passthrough' | 'replacement';\n readonly horizontalMirror?: boolean;\n}\n\nexport interface BlurConfig extends RenderConfig {\n readonly type: 'blur';\n readonly kernelSize: number;\n readonly sigma: number;\n}\n\nexport interface ReplacementConfig extends RenderConfig {\n readonly background: HTMLCanvasElement | HTMLVideoElement;\n readonly type: 'replacement';\n readonly static: boolean;\n readonly horizontalMirror?: boolean;\n}\n\nexport interface PassthroughConfig extends RenderConfig {\n readonly type: 'passthrough';\n}\n\nexport type Generator = 'worker';\n\nexport type BaseMaskConfig = {\n readonly generator: Generator;\n readonly generationIntervalMs: number;\n readonly modelRank: number;\n readonly modelUri: string;\n readonly workerUri: string;\n // These are optional because the input and output sizes are not known when preloading the model.\n // However, the sizes are needed when creating the pipeline to match the models seize requirements.\n readonly inputSize?: ResolutionConfig;\n readonly outputSize?: ResolutionConfig;\n readonly upscaleSize?: ResolutionConfig;\n};\n\nexport type MaskConfig = BaseMaskConfig & SizeConfig;\n\nexport type PreloadConfig = {\n readonly baseUrl?: string;\n readonly wasmUri: string;\n readonly mask: BaseMaskConfig;\n readonly executionProviders?: string[];\n readonly assetUrlResolver?: AssetUrlResolver;\n};\n\nexport type WorkerConfig = Omit<PreloadConfig, 'assetUrlResolver'>;\n\nexport type PipelineConfig = PreloadConfig & {\n readonly render: BlurConfig | RenderConfig | ReplacementConfig;\n readonly input: InputConfig;\n readonly mask: MaskConfig;\n};\n\nexport interface RawConfig {\n readonly baseUrls: {\n readonly int: string;\n readonly prod: string;\n };\n readonly basePaths: {\n readonly models: string;\n readonly wasm: string;\n readonly workers: string;\n };\n readonly blurStrengths: {\n readonly [key in Lowercase<BlurStrength>]: {\n readonly kernelSize: number;\n readonly sigma: number;\n };\n };\n readonly modelQualities: {\n readonly [key in Lowercase<Quality>]: ModelQualityConfig;\n };\n readonly onnxVersion: string;\n readonly modelVersion: string;\n readonly workerFile: string;\n readonly modelUrlTemplate: string;\n readonly wasmPathTemplate: string;\n readonly workerUrlTemplate: string;\n}\n"],"names":["Quality","BlurStrength"],"mappings":"AAAY,IAAAA,sBAAAA,OACRA,EAAA,OAAO,QACPA,EAAA,MAAM,OACNA,EAAA,SAAS,UACTA,EAAA,OAAO,QACPA,EAAA,QAAQ,SALAA,IAAAA,KAAA,CAAA,CAAA,GAQAC,sBAAAA,OACRA,EAAA,OAAO,QACPA,EAAA,WAAW,YACXA,EAAA,SAAS,UACTA,EAAA,WAAW,YACXA,EAAA,YAAY,aALJA,IAAAA,KAAA,CAAA,CAAA;"}
|
|
@@ -29,6 +29,6 @@ export interface MaskGenerator {
|
|
|
29
29
|
getLadonOutputForTimestamp(timestamp: number): Promise<LadonOutput>;
|
|
30
30
|
load(): Promise<void>;
|
|
31
31
|
isLoaded(): boolean;
|
|
32
|
-
|
|
32
|
+
process(input: Uint8ClampedArray, timestamp: number): Promise<void>;
|
|
33
33
|
destroy(): Promise<void>;
|
|
34
34
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FacesAndLandmarks } from './generator-types';
|
|
2
|
+
export interface InferenceStats {
|
|
3
|
+
/** Total time from frame input to output (ms) */
|
|
4
|
+
totalFrameTime: number;
|
|
5
|
+
/** Time spent running the actual ML model inference (ms) */
|
|
6
|
+
modelInferenceTime: number;
|
|
7
|
+
/** Time spent on pre-processing (resizing, normalizing, etc.) (ms) */
|
|
8
|
+
preprocessingTime?: number;
|
|
9
|
+
/** Time spent on post-processing (masking, blending, etc.) (ms) */
|
|
10
|
+
postprocessingTime?: number;
|
|
11
|
+
/** GPU/CPU queue time if measurable (ms) */
|
|
12
|
+
queueTime?: number;
|
|
13
|
+
/** Frame timestamp when processing started */
|
|
14
|
+
frameStartTime: number;
|
|
15
|
+
/** Frame timestamp when processing completed */
|
|
16
|
+
frameEndTime: number;
|
|
17
|
+
/** Whether inference was skipped (reused previous result) */
|
|
18
|
+
inferenceSkipped?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface IFrameInferenceResult {
|
|
21
|
+
facesAndLandmarks: FacesAndLandmarks;
|
|
22
|
+
gesture: Float32Array;
|
|
23
|
+
motion: number;
|
|
24
|
+
skipped: boolean;
|
|
25
|
+
timestamp: number;
|
|
26
|
+
stats?: InferenceStats;
|
|
27
|
+
}
|
|
28
|
+
export interface IPipeline {
|
|
29
|
+
onFrame(timestamp: number, maskNeeded?: boolean): Promise<IFrameInferenceResult>;
|
|
30
|
+
isLoaded(): boolean;
|
|
31
|
+
load(): Promise<void>;
|
|
32
|
+
destroy(): Promise<void>;
|
|
33
|
+
reset(): void;
|
|
34
|
+
}
|
|
@@ -1,77 +1,109 @@
|
|
|
1
1
|
import { PipelineConfig } from './config-types';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
timestamp: number;
|
|
9
|
-
}
|
|
10
|
-
export interface IPipeline {
|
|
11
|
-
onFrame(timestamp: number, maskNeeded?: boolean): Promise<IFrameInferenceResult>;
|
|
12
|
-
isLoaded(): boolean;
|
|
13
|
-
load(): Promise<void>;
|
|
14
|
-
destroy(): Promise<void>;
|
|
15
|
-
reset(): void;
|
|
16
|
-
}
|
|
2
|
+
import { FrameData } from './generator';
|
|
3
|
+
import { IFrameInferenceResult, IPipeline } from './pipeline-types';
|
|
4
|
+
/**
|
|
5
|
+
* Core pipeline implementation for processing video frames with ML inference and rendering.
|
|
6
|
+
* Handles frame processing, mask generation, and output rendering with proper timing controls.
|
|
7
|
+
*/
|
|
17
8
|
export declare class Pipeline implements IPipeline {
|
|
18
9
|
private inputCanvas;
|
|
19
10
|
private outputCanvas;
|
|
20
11
|
readonly config: PipelineConfig;
|
|
21
|
-
private readonly
|
|
12
|
+
private readonly inputVideoElement;
|
|
13
|
+
/** The mask generator responsible for ML inference. */
|
|
22
14
|
private generator?;
|
|
15
|
+
/** The renderer responsible for compositing the final output. */
|
|
23
16
|
private renderer?;
|
|
17
|
+
/** Flag indicating whether the pipeline has been fully loaded and initialized. */
|
|
24
18
|
private loaded;
|
|
25
|
-
|
|
19
|
+
/** 2D rendering context for the input canvas used for frame processing. */
|
|
20
|
+
private inputCanvasContext;
|
|
21
|
+
/** The most recent mask result from ML inference, used when skipping inference. */
|
|
26
22
|
private lastMaskResult?;
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
/** Flag indicating whether a frame is currently being processed. */
|
|
24
|
+
private isProcessingFrame;
|
|
25
|
+
/** Promise for the currently pending frame processing operation. */
|
|
26
|
+
private pendingFramePromise;
|
|
29
27
|
/**
|
|
30
|
-
*
|
|
31
|
-
* This method handles the frame data, performs inference if needed, and renders the output.
|
|
28
|
+
* Create a new pipeline instance.
|
|
32
29
|
*
|
|
33
|
-
* @param
|
|
34
|
-
* @param
|
|
35
|
-
* @
|
|
36
|
-
* @
|
|
30
|
+
* @param inputCanvas - Canvas element for input frame processing.
|
|
31
|
+
* @param outputCanvas - Canvas element where the final rendered output will be displayed.
|
|
32
|
+
* @param config - Pipeline configuration containing processing and rendering settings.
|
|
33
|
+
* @param inputVideoElement - Video element serving as the input source.
|
|
34
|
+
* @throws Error if the input canvas 2D context cannot be obtained.
|
|
37
35
|
*/
|
|
38
|
-
|
|
36
|
+
constructor(inputCanvas: HTMLCanvasElement, outputCanvas: HTMLCanvasElement, config: PipelineConfig, inputVideoElement: HTMLVideoElement);
|
|
39
37
|
/**
|
|
40
|
-
*
|
|
38
|
+
* Process a single frame and return the inference result.
|
|
39
|
+
* This method handles frame data, performs inference if needed, and renders the output.
|
|
40
|
+
* Includes frame processing serialization to prevent overlapping operations.
|
|
41
41
|
*
|
|
42
|
-
* @param
|
|
42
|
+
* @param timestamp - The timestamp of the frame in milliseconds.
|
|
43
|
+
* @param performInference - Whether to perform mask inference or reuse the last result.
|
|
44
|
+
* @param frameData - Optional frame data to process instead of capturing from input.
|
|
45
|
+
* @returns Promise resolving to the frame processing result with timing statistics.
|
|
46
|
+
* @throws Error if the pipeline (generator or renderer) is not loaded.
|
|
47
|
+
*/
|
|
48
|
+
onFrame(timestamp: number, performInference?: boolean, frameData?: FrameData): Promise<IFrameInferenceResult>;
|
|
49
|
+
/**
|
|
50
|
+
* Internal frame processing implementation.
|
|
51
|
+
* Handles the actual frame processing logic including timing measurements.
|
|
43
52
|
*
|
|
44
|
-
* @
|
|
53
|
+
* @param timestamp - The timestamp of the frame in milliseconds.
|
|
54
|
+
* @param performInference - Whether to perform mask inference or reuse the last result.
|
|
55
|
+
* @param frameData - Optional frame data to process instead of capturing from input.
|
|
56
|
+
* @returns Promise resolving to the frame processing result with timing statistics.
|
|
57
|
+
* @throws Error if the pipeline (generator or renderer) is not loaded.
|
|
58
|
+
*/
|
|
59
|
+
private processFrameInternal;
|
|
60
|
+
/**
|
|
61
|
+
* Get the raw image data from the input canvas context.
|
|
62
|
+
*
|
|
63
|
+
* @returns The frame data as a Uint8ClampedArray.
|
|
45
64
|
*/
|
|
46
65
|
private getInputFrameData;
|
|
47
66
|
/**
|
|
48
|
-
*
|
|
67
|
+
* Resize the image data if the model expects a different input size.
|
|
68
|
+
* Handles scaling between input dimensions and model requirements.
|
|
49
69
|
*
|
|
50
|
-
* @param
|
|
51
|
-
* @returns
|
|
70
|
+
* @param rawImageData - The raw image data to potentially resize.
|
|
71
|
+
* @returns The processed mask image data, resized if necessary.
|
|
52
72
|
*/
|
|
53
73
|
private getMaskImageData;
|
|
54
74
|
/**
|
|
55
|
-
*
|
|
75
|
+
* Create the inference result object with appropriate data.
|
|
76
|
+
* Handles both successful inference results and fallback cases.
|
|
56
77
|
*
|
|
57
|
-
* @param
|
|
58
|
-
* @param
|
|
59
|
-
* @returns
|
|
78
|
+
* @param timestamp - The timestamp of the frame in milliseconds.
|
|
79
|
+
* @param performedInference - Whether inference was actually performed for this frame.
|
|
80
|
+
* @returns The inference result object containing mask data and metadata.
|
|
60
81
|
*/
|
|
61
82
|
private createInferenceResult;
|
|
62
83
|
/**
|
|
63
|
-
*
|
|
84
|
+
* Check if the pipeline is fully loaded and ready for processing.
|
|
64
85
|
*
|
|
65
|
-
* @returns True if the pipeline is loaded, false otherwise.
|
|
86
|
+
* @returns True if the pipeline is loaded and ready, false otherwise.
|
|
66
87
|
*/
|
|
67
88
|
isLoaded(): boolean;
|
|
68
89
|
/**
|
|
69
|
-
*
|
|
90
|
+
* Load and initialize the pipeline components.
|
|
91
|
+
* Creates the appropriate renderer and generator based on configuration.
|
|
92
|
+
*
|
|
93
|
+
* @returns Promise that resolves when all components are loaded and ready.
|
|
94
|
+
* @throws Error if an unsupported renderer type is specified in the configuration.
|
|
70
95
|
*/
|
|
71
96
|
load(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Reset the pipeline state by clearing cached mask results.
|
|
99
|
+
* Useful when switching between different processing modes or sources.
|
|
100
|
+
*/
|
|
72
101
|
reset(): void;
|
|
73
102
|
/**
|
|
74
|
-
*
|
|
103
|
+
* Destroy the pipeline and clean up all resources.
|
|
104
|
+
* Properly disposes of the renderer and generator to prevent memory leaks.
|
|
105
|
+
*
|
|
106
|
+
* @returns Promise that resolves when cleanup is complete.
|
|
75
107
|
*/
|
|
76
108
|
destroy(): Promise<void>;
|
|
77
109
|
}
|