From 145fc1ed38fca43b9b027fc7eee42f91a8edc480 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 30 Mar 2023 17:54:15 -0700 Subject: [PATCH] Don't overwrite detection options if not specified in setOptions() PiperOrigin-RevId: 520790479 --- .../gesture_recognizer/gesture_recognizer.ts | 25 ++++++++++++++----- .../gesture_recognizer_test.ts | 12 +++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer.ts b/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer.ts index 4927d363..74d37cb6 100644 --- a/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer.ts +++ b/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer.ts @@ -140,6 +140,11 @@ export class GestureRecognizer extends VisionTaskRunner { new HandGestureRecognizerGraphOptions(); this.options.setHandGestureRecognizerGraphOptions( this.handGestureRecognizerGraphOptions); + this.handDetectorGraphOptions.setMinDetectionConfidence(DEFAULT_CONFIDENCE); + this.handLandmarkerGraphOptions.setMinTrackingConfidence( + DEFAULT_CONFIDENCE); + this.handLandmarksDetectorGraphOptions.setMinDetectionConfidence( + DEFAULT_CONFIDENCE); } protected override get baseOptions(): BaseOptionsProto { @@ -162,12 +167,20 @@ export class GestureRecognizer extends VisionTaskRunner { override setOptions(options: GestureRecognizerOptions): Promise { this.handDetectorGraphOptions.setNumHands( options.numHands ?? DEFAULT_NUM_HANDS); - this.handDetectorGraphOptions.setMinDetectionConfidence( - options.minHandDetectionConfidence ?? DEFAULT_CONFIDENCE); - this.handLandmarkerGraphOptions.setMinTrackingConfidence( - options.minTrackingConfidence ?? DEFAULT_CONFIDENCE); - this.handLandmarksDetectorGraphOptions.setMinDetectionConfidence( - options.minHandPresenceConfidence ?? DEFAULT_CONFIDENCE); + if ('minHandDetectionConfidence' in options) { + this.handDetectorGraphOptions.setMinDetectionConfidence( + options.minHandDetectionConfidence ?? DEFAULT_CONFIDENCE); + } + + if ('minTrackingConfidence' in options) { + this.handLandmarkerGraphOptions.setMinTrackingConfidence( + options.minTrackingConfidence ?? DEFAULT_CONFIDENCE); + } + + if ('minHandPresenceConfidence' in options) { + this.handLandmarksDetectorGraphOptions.setMinDetectionConfidence( + options.minHandPresenceConfidence ?? DEFAULT_CONFIDENCE); + } if (options.cannedGesturesClassifierOptions) { // Note that we have to support both JSPB and ProtobufJS and cannot diff --git a/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer_test.ts b/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer_test.ts index b2a2c0d7..2331f5e2 100644 --- a/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer_test.ts +++ b/mediapipe/tasks/web/vision/gesture_recognizer/gesture_recognizer_test.ts @@ -147,6 +147,18 @@ describe('GestureRecognizer', () => { ]); }); + it('does not reset default values when not specified', async () => { + await gestureRecognizer.setOptions({minHandDetectionConfidence: 0.5}); + await gestureRecognizer.setOptions({}); + verifyGraph(gestureRecognizer, [ + [ + 'handLandmarkerGraphOptions', 'handDetectorGraphOptions', + 'minDetectionConfidence' + ], + 0.5 + ]); + }); + describe('setOptions()', () => { interface TestCase { optionPath: [keyof GestureRecognizerOptions, ...string[]];