From 71f2f8f43b53f56c7ffea763bca18c8e2aaab7d2 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Wed, 31 May 2023 20:42:49 +0530 Subject: [PATCH] Added MPPHandLandmarkerResult Helpers --- .../ios/vision/hand_landmarker/utils/BUILD | 14 ++ .../sources/MPPHandLandmarkerResult+Helpers.h | 64 +++++++++ .../MPPHandLandmarkerResult+Helpers.mm | 122 ++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.h create mode 100644 mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.mm diff --git a/mediapipe/tasks/ios/vision/hand_landmarker/utils/BUILD b/mediapipe/tasks/ios/vision/hand_landmarker/utils/BUILD index dfb85a218..ed22d171f 100644 --- a/mediapipe/tasks/ios/vision/hand_landmarker/utils/BUILD +++ b/mediapipe/tasks/ios/vision/hand_landmarker/utils/BUILD @@ -31,3 +31,17 @@ objc_library( "//mediapipe/tasks/ios/vision/hand_landmarker:MPPHandLandmarkerOptions", ], ) + +objc_library( + name = "MPPHandLandmarkerResultHelpers", + srcs = ["sources/MPPHandLandmarkerResult+Helpers.mm"], + hdrs = ["sources/MPPHandLandmarkerResult+Helpers.h"], + deps = [ + "//mediapipe/framework:packet", + "//mediapipe/framework/formats:classification_cc_proto", + "//mediapipe/framework/formats:landmark_cc_proto", + "//mediapipe/tasks/ios/components/containers/utils:MPPCategoryHelpers", + "//mediapipe/tasks/ios/components/containers/utils:MPPLandmarkHelpers", + "//mediapipe/tasks/ios/vision/hand_landmarker:MPPHandLandmarkerResult", + ], +) diff --git a/mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.h b/mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.h new file mode 100644 index 000000000..fa7bd42ce --- /dev/null +++ b/mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.h @@ -0,0 +1,64 @@ +// Copyright 2023 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "mediapipe/tasks/ios/vision/hand_landmarker/sources/MPPHandLandmarkerResult.h" + +#include "mediapipe/framework/formats/classification.pb.h" +#include "mediapipe/framework/formats/landmark.pb.h" +#include "mediapipe/framework/packet.h" + +NS_ASSUME_NONNULL_BEGIN + +static const int kMicroSecondsPerMilliSecond = 1000; + +@interface MPPHandLandmarkerResult (Helpers) + +/** + * Creates an `MPPHandLandmarkerResult` from landmarks, world landmarks and handedness packets. + * + * @param landmarksPacket A MediaPipe packet wrapping + * a`std::vector`. + * @param worldLandmarksPacket A MediaPipe packet wrapping a`std::vector`. + * @param handednessPacket a MediaPipe packet wrapping a`std::vector`. + * + * @return An `MPPHandLandmarkerResult` object that contains the hand landmark detection + * results. + */ ++ (MPPHandLandmarkerResult *) + handLandmarkerResultWithLandmarksPacket:(const mediapipe::Packet &)handLandmarksPacket + worldLandmarksPacket:(const mediapipe::Packet &)worldLandmarksPacket + handednessPacket:(const mediapipe::Packet &)handednessPacket; + +/** + * Creates an `MPPHandLandmarkerResult` from handedness, landmarks and world landmarks proto + * vectors. + * + * @param landmarksProto A vector of protos of type `std::vector`. + * @param worldLandmarksPacket A vector of protos of type `std::vector`. + * @param handednessPacket A vector of protos of type `std::vector`. + * + * @return An `MPPHandLandmarkerResult` object that contains the hand landmark detection + * results. + */ ++ (MPPHandLandmarkerResult *) + handLandmarkerResultWithLandmarksProto: + (const std::vector &)landmarksProto + worldLandmarksProto: + (const std::vector &)worldLandmarksProto + handednessProto: + (const std::vector &)handednessProto; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.mm b/mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.mm new file mode 100644 index 000000000..2936853ab --- /dev/null +++ b/mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.mm @@ -0,0 +1,122 @@ +// Copyright 2023 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "mediapipe/tasks/ios/vision/hand_landmarker/utils/sources/MPPHandLandmarkerResult+Helpers.h" + +#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.h" +#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPLandmark+Helpers.h" + +namespace { +using ClassificationListProto = ::mediapipe::ClassificationList; +using LandmarkListProto = ::mediapipe::LandmarkList; +using NormalizedLandmarkListProto = ::mediapipe::NormalizedLandmarkList; +using ::mediapipe::Packet; +} // namespace + +@implementation MPPHandLandmarkerResult (Helpers) + ++ (MPPHandLandmarkerResult *)emptyHandLandmarkerResultWithTimestampInMilliseconds: + (NSInteger)timestampInMilliseconds { + return [[MPPHandLandmarkerResult alloc] initWithLandmarks:@[] + worldLandmarks:@[] + handedness:@[] + timestampInMilliseconds:timestampInMilliseconds]; +} + ++ (MPPHandLandmarkerResult *) + handLandmarkerResultWithLandmarksProto: + (const std::vector &)landmarksProto + worldLandmarksProto: + (const std::vector &)worldLandmarksProto + handednessProto: + (const std::vector &)handednessProto + timestampInMilliSeconds:(NSInteger)timestampInMilliseconds { + NSMutableArray *> *multiHandLandmarks = + [NSMutableArray arrayWithCapacity:(NSUInteger)landmarksProto.size()]; + + for (const auto &landmarkListProto : landmarksProto) { + NSMutableArray *landmarks = + [NSMutableArray arrayWithCapacity:(NSUInteger)landmarkListProto.landmark().size()]; + for (const auto &normalizedLandmarkProto : landmarkListProto.landmark()) { + MPPNormalizedLandmark *normalizedLandmark = + [MPPNormalizedLandmark normalizedLandmarkWithProto:normalizedLandmarkProto]; + [landmarks addObject:normalizedLandmark]; + } + [multiHandLandmarks addObject:landmarks]; + } + + 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]; + } + + 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]; + } + + MPPHandLandmarkerResult *handLandmarkerResult = + [[MPPHandLandmarkerResult alloc] initWithLandmarks:multiHandLandmarks + worldLandmarks:multiHandWorldLandmarks + handedness:multiHandHandedness + timestampInMilliseconds:timestampInMilliseconds]; + return handLandmarkerResult; +} + ++ (MPPHandLandmarkerResult *) + handLandmarkerResultWithLandmarksPacket:(const Packet &)landmarksPacket + worldLandmarksPacket:(const Packet &)worldLandmarksPacket + handednessPacket:(const Packet &)handednessPacket { + NSInteger timestampInMilliseconds = + (NSInteger)(landmarksPacket.Timestamp().Value() / kMicroSecondsPerMilliSecond); + + if (landmarksPacket.IsEmpty()) { + return [MPPHandLandmarkerResult + emptyHandLandmarkerResultWithTimestampInMilliseconds:timestampInMilliseconds]; + } + + if (!handednessPacket.ValidateAsType>().ok() || + !landmarksPacket.ValidateAsType>().ok() || + !worldLandmarksPacket.ValidateAsType>().ok()) { + return [MPPHandLandmarkerResult + emptyHandLandmarkerResultWithTimestampInMilliseconds:timestampInMilliseconds]; + } + + return [MPPHandLandmarkerResult + handLandmarkerResultWithLandmarksProto:landmarksPacket + .Get>() + worldLandmarksProto:worldLandmarksPacket + .Get>() + handednessProto:handednessPacket + .Get>() + timestampInMilliSeconds:timestampInMilliseconds]; +} + +@end