From 84560f3e7db5026fb762f570b1ee13cd5c159442 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Wed, 31 May 2023 11:36:29 +0530 Subject: [PATCH] Added more recognize tests to iOS Gesture Recognizer Objective C tests --- .../MPPGestureRecognizerTests.m | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/mediapipe/tasks/ios/test/vision/gesture_recognizer/MPPGestureRecognizerTests.m b/mediapipe/tasks/ios/test/vision/gesture_recognizer/MPPGestureRecognizerTests.m index 1a48322b4..94600a83f 100644 --- a/mediapipe/tasks/ios/test/vision/gesture_recognizer/MPPGestureRecognizerTests.m +++ b/mediapipe/tasks/ios/test/vision/gesture_recognizer/MPPGestureRecognizerTests.m @@ -284,4 +284,128 @@ static const float kLandmarksErrorTolerance = 0.03f; thumbUpGestureRecognizerResult]]; } +- (void)testRecognizeWithEmptyResultsSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + + MPPGestureRecognizerResult *gestureRecognizerResult = + [self recognizeImageWithFileInfo:kNoHandsImage usingGestureRecognizer:gestureRecognizer]; + AssertGestureRecognizerResultIsEmpty(gestureRecognizerResult); +} + +- (void)testRecognizeWithScoreThresholdSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + gestureRecognizerOptions.cannedGesturesClassifierOptions = [[MPPClassifierOptions alloc] init]; + gestureRecognizerOptions.cannedGesturesClassifierOptions.scoreThreshold = 0.5f; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + + MPPGestureRecognizerResult *gestureRecognizerResult = + [self recognizeImageWithFileInfo:kThumbUpImage usingGestureRecognizer:gestureRecognizer]; + + MPPGestureRecognizerResult *expectedGestureRecognizerResult = + [MPPGestureRecognizerTests thumbUpGestureRecognizerResult]; + + XCTAssertTrue(gestureRecognizerResult.gestures.count == 1); + AssertEqualGestures(gestureRecognizerResult.gestures[0][0], + expectedGestureRecognizerResult.gestures[0][0], 0, 0); +} + +- (void)testRecognizeWithNumHandsSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + + const NSInteger numberOfHands = 2; + gestureRecognizerOptions.numberOfHands = numberOfHands; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + + MPPGestureRecognizerResult *gestureRecognizerResult = + [self recognizeImageWithFileInfo:kTwoHandsImage usingGestureRecognizer:gestureRecognizer]; + + XCTAssertTrue(gestureRecognizerResult.handedness.count == numberOfHands); +} + +- (void)testRecognizeWithRotationSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + + const NSInteger numberOfHands = 2; + gestureRecognizerOptions.numberOfHands = numberOfHands; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + MPPImage *mppImage = [self imageWithFileInfo:kPointingUpRotatedImage + orientation:UIImageOrientationRight]; + + MPPGestureRecognizerResult *gestureRecognizerResult = [gestureRecognizer recognizeImage:mppImage + error:nil]; + + XCTAssertNotNil(gestureRecognizerResult); + + const NSInteger expectedGesturesCount = 1; + + XCTAssertEqual(gestureRecognizerResult.gestures.count, expectedGesturesCount); + XCTAssertEqualObjects(gestureRecognizerResult.gestures[0][0].categoryName, + kExpectedPointingUpLabel); +} + +- (void)testRecognizeWithCannedGestureFistSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + + const NSInteger numberOfHands = 1; + gestureRecognizerOptions.numberOfHands = numberOfHands; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + + [self assertResultsOfRecognizeImageWithFileInfo:kFistImage + usingGestureRecognizer:gestureRecognizer + approximatelyEqualsGestureRecognizerResult: + [MPPGestureRecognizerTests fistGestureRecognizerResultWithLabel:kFistLabel]]; +} + +- (void)testRecognizeWithAllowGestureFistSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + gestureRecognizerOptions.cannedGesturesClassifierOptions = [[MPPClassifierOptions alloc] init]; + gestureRecognizerOptions.cannedGesturesClassifierOptions.scoreThreshold = 0.5f; + gestureRecognizerOptions.cannedGesturesClassifierOptions.categoryAllowlist = @[ kFistLabel ]; + + const NSInteger numberOfHands = 1; + gestureRecognizerOptions.numberOfHands = numberOfHands; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + + [self assertResultsOfRecognizeImageWithFileInfo:kFistImage + usingGestureRecognizer:gestureRecognizer + approximatelyEqualsGestureRecognizerResult: + [MPPGestureRecognizerTests fistGestureRecognizerResultWithLabel:kFistLabel]]; +} + +- (void)testRecognizeWithDenyGestureFistSucceeds { + MPPGestureRecognizerOptions *gestureRecognizerOptions = + [self gestureRecognizerOptionsWithModelFileInfo:kGestureRecognizerBundleAssetFile]; + gestureRecognizerOptions.cannedGesturesClassifierOptions = [[MPPClassifierOptions alloc] init]; + gestureRecognizerOptions.cannedGesturesClassifierOptions.scoreThreshold = 0.5f; + gestureRecognizerOptions.cannedGesturesClassifierOptions.categoryDenylist = @[ kFistLabel ]; + + const NSInteger numberOfHands = 1; + gestureRecognizerOptions.numberOfHands = numberOfHands; + + MPPGestureRecognizer *gestureRecognizer = + [self createGestureRecognizerWithOptionsSucceeds:gestureRecognizerOptions]; + MPPGestureRecognizerResult *gestureRecognizerResult = + [self recognizeImageWithFileInfo:kFistImage usingGestureRecognizer:gestureRecognizer]; + AssertGestureRecognizerResultIsEmpty(gestureRecognizerResult); +} + @end