Added convenience method for creating results for tests in MPPGestureRecognizerResult Helpers

This commit is contained in:
Prianka Liz Kariat 2023-05-31 11:50:39 +05:30
parent 84560f3e7d
commit f3f664300c
2 changed files with 110 additions and 76 deletions

View File

@ -43,6 +43,30 @@ static const int kMicroSecondsPerMilliSecond = 1000;
handLandmarksPacket:(const mediapipe::Packet &)handLandmarksPacket handLandmarksPacket:(const mediapipe::Packet &)handLandmarksPacket
worldLandmarksPacket:(const mediapipe::Packet &)worldLandmarksPacket; 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<ClassificationListProto>`.
* @param handednessPacket A vector of protos of type `std::vector<ClassificationListProto>`.
* @param handLandmarksPacket A vector of protos of type `std::vector<NormalizedlandmarkListProto>`.
* @param handLandmarksPacket A vector of protos of type `std::vector<LandmarkListProto>`.
*
* @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition
* results.
*/
+ (MPPGestureRecognizerResult *)
gestureRecognizerResultWithHandGesturesProto:
(const std::vector<mediapipe::ClassificationList> &)handGesturesProto
handednessroto:
(const std::vector<mediapipe::ClassificationList> &)
handednessProto
handLandmarksPacket:
(const std::vector<mediapipe::NormalizedLandmarkList> &)
handLandmarksProto
worldLandmarksPacket:
(const std::vector<mediapipe::LandmarkList> &)worldLandmarksProto
timestampInMilliSeconds:(NSInteger)timestampInMilliseconds;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@ -17,12 +17,6 @@
#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.h" #import "mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.h"
#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPLandmark+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 { namespace {
using ClassificationListProto = ::mediapipe::ClassificationList; using ClassificationListProto = ::mediapipe::ClassificationList;
using LandmarkListProto = ::mediapipe::LandmarkList; using LandmarkListProto = ::mediapipe::LandmarkList;
@ -30,6 +24,8 @@ using NormalizedLandmarkListProto = ::mediapipe::NormalizedLandmarkList;
using ::mediapipe::Packet; using ::mediapipe::Packet;
} // namespace } // namespace
static const NSInteger kDefaultGestureIndex = -1;
@implementation MPPGestureRecognizerResult (Helpers) @implementation MPPGestureRecognizerResult (Helpers)
+ (MPPGestureRecognizerResult *)emptyGestureRecognizerResultWithTimestampInMilliseconds: + (MPPGestureRecognizerResult *)emptyGestureRecognizerResultWithTimestampInMilliseconds:
@ -41,6 +37,80 @@ using ::mediapipe::Packet;
timestampInMilliseconds:timestampInMilliseconds]; timestampInMilliseconds:timestampInMilliseconds];
} }
+ (MPPGestureRecognizerResult *)
gestureRecognizerResultWithHandGesturesProto:
(const std::vector<ClassificationListProto> &)handGesturesProto
handednessroto:
(const std::vector<ClassificationListProto> &)handednessProto
handLandmarksPacket:(const std::vector<NormalizedLandmarkListProto> &)
handLandmarksProto
worldLandmarksPacket:
(const std::vector<LandmarkListProto> &)worldLandmarksProto
timestampInMilliSeconds:(NSInteger)timestampInMilliseconds {
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandGestures =
[NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesProto.size()];
for (const auto &classificationListProto : handGesturesProto) {
NSMutableArray<MPPCategory *> *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<NSMutableArray<MPPCategory *> *> *multiHandHandedness =
[NSMutableArray arrayWithCapacity:(NSUInteger)handednessProto.size()];
for (const auto &classificationListProto : handednessProto) {
NSMutableArray<MPPCategory *> *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<NSMutableArray<MPPNormalizedLandmark *> *> *multiHandLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarksProto.size()];
for (const auto &handLandmarkListProto : handLandmarksProto) {
NSMutableArray<MPPNormalizedLandmark *> *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<NSMutableArray<MPPLandmark *> *> *multiHandWorldLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarksProto.size()];
for (const auto &worldLandmarkListProto : worldLandmarksProto) {
NSMutableArray<MPPLandmark *> *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 *) + (MPPGestureRecognizerResult *)
gestureRecognizerResultWithHandGesturesPacket:(const Packet &)handGesturesPacket gestureRecognizerResultWithHandGesturesPacket:(const Packet &)handGesturesPacket
handednessPacket:(const Packet &)handednessPacket handednessPacket:(const Packet &)handednessPacket
@ -62,76 +132,16 @@ using ::mediapipe::Packet;
emptyGestureRecognizerResultWithTimestampInMilliseconds:timestampInMilliseconds]; emptyGestureRecognizerResultWithTimestampInMilliseconds:timestampInMilliseconds];
} }
const std::vector<ClassificationListProto> &handGesturesClassificationListProtos = return [MPPGestureRecognizerResult
handGesturesPacket.Get<std::vector<ClassificationListProto>>(); gestureRecognizerResultWithHandGesturesProto:handGesturesPacket
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandGestures = .Get<std::vector<ClassificationListProto>>()
[NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesClassificationListProtos.size()]; handednessroto:handednessPacket
.Get<std::vector<ClassificationListProto>>()
for (const auto &classificationListProto : handGesturesClassificationListProtos) { handLandmarksPacket:handLandmarksPacket
NSMutableArray<MPPCategory *> *gestures = [NSMutableArray .Get<std::vector<NormalizedLandmarkListProto>>()
arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; worldLandmarksPacket:worldLandmarksPacket
for (const auto &classificationProto : classificationListProto.classification()) { .Get<std::vector<LandmarkListProto>>()
MPPCategory *category = [MPPCategory categoryWithProto:classificationProto timestampInMilliSeconds:timestampInMilliseconds];
index:kDefaultGestureIndex];
[gestures addObject:category];
}
[multiHandGestures addObject:gestures];
}
const std::vector<ClassificationListProto> &handednessClassificationListProtos =
handednessPacket.Get<std::vector<ClassificationListProto>>();
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandHandedness =
[NSMutableArray arrayWithCapacity:(NSUInteger)handednessClassificationListProtos.size()];
for (const auto &classificationListProto : handednessClassificationListProtos) {
NSMutableArray<MPPCategory *> *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<NormalizedLandmarkListProto> &handLandmarkListProtos =
handLandmarksPacket.Get<std::vector<NormalizedLandmarkListProto>>();
NSMutableArray<NSMutableArray<MPPNormalizedLandmark *> *> *multiHandLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProtos.size()];
for (const auto &handLandmarkListProto : handLandmarkListProtos) {
NSMutableArray<MPPNormalizedLandmark *> *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<LandmarkListProto> &worldLandmarkListProtos =
worldLandmarksPacket.Get<std::vector<LandmarkListProto>>();
NSMutableArray<NSMutableArray<MPPLandmark *> *> *multiHandWorldLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProtos.size()];
for (const auto &worldLandmarkListProto : worldLandmarkListProtos) {
NSMutableArray<MPPLandmark *> *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;
} }
@end @end