From 69b27b246a3f11e775791805eb2c2b4858ed9412 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Mon, 5 Dec 2022 14:14:13 -0800 Subject: [PATCH] Adds a public function for creating TaskRunner instances. PiperOrigin-RevId: 493109736 --- mediapipe/tasks/web/core/task_runner.ts | 46 ++++++++++++++++--------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/mediapipe/tasks/web/core/task_runner.ts b/mediapipe/tasks/web/core/task_runner.ts index d769139bc..e2ab42e31 100644 --- a/mediapipe/tasks/web/core/task_runner.ts +++ b/mediapipe/tasks/web/core/task_runner.ts @@ -32,6 +32,34 @@ const GraphRunnerImageLibType = /** An implementation of the GraphRunner that supports image operations */ export class GraphRunnerImageLib extends GraphRunnerImageLibType {} +/** + * Creates a new instance of a Mediapipe Task. Determines if SIMD is + * supported and loads the relevant WASM binary. + * @return A fully instantiated instance of `T`. + */ +export async function +createTaskRunner, O extends TaskRunnerOptions>( + type: WasmMediaPipeConstructor, initializeCanvas: boolean, + fileset: WasmFileset, options: O): Promise { + const fileLocator: FileLocator = { + locateFile() { + // The only file loaded with this mechanism is the Wasm binary + return fileset.wasmBinaryPath.toString(); + } + }; + + // Initialize a canvas if requested. If OffscreenCanvas is availble, we + // let the graph runner initialize it by passing `undefined`. + const canvas = initializeCanvas ? (typeof OffscreenCanvas === 'undefined' ? + document.createElement('canvas') : + undefined) : + null; + const instance = await createMediaPipeLib( + type, fileset.wasmLoaderPath, NO_ASSETS, canvas, fileLocator); + await instance.setOptions(options); + return instance; +} + /** Base class for all MediaPipe Tasks. */ export abstract class TaskRunner { protected abstract baseOptions: BaseOptionsProto; @@ -47,23 +75,7 @@ export abstract class TaskRunner { O extends TaskRunnerOptions>( type: WasmMediaPipeConstructor, initializeCanvas: boolean, fileset: WasmFileset, options: O): Promise { - const fileLocator: FileLocator = { - locateFile() { - // The only file loaded with this mechanism is the Wasm binary - return fileset.wasmBinaryPath.toString(); - } - }; - - // Initialize a canvas if requested. If OffscreenCanvas is availble, we - // let the graph runner initialize it by passing `undefined`. - const canvas = initializeCanvas ? (typeof OffscreenCanvas === 'undefined' ? - document.createElement('canvas') : - undefined) : - null; - const instance = await createMediaPipeLib( - type, fileset.wasmLoaderPath, NO_ASSETS, canvas, fileLocator); - await instance.setOptions(options); - return instance; + return createTaskRunner(type, initializeCanvas, fileset, options); } constructor(