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
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
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/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<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 *)
gestureRecognizerResultWithHandGesturesPacket:(const Packet &)handGesturesPacket
handednessPacket:(const Packet &)handednessPacket
@ -62,76 +132,16 @@ using ::mediapipe::Packet;
emptyGestureRecognizerResultWithTimestampInMilliseconds:timestampInMilliseconds];
}
const std::vector<ClassificationListProto> &handGesturesClassificationListProtos =
handGesturesPacket.Get<std::vector<ClassificationListProto>>();
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandGestures =
[NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesClassificationListProtos.size()];
for (const auto &classificationListProto : handGesturesClassificationListProtos) {
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];
}
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;
return [MPPGestureRecognizerResult
gestureRecognizerResultWithHandGesturesProto:handGesturesPacket
.Get<std::vector<ClassificationListProto>>()
handednessroto:handednessPacket
.Get<std::vector<ClassificationListProto>>()
handLandmarksPacket:handLandmarksPacket
.Get<std::vector<NormalizedLandmarkListProto>>()
worldLandmarksPacket:worldLandmarksPacket
.Get<std::vector<LandmarkListProto>>()
timestampInMilliSeconds:timestampInMilliseconds];
}
@end