From 38eac174e9a36dbb67f6c2e33d9ca984ba27d3ad Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Wed, 1 Feb 2023 18:51:01 +0530 Subject: [PATCH] Added MPPTextEmbedderResultHelpers --- .../tasks/ios/text/text_embedder/utils/BUILD | 11 +++++ .../sources/MPPTextEmbedderResult+Helpers.h | 28 +++++++++++++ .../sources/MPPTextEmbedderResult+Helpers.mm | 42 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.h create mode 100644 mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.mm diff --git a/mediapipe/tasks/ios/text/text_embedder/utils/BUILD b/mediapipe/tasks/ios/text/text_embedder/utils/BUILD index 0c41298ac..eeb4981fb 100644 --- a/mediapipe/tasks/ios/text/text_embedder/utils/BUILD +++ b/mediapipe/tasks/ios/text/text_embedder/utils/BUILD @@ -31,3 +31,14 @@ objc_library( ], ) +objc_library( + name = "MPPTextEmbedderResultHelpers", + srcs = ["sources/MPPTextEmbedderResult+Helpers.mm"], + hdrs = ["sources/MPPTextEmbedderResult+Helpers.h"], + deps = [ + "//mediapipe/framework:packet", + "//mediapipe/tasks/cc/components/containers/proto:embeddings_cc_proto", + "//mediapipe/tasks/ios/components/containers/utils:MPPEmbeddingResultHelpers", + "//mediapipe/tasks/ios/text/text_embedder:MPPTextEmbedderResult", + ], +) diff --git a/mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.h b/mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.h new file mode 100644 index 000000000..de899a103 --- /dev/null +++ b/mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.h @@ -0,0 +1,28 @@ +// 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/text/text_embedder/sources/MPPTextEmbedderResult.h" + +#include "mediapipe/framework/packet.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MPPTextEmbedderResult (Helpers) + ++ (MPPTextEmbedderResult *)textEmbedderResultWithOutputPacket: + (const mediapipe::Packet &)packet; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.mm b/mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.mm new file mode 100644 index 000000000..bba1b345e --- /dev/null +++ b/mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.mm @@ -0,0 +1,42 @@ +// 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/MPPEmbeddingResult+Helpers.h" +#import "mediapipe/tasks/ios/text/text_embedder/utils/sources/MPPTextEmbedderResult+Helpers.h" + +#include "mediapipe/tasks/cc/components/containers/proto/embeddings.pb.h" + +static const int kMicroSecondsPerMilliSecond = 1000; + +namespace { +using EmbeddingResultProto = + ::mediapipe::tasks::components::containers::proto::EmbeddingResult; +using ::mediapipe::Packet; +} // namespace + +#define int kMicroSecondsPerMilliSecond = 1000; + +@implementation MPPTextEmbedderResult (Helpers) + ++ (MPPTextEmbedderResult *)textEmbedderResultWithOutputPacket:(const Packet &)packet { + MPPEmbeddingResult *embeddingResult = [MPPEmbeddingResult + embeddingResultWithProto:packet.Get()]; + + return [[MPPTextEmbedderResult alloc] + initWithEmbeddingResult:embeddingResult + timestampMs:(NSInteger)(packet.Timestamp().Value() / + kMicroSecondsPerMilliSecond)]; +} + +@end