diff --git a/mediapipe/tasks/ios/components/containers/utils/BUILD b/mediapipe/tasks/ios/components/containers/utils/BUILD index 923a8e013..e0989530e 100644 --- a/mediapipe/tasks/ios/components/containers/utils/BUILD +++ b/mediapipe/tasks/ios/components/containers/utils/BUILD @@ -38,3 +38,14 @@ objc_library( "//mediapipe/tasks/ios/components/containers:MPPClassificationResult", ], ) + +objc_library( + name = "MPPEmbeddingHelpers", + srcs = ["sources/MPPEmbedding+Helpers.mm"], + hdrs = ["sources/MPPEmbedding+Helpers.h"], + deps = [ + "//mediapipe/tasks/cc/components/containers/proto:embeddings_cc_proto", + "//mediapipe/tasks/ios/common/utils:NSStringHelpers", + "//mediapipe/tasks/ios/components/containers:MPPEmbedding", + ] +) diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+Helpers.h b/mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+Helpers.h new file mode 100644 index 000000000..9ff5455d9 --- /dev/null +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+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/tasks/cc/components/containers/proto/embeddings.pb.h" +#import "mediapipe/tasks/ios/components/containers/sources/MPPEmbedding.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MPPEmbedding (Helpers) + ++ (MPPEmbedding *)embeddingWithProto:(const ::mediapipe::tasks::components::containers::proto::Embedding &)embeddingProto; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+Helpers.mm b/mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+Helpers.mm new file mode 100644 index 000000000..4b2acff7c --- /dev/null +++ b/mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+Helpers.mm @@ -0,0 +1,62 @@ +// 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/utils/sources/NSString+Helpers.h" +#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPEmbedding+Helpers.h" + +#include + +namespace { +using EmbeddingProto = ::mediapipe::tasks::components::containers::proto::Embedding; +} + +@implementation MPPEmbedding (Helpers) + ++ (MPPEmbedding *)embeddingWithProto:(const EmbeddingProto &)embeddingProto { + NSString *categoryName; + NSString *displayName; + + NSMutableArray *floatEmbedding; + NSData *quantizedEmbedding; + + if (embeddingProto.has_float_embedding()) { + floatEmbedding = [NSMutableArray arrayWithCapacity:embeddingProto.float_embedding().values_size()]; + const auto floatEmbeddingValues = embeddingProto.float_embedding().values(); + + for (const auto value : embeddingProto.float_embedding().values()) { + [floatEmbedding addObject:[NSNumber numberWithFloat:value]]; + } + } + + if (embeddingProto.has_quantized_embedding()) { + const std::string& cppQuantizedEmbedding = + embeddingProto.quantized_embedding().values().data(); + + const char *cppQuantizedEmbeddingCString = cppQuantizedEmbedding.c_str(); + quantizedEmbedding = [NSData dataWithBytes:cppQuantizedEmbeddingCString length:sizeof(cppQuantizedEmbeddingCString)]; + } + + NSString *headName; + + if (embeddingProto.has_head_name()) { + headName = [NSString stringWithCppString:embeddingProto.head_name()]; + } + + return [[MPPEmbedding alloc] initWithFloatEmbedding:floatEmbedding + quantizedEmbedding:quantizedEmbedding + headIndex:embeddingProto.head_index() + headName:headName]; +} + +@end