Added MPPEmbedding Helpers
This commit is contained in:
parent
8c21dc02a6
commit
24bd104b0f
|
@ -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",
|
||||
]
|
||||
)
|
||||
|
|
|
@ -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
|
|
@ -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 <memory>
|
||||
|
||||
namespace {
|
||||
using EmbeddingProto = ::mediapipe::tasks::components::containers::proto::Embedding;
|
||||
}
|
||||
|
||||
@implementation MPPEmbedding (Helpers)
|
||||
|
||||
+ (MPPEmbedding *)embeddingWithProto:(const EmbeddingProto &)embeddingProto {
|
||||
NSString *categoryName;
|
||||
NSString *displayName;
|
||||
|
||||
NSMutableArray<NSNumber *> *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
|
Loading…
Reference in New Issue
Block a user