From 4c02980b3f62d10ae409b629468012b7de8988c0 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 30 Nov 2023 02:31:11 +0530 Subject: [PATCH] Added iOS region of interest helpers --- .../ios/components/containers/utils/BUILD | 13 +++++ .../sources/MPPRegionOfInterest+Helpers.h | 35 ++++++++++++ .../sources/MPPRegionOfInterest+Helpers.mm | 56 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.h create mode 100644 mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.mm diff --git a/mediapipe/tasks/ios/components/containers/utils/BUILD b/mediapipe/tasks/ios/components/containers/utils/BUILD index 5f0311f51..6da949858 100644 --- a/mediapipe/tasks/ios/components/containers/utils/BUILD +++ b/mediapipe/tasks/ios/components/containers/utils/BUILD @@ -16,6 +16,7 @@ package(default_visibility = ["//mediapipe/tasks:internal"]) licenses(["notice"]) + objc_library( name = "MPPCategoryHelpers", srcs = ["sources/MPPCategory+Helpers.mm"], @@ -84,3 +85,15 @@ objc_library( "//mediapipe/tasks/ios/components/containers:MPPLandmark", ], ) + +objc_library( + name = "MPPRegionOfInterestHelpers", + srcs = ["sources/MPPRegionOfInterest+Helpers.mm"], + hdrs = ["sources/MPPRegionOfInterest+Helpers.h"], + deps = [ + "//mediapipe/util:render_data_cc_proto", + "//mediapipe/tasks/ios/common:MPPCommon", + "//mediapipe/tasks/ios/common/utils:MPPCommonUtils", + "//mediapipe/tasks/ios/components/containers:MPPRegionOfInterest", + ], +) diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.h b/mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.h new file mode 100644 index 000000000..026d4f2e9 --- /dev/null +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.h @@ -0,0 +1,35 @@ +// 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/util/render_data.pb.h" + +#import "mediapipe/tasks/ios/components/containers/sources/MPPRegionOfInterest.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MPPRegionOfInterest (Helpers) + +/** + * Creates a `RenderData` from the region of interest. + * + * @param error Pointer to the memory location where errors if any should be saved. If @c NULL, no + * error will be saved. + * + * @return A `RenderData1 proto created from the region of interest. + */ +- (std::optional)getRenderDataWithError:(NSError **)error; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.mm b/mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.mm new file mode 100644 index 000000000..c7f0bc9a0 --- /dev/null +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.mm @@ -0,0 +1,56 @@ +// 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/common/sources/MPPCommon.h" +#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" +#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPRegionOfInterest+Helpers.h" + +namespace { +using RenderData = ::mediapipe::RenderData; +using RenderAnnotation = ::mediapipe::RenderAnnotation; + +} // namespace + +@implementation MPPRegionOfInterest (Helpers) + +- (std::optional)getRenderDataWithError:(NSError**)error { + RenderData result; + if (self.keypoint) { + auto* annotation = result.add_render_annotations(); + annotation->mutable_color()->set_r(255); + auto* point = annotation->mutable_point(); + point->set_normalized(true); + point->set_x(self.keypoint.location.x); + point->set_y(self.keypoint.location.y); + return result; + } else if (self.scribbles) { + auto* annotation = result.add_render_annotations(); + annotation->mutable_color()->set_r(255); + for (MPPNormalizedKeypoint* keypoint in self.scribbles) { + auto* point = annotation->mutable_scribble()->add_point(); + point->set_normalized(true); + point->set_x(self.keypoint.location.x); + point->set_y(self.keypoint.location.y); + } + return result; + } + + [MPPCommonUtils createCustomError:error + withCode:MPPTasksErrorCodeInvalidArgumentError + description:@"RegionOfInterest does not include a valid user interaction."]; + + return std::nullopt; +} + +@end