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"
#include "mediapipe/framework/formats/classification.pb.h"
#include "mediapipe/framework/formats/landmark.pb.h"
#include "mediapipe/framework/packet.h"
NS_ASSUME_NONNULL_BEGIN
@ -23,14 +25,14 @@ static const int kMicroSecondsPerMilliSecond = 1000;
@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.
*
* @param handGesturesPacket 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 handGesturesPacket A MediaPipe packet wrapping a`std::vector<ClassificationListProto>`.
* @param handednessPacket A MediaPipe packet wrapping a`std::vector<ClassificationListProto>`.
* @param handLandmarksPacket A MediaPipe packet wrapping
* 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
* results.
@ -41,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,10 +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"
namespace {
using ClassificationListProto = ::mediapipe::ClassificationList;
using LandmarkListProto = ::mediapipe::LandmarkList;
@ -28,52 +24,47 @@ using NormalizedLandmarkListProto = ::mediapipe::NormalizedLandmarkList;
using ::mediapipe::Packet;
} // namespace
static const NSInteger kDefaultGestureIndex = -1;
@implementation MPPGestureRecognizerResult (Helpers)
+ (MPPGestureRecognizerResult *)emptyGestureRecognizerResultWithTimestampInMilliseconds:
(NSInteger)timestampInMilliseconds {
return [[MPPGestureRecognizerResult alloc] initWithGestures:@[]
handedness:@[]
landmarks:@[]
worldLandmarks:@[]
timestampInMilliseconds:timestampInMilliseconds];
}
+ (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 alloc] initWithGestures:@[]
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>>();
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)handGesturesClassificationListProtos.size()];
[NSMutableArray arrayWithCapacity:(NSUInteger)handGesturesProto.size()];
for (const auto &classificationListProto : handGesturesClassificationListProtos) {
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];
MPPCategory *category = [MPPCategory categoryWithProto:classificationProto
andIndex: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()];
[NSMutableArray arrayWithCapacity:(NSUInteger)handednessProto.size()];
for (const auto &classificationListProto : handednessClassificationListProtos) {
for (const auto &classificationListProto : handednessProto) {
NSMutableArray<MPPCategory *> *handedness = [NSMutableArray
arrayWithCapacity:(NSUInteger)classificationListProto.classification().size()];
for (const auto &classificationProto : classificationListProto.classification()) {
@ -83,36 +74,31 @@ using ::mediapipe::Packet;
[multiHandHandedness addObject:handedness];
}
const std::vector<NormalizedLandmarkListProto> &handLandmarkListProtos =
handLandmarksPacket.Get<std::vector<NormalizedLandmarkListProto>>();
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 arrayWithCapacity:(NSUInteger)handLandmarkListProto.landmark().size()];
for (const auto &normalizedLandmarkProto : handLandmarkListProto.landmark()) {
MPPNormalizedLandmark *normalizedLandmark =
[MPPNormalizedLandmark normalizedLandmarkWithProto:normalizedLandmarkProto];
[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()];
[NSMutableArray arrayWithCapacity:(NSUInteger)worldLandmarksProto.size()];
for (const auto &worldLandmarkListProto : worldLandmarkListProtos) {
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];
MPPLandmark *landmark = [MPPLandmark landmarkWithProto:landmarkProto];
[worldLandmarks addObject:landmark];
}
[multiHandWorldLandmarks addObject:worldLandmarks];
[multiHandWorldLandmarks addObject:worldLandmarks];
}
MPPGestureRecognizerResult *gestureRecognizerResult =
@ -125,4 +111,37 @@ using ::mediapipe::Packet;
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