Added iOS helpers for classification result containers
This commit is contained in:
parent
33df6c042f
commit
89aad67a87
40
mediapipe/tasks/ios/components/containers/utils/BUILD
Normal file
40
mediapipe/tasks/ios/components/containers/utils/BUILD
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# Copyright 2023 The MediaPipe Authors. All Rights Reserved.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
package(default_visibility = ["//mediapipe/tasks:internal"])
|
||||||
|
|
||||||
|
licenses(["notice"])
|
||||||
|
|
||||||
|
objc_library(
|
||||||
|
name = "MPPCategoryHelpers",
|
||||||
|
srcs = ["sources/MPPCategory+Helpers.mm"],
|
||||||
|
hdrs = ["sources/MPPCategory+Helpers.h"],
|
||||||
|
deps = [
|
||||||
|
"//mediapipe/tasks/ios/components/containers:MPPCategory",
|
||||||
|
"//mediapipe/framework/formats:classification_cc_proto",
|
||||||
|
"//mediapipe/tasks/ios/common/utils:NSStringHelpers",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
objc_library(
|
||||||
|
name = "MPPClassificationResultHelpers",
|
||||||
|
srcs = ["sources/MPPClassificationResult+Helpers.mm"],
|
||||||
|
hdrs = ["sources/MPPClassificationResult+Helpers.h"],
|
||||||
|
deps = [
|
||||||
|
"//mediapipe/tasks/ios/components/containers:MPPClassificationResult",
|
||||||
|
":MPPCategoryHelpers",
|
||||||
|
"//mediapipe/tasks/ios/common/utils:NSStringHelpers",
|
||||||
|
"//mediapipe/tasks/cc/components/containers/proto:classifications_cc_proto",
|
||||||
|
],
|
||||||
|
)
|
|
@ -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/classification.pb.h"
|
||||||
|
#import "mediapipe/tasks/ios/components/containers/sources/MPPCategory.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface MPPCategory (Helpers)
|
||||||
|
|
||||||
|
+ (MPPCategory *)categoryWithProto:(const mediapipe::Classification &)classificationProto;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
|
@ -0,0 +1,43 @@
|
||||||
|
// 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/MPPCategory+Helpers.h"
|
||||||
|
|
||||||
|
#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
using ClassificationProto = ::mediapipe::Classification;
|
||||||
|
}
|
||||||
|
|
||||||
|
@implementation MPPCategory (Helpers)
|
||||||
|
|
||||||
|
+ (MPPCategory *)categoryWithProto:(const ClassificationProto &)clasificationProto {
|
||||||
|
NSString *categoryName;
|
||||||
|
NSString *displayName;
|
||||||
|
|
||||||
|
if (clasificationProto.has_label()) {
|
||||||
|
categoryName = [NSString stringWithCppString:clasificationProto.label()];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clasificationProto.has_display_name()) {
|
||||||
|
displayName = [NSString stringWithCppString:clasificationProto.display_name()];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [[MPPCategory alloc] initWithIndex:clasificationProto.index()
|
||||||
|
score:clasificationProto.score()
|
||||||
|
categoryName:categoryName
|
||||||
|
displayName:displayName];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
|
@ -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/tasks/cc/components/containers/proto/classifications.pb.h"
|
||||||
|
#import "mediapipe/tasks/ios/components/containers/sources/MPPClassificationResult.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface MPPClassifications (Helpers)
|
||||||
|
|
||||||
|
+ (MPPClassifications *)classificationsWithProto:
|
||||||
|
(const mediapipe::tasks::components::containers::proto::Classifications &)classificationsProto;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface MPPClassificationResult (Helpers)
|
||||||
|
|
||||||
|
+ (MPPClassificationResult *)classificationResultWithProto:
|
||||||
|
(const mediapipe::tasks::components::containers::proto::ClassificationResult &)
|
||||||
|
classificationResultProto;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
|
@ -0,0 +1,64 @@
|
||||||
|
// 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/MPPClassificationResult+Helpers.h"
|
||||||
|
|
||||||
|
#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h"
|
||||||
|
#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPCategory+Helpers.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
using ClassificationsProto = ::mediapipe::tasks::components::containers::proto::Classifications;
|
||||||
|
using ClassificationResultProto =
|
||||||
|
::mediapipe::tasks::components::containers::proto::ClassificationResult;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
@implementation MPPClassifications (Helpers)
|
||||||
|
|
||||||
|
+ (MPPClassifications *)classificationsWithProto:
|
||||||
|
(const ClassificationsProto &)classificationsProto {
|
||||||
|
NSMutableArray<MPPCategory *> *categories = [NSMutableArray arrayWithCapacity:(NSUInteger)classificationsProto.classification_list().classification_size()];
|
||||||
|
for (const auto &classification : classificationsProto.classification_list().classification()) {
|
||||||
|
[categories addObject:[MPPCategory categoryWithProto:classification]];
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *headName;
|
||||||
|
if (classificationsProto.has_head_name()) {
|
||||||
|
headName = [NSString stringWithCppString:classificationsProto.head_name()];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [[MPPClassifications alloc] initWithHeadIndex:(NSInteger)classificationsProto.head_index()
|
||||||
|
headName:headName
|
||||||
|
categories:categories];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation MPPClassificationResult (Helpers)
|
||||||
|
|
||||||
|
+ (MPPClassificationResult *)classificationResultWithProto:
|
||||||
|
(const ClassificationResultProto &)classificationResultProto {
|
||||||
|
NSMutableArray *classifications = [NSMutableArray arrayWithCapacity:(NSUInteger)classificationResultProto.classifications_size()];
|
||||||
|
for (const auto &classificationsProto : classificationResultProto.classifications()) {
|
||||||
|
[classifications addObject:[MPPClassifications classificationsWithProto:classificationsProto]];
|
||||||
|
}
|
||||||
|
|
||||||
|
NSInteger timestampMs;
|
||||||
|
if (classificationResultProto.has_timestamp_ms()) {
|
||||||
|
timestampMs = (NSInteger)classificationResultProto.timestamp_ms();
|
||||||
|
}
|
||||||
|
|
||||||
|
return [[MPPClassificationResult alloc] initWithClassifications:classifications timestampMs:timestampMs];;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
Loading…
Reference in New Issue
Block a user