From f3f664300cac55b34ab478b26b9780c0ccd30bcc Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Wed, 31 May 2023 11:50:39 +0530 Subject: [PATCH] Added convenience method for creating results for tests in MPPGestureRecognizerResult Helpers --- .../MPPGestureRecognizerResult+Helpers.h | 24 +++ .../MPPGestureRecognizerResult+Helpers.mm | 162 ++++++++++-------- 2 files changed, 110 insertions(+), 76 deletions(-) diff --git a/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.h b/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.h index 6b0f8bf81..5e75febf3 100644 --- a/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.h +++ b/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.h @@ -43,6 +43,30 @@ static const int kMicroSecondsPerMilliSecond = 1000; handLandmarksPacket:(const mediapipe::Packet &)handLandmarksPacket worldLandmarksPacket:(const mediapipe::Packet &)worldLandmarksPacket; +/** + * Creates an `MPPGestureRecognizerResult` from hand gestures, handedness, hand landmarks and world + * landmarks proto vectors. + * + * @param handGesturesProto A vector of protos of type `std::vector`. + * @param handednessPacket A vector of protos of type `std::vector`. + * @param handLandmarksPacket A vector of protos of type `std::vector`. + * @param handLandmarksPacket A vector of protos of type `std::vector`. + * + * @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition + * results. + */ ++ (MPPGestureRecognizerResult *) + gestureRecognizerResultWithHandGesturesProto: + (const std::vector &)handGesturesProto + handednessroto: + (const std::vector &) + handednessProto + handLandmarksPacket: + (const std::vector &) + handLandmarksProto + worldLandmarksPacket: + (const std::vector &)worldLandmarksProto + timestampInMilliSeconds:(NSInteger)timestampInMilliseconds; @end NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.mm b/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.mm index 8eed2a923..5162dd891 100644 --- a/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.mm +++ b/mediapipe/tasks/ios/vision/gesture_recognizer/utils/sources/MPPGestureRecognizerResult+Helpers.mm @@ -17,12 +17,6 @@ #import "mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.h" #import "mediapipe/tasks/ios/components/containers/utils/sources/MPPLandmark+Helpers.h" -#include "mediapipe/framework/formats/classification.pb.h" -#include "mediapipe/framework/formats/landmark.pb.h" -#include "mediapipe/framework/packet.h" - -static const NSInteger kDefaultGestureIndex = -1; - namespace { using ClassificationListProto = ::mediapipe::ClassificationList; using LandmarkListProto = ::mediapipe::LandmarkList; @@ -30,6 +24,8 @@ using NormalizedLandmarkListProto = ::mediapipe::NormalizedLandmarkList; using ::mediapipe::Packet; } // namespace +static const NSInteger kDefaultGestureIndex = -1; + @implementation MPPGestureRecognizerResult (Helpers) + (MPPGestureRecognizerResult *)emptyGestureRecognizerResultWithTimestampInMilliseconds: @@ -41,6 +37,80 @@ using ::mediapipe::Packet; timestampInMilliseconds:timestampInMilliseconds]; } ++ (MPPGestureRecognizerResult *) + gestureRecognizerResultWithHandGesturesProto: + (const std::vector &)handGesturesProto + handednessroto: + (const std::vector &)handednessProto + handLandmarksPacket:(const std::vector &) + handLandmarksProto + worldLandmarksPacket: + (const std::vector &)worldLandmarksProto + timestampInMilliSeconds:(NSInteger)timestampInMilliseconds { + NSMutableArray *> *multiHandGestures = + [NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesProto.size()]; + + for (const auto &classificationListProto : handGesturesProto) { + NSMutableArray *gestures = [NSMutableArray + arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; + for (const auto &classificationProto : classificationListProto.classification()) { + MPPCategory *category = [MPPCategory categoryWithProto:classificationProto + index:kDefaultGestureIndex]; + [gestures addObject:category]; + } + [multiHandGestures addObject:gestures]; + } + + NSMutableArray *> *multiHandHandedness = + [NSMutableArray arrayWithCapacity:(NSUInteger)handednessProto.size()]; + + for (const auto &classificationListProto : handednessProto) { + NSMutableArray *handedness = [NSMutableArray + arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; + for (const auto &classificationProto : classificationListProto.classification()) { + MPPCategory *category = [MPPCategory categoryWithProto:classificationProto]; + [handedness addObject:category]; + } + [multiHandHandedness addObject:handedness]; + } + + NSMutableArray *> *multiHandLandmarks = + [NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarksProto.size()]; + + for (const auto &handLandmarkListProto : handLandmarksProto) { + NSMutableArray *handLandmarks = + [NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProto.landmark().size()]; + for (const auto &normalizedLandmarkProto : handLandmarkListProto.landmark()) { + MPPNormalizedLandmark *normalizedLandmark = + [MPPNormalizedLandmark normalizedLandmarkWithProto:normalizedLandmarkProto]; + [handLandmarks addObject:normalizedLandmark]; + } + [multiHandLandmarks addObject:handLandmarks]; + } + + NSMutableArray *> *multiHandWorldLandmarks = + [NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarksProto.size()]; + + for (const auto &worldLandmarkListProto : worldLandmarksProto) { + NSMutableArray *worldLandmarks = + [NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProto.landmark().size()]; + for (const auto &landmarkProto : worldLandmarkListProto.landmark()) { + MPPLandmark *landmark = [MPPLandmark landmarkWithProto:landmarkProto]; + [worldLandmarks addObject:landmark]; + } + [multiHandWorldLandmarks addObject:worldLandmarks]; + } + + MPPGestureRecognizerResult *gestureRecognizerResult = + [[MPPGestureRecognizerResult alloc] initWithGestures:multiHandGestures + handedness:multiHandHandedness + landmarks:multiHandLandmarks + worldLandmarks:multiHandWorldLandmarks + timestampInMilliseconds:timestampInMilliseconds]; + + return gestureRecognizerResult; +} + + (MPPGestureRecognizerResult *) gestureRecognizerResultWithHandGesturesPacket:(const Packet &)handGesturesPacket handednessPacket:(const Packet &)handednessPacket @@ -62,76 +132,16 @@ using ::mediapipe::Packet; emptyGestureRecognizerResultWithTimestampInMilliseconds:timestampInMilliseconds]; } - const std::vector &handGesturesClassificationListProtos = - handGesturesPacket.Get>(); - NSMutableArray *> *multiHandGestures = - [NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesClassificationListProtos.size()]; - - for (const auto &classificationListProto : handGesturesClassificationListProtos) { - NSMutableArray *gestures = [NSMutableArray - arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; - for (const auto &classificationProto : classificationListProto.classification()) { - MPPCategory *category = [MPPCategory categoryWithProto:classificationProto - index:kDefaultGestureIndex]; - [gestures addObject:category]; - } - [multiHandGestures addObject:gestures]; - } - - const std::vector &handednessClassificationListProtos = - handednessPacket.Get>(); - NSMutableArray *> *multiHandHandedness = - [NSMutableArray arrayWithCapacity:(NSUInteger)handednessClassificationListProtos.size()]; - - for (const auto &classificationListProto : handednessClassificationListProtos) { - NSMutableArray *handedness = [NSMutableArray - arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; - for (const auto &classificationProto : classificationListProto.classification()) { - MPPCategory *category = [MPPCategory categoryWithProto:classificationProto]; - [handedness addObject:category]; - } - [multiHandHandedness addObject:handedness]; - } - - const std::vector &handLandmarkListProtos = - handLandmarksPacket.Get>(); - NSMutableArray *> *multiHandLandmarks = - [NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProtos.size()]; - - for (const auto &handLandmarkListProto : handLandmarkListProtos) { - NSMutableArray *handLandmarks = - [NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProto.landmark().size()]; - for (const auto &normalizedLandmarkProto : handLandmarkListProto.landmark()) { - MPPNormalizedLandmark *normalizedLandmark = - [MPPNormalizedLandmark normalizedLandmarkWithProto:normalizedLandmarkProto]; - [handLandmarks addObject:normalizedLandmark]; - } - [multiHandLandmarks addObject:handLandmarks]; - } - - const std::vector &worldLandmarkListProtos = - worldLandmarksPacket.Get>(); - NSMutableArray *> *multiHandWorldLandmarks = - [NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProtos.size()]; - - for (const auto &worldLandmarkListProto : worldLandmarkListProtos) { - NSMutableArray *worldLandmarks = - [NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProto.landmark().size()]; - for (const auto &landmarkProto : worldLandmarkListProto.landmark()) { - MPPLandmark *landmark = [MPPLandmark landmarkWithProto:landmarkProto]; - [worldLandmarks addObject:landmark]; - } - [multiHandWorldLandmarks addObject:worldLandmarks]; - } - - MPPGestureRecognizerResult *gestureRecognizerResult = - [[MPPGestureRecognizerResult alloc] initWithGestures:multiHandGestures - handedness:multiHandHandedness - landmarks:multiHandLandmarks - worldLandmarks:multiHandWorldLandmarks - timestampInMilliseconds:timestampInMilliseconds]; - - return gestureRecognizerResult; + return [MPPGestureRecognizerResult + gestureRecognizerResultWithHandGesturesProto:handGesturesPacket + .Get>() + handednessroto:handednessPacket + .Get>() + handLandmarksPacket:handLandmarksPacket + .Get>() + worldLandmarksPacket:worldLandmarksPacket + .Get>() + timestampInMilliSeconds:timestampInMilliseconds]; } @end