From 6c9104318809936bb705b7246ad8376b76da8a0b Mon Sep 17 00:00:00 2001 From: Mou Date: Mon, 1 Apr 2024 02:18:33 +0200 Subject: [PATCH] Move gesture recognizer defaults from constants.ts to async create method --- src/lib/Recognizer.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib/Recognizer.ts b/src/lib/Recognizer.ts index c326ac2..96babeb 100644 --- a/src/lib/Recognizer.ts +++ b/src/lib/Recognizer.ts @@ -1,9 +1,9 @@ import { + FilesetResolver, GestureRecognizer, type GestureRecognizerOptions, type GestureRecognizerResult, } from "@mediapipe/tasks-vision"; -import { defaultOptions, defaultWasmFileset } from "./constants"; import type { WasmFileset } from "./types"; /** @@ -68,8 +68,31 @@ export default class Recognizer { vision?: WasmFileset; options?: GestureRecognizerOptions; }): Promise { + /** + * Default WebAssembly fileset required for MediaPipe Task APIs. + * https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm + */ + const defaultWasmFileset = await FilesetResolver.forVisionTasks( + "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm", + ); + const recognizerVision = vision || defaultWasmFileset; + /** + * Default options for the gesture recognizer. + */ + const defaultOptions: GestureRecognizerOptions = { + runningMode: "VIDEO", + baseOptions: { + // Pretrained model for gesture recognition. + // https://developers.google.com/mediapipe/solutions/vision/gesture_recognizer#models + modelAssetPath: + "https://storage.googleapis.com/mediapipe-models/gesture_recognizer/gesture_recognizer/float16/1/gesture_recognizer.task", + delegate: "GPU", + }, + numHands: 1, + }; + const recognizerOptions = options || defaultOptions; const recognizer = new Recognizer(videoElement, recognizerVision, recognizerOptions);