diff --git a/mediapipe/tasks/ios/components/containers/utils/BUILD b/mediapipe/tasks/ios/components/containers/utils/BUILD index 64ca29b88..3520740b0 100644 --- a/mediapipe/tasks/ios/components/containers/utils/BUILD +++ b/mediapipe/tasks/ios/components/containers/utils/BUILD @@ -61,3 +61,14 @@ objc_library( "//mediapipe/tasks/ios/components/containers:MPPEmbeddingResult", ], ) + +objc_library( + name = "MPPDetectionHelpers", + srcs = ["sources/MPPDetection+Helpers.mm"], + hdrs = ["sources/MPPDetection+Helpers.h"], + deps = [ + "//mediapipe/framework/formats:detection_cc_proto", + "//mediapipe/tasks/ios/common/utils:NSStringHelpers", + "//mediapipe/tasks/ios/components/containers:MPPDetection", + ], +) diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPDetection+Helpers.h b/mediapipe/tasks/ios/components/containers/utils/sources/MPPDetection+Helpers.h new file mode 100644 index 000000000..c06c04d3e --- /dev/null +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPDetection+Helpers.h @@ -0,0 +1,26 @@ +// 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. + +#include "mediapipe/framework/formats/detection.pb.h" +#import "mediapipe/tasks/ios/components/containers/sources/MPPDetection.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MPPDetection (Helpers) + ++ (MPPDetection *)detectionWithProto:(const mediapipe::Detection &)detectionProto; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPDetection+Helpers.mm b/mediapipe/tasks/ios/components/containers/utils/sources/MPPDetection+Helpers.mm new file mode 100644 index 000000000..e5cc8dc03 --- /dev/null +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPDetection+Helpers.mm @@ -0,0 +1,82 @@ +// 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/components/containers/utils/sources/MPPDetection+Helpers.h" + +#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h" + +static const NSInteger kDefaultCategoryIndex = -1; + +namespace { +using DetectionProto = ::mediapipe::Detection; +using BoundingBoxProto = ::mediapipe::LocationData::BoundingBox; +} // namespace + +@implementation MPPDetection (Helpers) + ++ (MPPDetection *)detectionWithProto:(const DetectionProto &)detectionProto { + NSMutableArray *categories = + [NSMutableArray arrayWithCapacity:(NSUInteger)detectionProto.score_size()]; + + for (int idx = 0; idx < detectionProto.score_size(); ++idx) { + NSInteger categoryIndex = + detectionProto.label_id_size() > idx ? detectionProto.label_id(idx) : kDefaultCategoryIndex; + NSString *categoryName = detectionProto.label_size() > idx + ? [NSString stringWithCppString:detectionProto.label(idx)] + : nil; + + NSString *displayName = detectionProto.display_name_size() > idx + ? [NSString stringWithCppString:detectionProto.display_name(idx)] + : nil; + + [categories addObject:[[MPPCategory alloc] initWithIndex:categoryIndex + score:detectionProto.score(idx) + categoryName:categoryName + displayName:displayName]]; + } + + CGRect boundingBox = CGRectZero; + + if (detectionProto.location_data().has_bounding_box()) { + const BoundingBoxProto &boundingBoxProto = detectionProto.location_data().bounding_box(); + boundingBox.origin.x = boundingBoxProto.xmin(); + boundingBox.origin.y = boundingBoxProto.ymin(); + boundingBox.size.width = boundingBoxProto.width(); + boundingBox.size.height = boundingBoxProto.height(); + } + + NSMutableArray *normalizedKeypoints; + + if (!detectionProto.location_data().relative_keypoints().empty()) { + normalizedKeypoints = [NSMutableArray + arrayWithCapacity:(NSUInteger)detectionProto.location_data().relative_keypoints_size()]; + for (const auto &keypoint : detectionProto.location_data().relative_keypoints()) { + NSString *label = keypoint.has_keypoint_label() + ? [NSString stringWithCppString:keypoint.keypoint_label()] + : nil; + CGPoint location = CGPointMake(keypoint.x(), keypoint.y()); + float score = keypoint.has_score() ? keypoint.score() : 0.0f; + + [normalizedKeypoints addObject:[[MPPNormalizedKeypoint alloc] initWithLocation:location + label:label + score:score]]; + } + } + + return [[MPPDetection alloc] initWithCategories:categories + boundingBox:boundingBox + keypoints:normalizedKeypoints]; +} + +@end