Added MPPHandLandmarkerResult Helpers

This commit is contained in:
Prianka Liz Kariat 2023-05-31 20:42:49 +05:30
parent d76b48ec8b
commit 71f2f8f43b
3 changed files with 200 additions and 0 deletions

View File

@ -31,3 +31,17 @@ objc_library(
"//mediapipe/tasks/ios/vision/hand_landmarker:MPPHandLandmarkerOptions", "//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",
],
)

View File

@ -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<NormalizedlandmarkListProto>`.
* @param worldLandmarksPacket A MediaPipe packet wrapping a`std::vector<LandmarkListProto>`.
* @param handednessPacket a MediaPipe packet wrapping a`std::vector<ClassificationListProto>`.
*
* @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<NormalizedlandmarkListProto>`.
* @param worldLandmarksPacket A vector of protos of type `std::vector<LandmarkListProto>`.
* @param handednessPacket A vector of protos of type `std::vector<ClassificationListProto>`.
*
* @return An `MPPHandLandmarkerResult` object that contains the hand landmark detection
* results.
*/
+ (MPPHandLandmarkerResult *)
handLandmarkerResultWithLandmarksProto:
(const std::vector<mediapipe::NormalizedLandmarkList> &)landmarksProto
worldLandmarksProto:
(const std::vector<mediapipe::LandmarkList> &)worldLandmarksProto
handednessProto:
(const std::vector<mediapipe::ClassificationList> &)handednessProto;
@end
NS_ASSUME_NONNULL_END

View File

@ -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<NormalizedLandmarkListProto> &)landmarksProto
worldLandmarksProto:
(const std::vector<LandmarkListProto> &)worldLandmarksProto
handednessProto:
(const std::vector<ClassificationListProto> &)handednessProto
timestampInMilliSeconds:(NSInteger)timestampInMilliseconds {
NSMutableArray<NSMutableArray<MPPNormalizedLandmark *> *> *multiHandLandmarks =
[NSMutableArray arrayWithCapacity:(NSUInteger)landmarksProto.size()];
for (const auto &landmarkListProto : landmarksProto) {
NSMutableArray<MPPNormalizedLandmark *> *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<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];
}
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];
}
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<std::vector<ClassificationListProto>>().ok() ||
!landmarksPacket.ValidateAsType<std::vector<NormalizedLandmarkListProto>>().ok() ||
!worldLandmarksPacket.ValidateAsType<std::vector<LandmarkListProto>>().ok()) {
return [MPPHandLandmarkerResult
emptyHandLandmarkerResultWithTimestampInMilliseconds:timestampInMilliseconds];
}
return [MPPHandLandmarkerResult
handLandmarkerResultWithLandmarksProto:landmarksPacket
.Get<std::vector<NormalizedLandmarkListProto>>()
worldLandmarksProto:worldLandmarksPacket
.Get<std::vector<LandmarkListProto>>()
handednessProto:handednessPacket
.Get<std::vector<ClassificationListProto>>()
timestampInMilliSeconds:timestampInMilliseconds];
}
@end