Added a method for creating ios hand gesture result from protos

This commit is contained in:
Prianka Liz Kariat 2023-05-24 19:09:48 +05:30
parent bb5fcc2d64
commit e16f5f8f08
2 changed files with 97 additions and 52 deletions

View File

@ -14,6 +14,8 @@
#import "mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizerResult.h" #import "mediapipe/tasks/ios/vision/gesture_recognizer/sources/MPPGestureRecognizerResult.h"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/landmark.pb.h"
#include "mediapipe/framework/packet.h" #include "mediapipe/framework/packet.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -23,14 +25,14 @@ static const int kMicroSecondsPerMilliSecond = 1000;
@interface MPPGestureRecognizerResult (Helpers) @interface MPPGestureRecognizerResult (Helpers)
/** /**
* Creates an `MPPGestureRecognizerResult` from hand gestures, handedness, hand landmarks and world * Creates an `MPPGestureRecognizerResult` from hand gestures, handedness, hand landmarks and world
* landmarks packets. * landmarks packets.
* *
* @param handGesturesPacket a MediaPipe packet wrapping a`std::vector<ClassificationListProto>`. * @param handGesturesPacket A MediaPipe packet wrapping a`std::vector<ClassificationListProto>`.
* @param handednessPacket a MediaPipe packet wrapping a`std::vector<ClassificationListProto>`. * @param handednessPacket A MediaPipe packet wrapping a`std::vector<ClassificationListProto>`.
* @param handLandmarksPacket a MediaPipe packet wrapping * @param handLandmarksPacket A MediaPipe packet wrapping
* a`std::vector<NormalizedlandmarkListProto>`. * a`std::vector<NormalizedlandmarkListProto>`.
* @param handLandmarksPacket a MediaPipe packet wrapping a`std::vector<LandmarkListProto>`. * @param worldLandmarksPacket A MediaPipe packet wrapping a`std::vector<LandmarkListProto>`.
* *
* @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition * @return An `MPPGestureRecognizerResult` object that contains the hand gesture recognition
* results. * results.
@ -41,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,10 +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"
namespace { namespace {
using ClassificationListProto = ::mediapipe::ClassificationList; using ClassificationListProto = ::mediapipe::ClassificationList;
using LandmarkListProto = ::mediapipe::LandmarkList; using LandmarkListProto = ::mediapipe::LandmarkList;
@ -28,52 +24,47 @@ 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:
(NSInteger)timestampInMilliseconds {
return [[MPPGestureRecognizerResult alloc] initWithGestures:@[]
handedness:@[]
landmarks:@[]
worldLandmarks:@[]
timestampInMilliseconds:timestampInMilliseconds];
}
+ (MPPGestureRecognizerResult *) + (MPPGestureRecognizerResult *)
gestureRecognizerResultWithHandGesturesPacket:(const Packet &)handGesturesPacket gestureRecognizerResultWithHandGesturesProto:
handednessPacket:(const Packet &)handednessPacket (const std::vector<ClassificationListProto> &)handGesturesProto
handLandmarksPacket:(const Packet &)handLandmarksPacket handednessroto:
worldLandmarksPacket:(const Packet &)worldLandmarksPacket { (const std::vector<ClassificationListProto> &)handednessProto
NSInteger timestampInMilliseconds = handLandmarksPacket:(const std::vector<NormalizedLandmarkListProto> &)
(NSInteger)(handGesturesPacket.Timestamp().Value() / kMicroSecondsPerMilliSecond); handLandmarksProto
worldLandmarksPacket:
if (handGesturesPacket.IsEmpty()) { (const std::vector<LandmarkListProto> &)worldLandmarksProto
return [[MPPGestureRecognizerResult alloc] initWithGestures:@[] timestampInMilliSeconds:(NSInteger)timestampInMilliseconds {
handedness:@[]
landmarks:@[]
worldLandmarks:@[]
timestampInMilliseconds:timestampInMilliseconds];
}
if (!handGesturesPacket.ValidateAsType<std::vector<ClassificationListProto>>().ok() ||
!handednessPacket.ValidateAsType<std::vector<ClassificationListProto>>().ok() ||
!handLandmarksPacket.ValidateAsType<std::vector<NormalizedLandmarkListProto>>().ok() ||
!worldLandmarksPacket.ValidateAsType<std::vector<LandmarkListProto>>().ok()) {
return nil;
}
const std::vector<ClassificationListProto> &handGesturesClassificationListProtos =
handGesturesPacket.Get<std::vector<ClassificationListProto>>();
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandGestures = NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandGestures =
[NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesClassificationListProtos.size()]; [NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesProto.size()];
for (const auto &classificationListProto : handGesturesClassificationListProtos) { for (const auto &classificationListProto : handGesturesProto) {
NSMutableArray<MPPCategory *> *gestures = [NSMutableArray NSMutableArray<MPPCategory *> *gestures = [NSMutableArray
arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()];
for (const auto &classificationProto : classificationListProto.classification()) { for (const auto &classificationProto : classificationListProto.classification()) {
MPPCategory *category = [MPPCategory categoryWithProto:classificationProto]; MPPCategory *category = [MPPCategory categoryWithProto:classificationProto
andIndex:kDefaultGestureIndex];
[gestures addObject:category]; [gestures addObject:category];
} }
[multiHandGestures addObject:gestures]; [multiHandGestures addObject:gestures];
} }
const std::vector<ClassificationListProto> &handednessClassificationListProtos =
handednessPacket.Get<std::vector<ClassificationListProto>>();
NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandHandedness = NSMutableArray<NSMutableArray<MPPCategory *> *> *multiHandHandedness =
[NSMutableArray arrayWithCapacity:(NSUInteger)handednessClassificationListProtos.size()]; [NSMutableArray arrayWithCapacity:(NSUInteger)handednessProto.size()];
for (const auto &classificationListProto : handednessClassificationListProtos) { for (const auto &classificationListProto : handednessProto) {
NSMutableArray<MPPCategory *> *handedness = [NSMutableArray NSMutableArray<MPPCategory *> *handedness = [NSMutableArray
arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()]; arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()];
for (const auto &classificationProto : classificationListProto.classification()) { for (const auto &classificationProto : classificationListProto.classification()) {
@ -83,12 +74,10 @@ using ::mediapipe::Packet;
[multiHandHandedness addObject:handedness]; [multiHandHandedness addObject:handedness];
} }
const std::vector<NormalizedLandmarkListProto> &handLandmarkListProtos =
handLandmarksPacket.Get<std::vector<NormalizedLandmarkListProto>>();
NSMutableArray<NSMutableArray<MPPNormalizedLandmark *> *> *multiHandLandmarks = NSMutableArray<NSMutableArray<MPPNormalizedLandmark *> *> *multiHandLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProtos.size()]; [NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarksProto.size()];
for (const auto &handLandmarkListProto : handLandmarkListProtos) { for (const auto &handLandmarkListProto : handLandmarksProto) {
NSMutableArray<MPPNormalizedLandmark *> *handLandmarks = NSMutableArray<MPPNormalizedLandmark *> *handLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProto.landmark().size()]; [NSMutableArray arrayWithCapacity:(NSUInteger)handLandmarkListProto.landmark().size()];
for (const auto &normalizedLandmarkProto : handLandmarkListProto.landmark()) { for (const auto &normalizedLandmarkProto : handLandmarkListProto.landmark()) {
@ -99,20 +88,17 @@ using ::mediapipe::Packet;
[multiHandLandmarks addObject:handLandmarks]; [multiHandLandmarks addObject:handLandmarks];
} }
const std::vector<LandmarkListProto> &worldLandmarkListProtos =
worldLandmarksPacket.Get<std::vector<LandmarkListProto>>();
NSMutableArray<NSMutableArray<MPPLandmark *> *> *multiHandWorldLandmarks = NSMutableArray<NSMutableArray<MPPLandmark *> *> *multiHandWorldLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProtos.size()]; [NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarksProto.size()];
for (const auto &worldLandmarkListProto : worldLandmarkListProtos) { for (const auto &worldLandmarkListProto : worldLandmarksProto) {
NSMutableArray<MPPLandmark *> *worldLandmarks = NSMutableArray<MPPLandmark *> *worldLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProto.landmark().size()]; [NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarkListProto.landmark().size()];
for (const auto &landmarkProto : worldLandmarkListProto.landmark()) { for (const auto &landmarkProto : worldLandmarkListProto.landmark()) {
MPPLandmark *landmark = MPPLandmark *landmark = [MPPLandmark landmarkWithProto:landmarkProto];
[MPPLandmark landmarkWithProto:landmarkProto];
[worldLandmarks addObject:landmark]; [worldLandmarks addObject:landmark];
} }
[multiHandWorldLandmarks addObject:worldLandmarks]; [multiHandWorldLandmarks addObject:worldLandmarks];
} }
MPPGestureRecognizerResult *gestureRecognizerResult = MPPGestureRecognizerResult *gestureRecognizerResult =
@ -125,4 +111,37 @@ using ::mediapipe::Packet;
return gestureRecognizerResult; return gestureRecognizerResult;
} }
+ (MPPGestureRecognizerResult *)
gestureRecognizerResultWithHandGesturesPacket:(const Packet &)handGesturesPacket
handednessPacket:(const Packet &)handednessPacket
handLandmarksPacket:(const Packet &)handLandmarksPacket
worldLandmarksPacket:(const Packet &)worldLandmarksPacket {
NSInteger timestampInMilliseconds =
(NSInteger)(handGesturesPacket.Timestamp().Value() / kMicroSecondsPerMilliSecond);
if (handGesturesPacket.IsEmpty()) {
return [MPPGestureRecognizerResult
emptyGestureRecognizerResultWithTimestampInMilliseconds:timestampInMilliseconds];
}
if (!handGesturesPacket.ValidateAsType<std::vector<ClassificationListProto>>().ok() ||
!handednessPacket.ValidateAsType<std::vector<ClassificationListProto>>().ok() ||
!handLandmarksPacket.ValidateAsType<std::vector<NormalizedLandmarkListProto>>().ok() ||
!worldLandmarksPacket.ValidateAsType<std::vector<LandmarkListProto>>().ok()) {
return [MPPGestureRecognizerResult
emptyGestureRecognizerResultWithTimestampInMilliseconds:timestampInMilliseconds];
}
return [MPPGestureRecognizerResult
gestureRecognizerResultWithHandGesturesProto:handGesturesPacket
.Get<std::vector<ClassificationListProto>>()
handednessroto:handednessPacket
.Get<std::vector<ClassificationListProto>>()
handLandmarksPacket:handednessPacket
.Get<std::vector<ClassificationListProto>>()
worldLandmarksPacket:worldLandmarksPacket
.Get<std::vector<LandmarkListProto>>()
timestampInMilliSeconds:timestampInMilliseconds];
}
@end @end