From 3c13e4b6d6220091659d52b88507ec3b1b67b17d Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 3 Oct 2023 19:20:33 +0530 Subject: [PATCH 1/3] Added iOS language detector options --- .../tasks/ios/text/language_detector/BUILD | 27 ++++++++ .../sources/MPPLanguageDetectorOptions.h | 61 +++++++++++++++++++ .../sources/MPPLanguageDetectorOptions.m | 40 ++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 mediapipe/tasks/ios/text/language_detector/BUILD create mode 100644 mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.h create mode 100644 mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.m diff --git a/mediapipe/tasks/ios/text/language_detector/BUILD b/mediapipe/tasks/ios/text/language_detector/BUILD new file mode 100644 index 000000000..7e9d6e68b --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/BUILD @@ -0,0 +1,27 @@ +# 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. + +package(default_visibility = [ + "//mediapipe/tasks:internal", +]) + +licenses(["notice"]) + +objc_library( + name = "MPPLanguageDetectorOptions", + srcs = ["sources/MPPLanguageDetectorOptions.m"], + hdrs = ["sources/MPPLanguageDetectorOptions.h"], + deps = ["//mediapipe/tasks/ios/core:MPPTaskOptions"], +) + diff --git a/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.h b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.h new file mode 100644 index 000000000..9674c482b --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.h @@ -0,0 +1,61 @@ +// 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 + +#import "mediapipe/tasks/ios/core/sources/MPPTaskOptions.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + * Options for setting up a `LanguageDetector`. + */ +NS_SWIFT_NAME(LanguageDetectorOptions) +@interface MPPLanguageDetectorOptions : MPPTaskOptions + +/** + * The locale to use for display names specified through the TFLite Model Metadata, if any. Defaults + * to English. + */ +@property(nonatomic, copy) NSString *displayNamesLocale; + +/** + * The maximum number of top-scored classification results to return. If < 0, all available results + * will be returned. If 0, an invalid argument error is returned. + */ +@property(nonatomic) NSInteger maxResults; + +/** + * Score threshold to override the one provided in the model metadata (if any). Results below this + * value are rejected. + */ +@property(nonatomic) float scoreThreshold; + +/** + * The allowlist of category names. If non-empty, detection results whose category name is not in + * this set will be filtered out. Duplicate or unknown category names are ignored. Mutually + * exclusive with categoryDenylist. + */ +@property(nonatomic, copy) NSArray *categoryAllowlist; + +/** + * The denylist of category names. If non-empty, detection results whose category name is in this + * set will be filtered out. Duplicate or unknown category names are ignored. Mutually exclusive + * with categoryAllowlist. + */ +@property(nonatomic, copy) NSArray *categoryDenylist; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.m b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.m new file mode 100644 index 000000000..df36493ef --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.m @@ -0,0 +1,40 @@ +// 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/language_detector/sources/MPPLanguageDetectorOptions.h" + +@implementation MPPLanguageDetectorOptions + +- (instancetype)init { + self = [super init]; + if (self) { + _maxResults = -1; + _scoreThreshold = 0; + } + return self; +} + +- (id)copyWithZone:(NSZone *)zone { + MPPLanguageDetectorOptions *languageDetectorOptions = [super copyWithZone:zone]; + + languageDetectorOptions.scoreThreshold = self.scoreThreshold; + languageDetectorOptions.maxResults = self.maxResults; + languageDetectorOptions.categoryDenylist = self.categoryDenylist; + languageDetectorOptions.categoryAllowlist = self.categoryAllowlist; + languageDetectorOptions.displayNamesLocale = self.displayNamesLocale; + + return languageDetectorOptions; +} + +@end From 38de7493dfcd6ccfd726d5dc423471f7d211f6bd Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 3 Oct 2023 19:20:45 +0530 Subject: [PATCH 2/3] Added iOS language detector results --- .../tasks/ios/text/language_detector/BUILD | 8 +++ .../sources/MPPLanguageDetectorResult.h | 70 +++++++++++++++++++ .../sources/MPPLanguageDetectorResult.m | 41 +++++++++++ 3 files changed, 119 insertions(+) create mode 100644 mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.h create mode 100644 mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.m diff --git a/mediapipe/tasks/ios/text/language_detector/BUILD b/mediapipe/tasks/ios/text/language_detector/BUILD index 7e9d6e68b..cf2fbacb6 100644 --- a/mediapipe/tasks/ios/text/language_detector/BUILD +++ b/mediapipe/tasks/ios/text/language_detector/BUILD @@ -25,3 +25,11 @@ objc_library( deps = ["//mediapipe/tasks/ios/core:MPPTaskOptions"], ) +objc_library( + name = "MPPLanguageDetectorResult", + srcs = ["sources/MPPLanguageDetectorResult.m"], + hdrs = ["sources/MPPLanguageDetectorResult.h"], + deps = [ + "//mediapipe/tasks/ios/core:MPPTaskResult", + ], +) diff --git a/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.h b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.h new file mode 100644 index 000000000..a8b9fe735 --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.h @@ -0,0 +1,70 @@ +// 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 +#import "mediapipe/tasks/ios/core/sources/MPPTaskResult.h" + +NS_ASSUME_NONNULL_BEGIN + +NS_SWIFT_NAME(LanguagePrediction) +@interface MPPLanguagePrediction : NSObject + +/** The i18n language / locale code for the prediction. */ +@property(nonatomic, readonly) NSString *languageCode; + +/** The probability for the prediction. */ +@property(nonatomic, readonly) float probability; + +/** + * Initializes a new `LanguagePrediction` with the given language code and probability. + * + * @param languageCode The i18n language / locale code for the prediction. + * @param probability The probability for the prediction. + * + * @return An instance of `LanguagePrediction` initialized with the given language code and + * probability. + */ +- (instancetype)initWithLanguageCode:(NSString *)languageCode probability:(float)probability; + +@end + +/** Represents the results generated by `LanguageDetector`. **/ +NS_SWIFT_NAME(LanguageDetectorResult) +@interface MPPLanguageDetectorResult : MPPTaskResult + +/** A list of language predictions. */ +@property(nonatomic, readonly) NSArray *languagePredictions; + +/** + * Initializes a new `LanguageDetectorResult` with the given array of language predictions and + * timestamp (in milliseconds). + * + * @param languagePrediction The array of language predictions in this result. + * @param timestampInMilliseconds The timestamp (in milliseconds) for this result. + * + * @return An instance of `LanguageDetectorResult` initialized with the given array of language + * predictions and timestamp (in milliseconds). + */ +- (instancetype)initWithLanguagePredictions:(NSArray *)languagePredictions + timestampInMilliseconds:(NSInteger)timestampInMilliseconds; + +- (instancetype)initWithTimestampInMilliseconds:(NSInteger)timestampInMilliseconds NS_UNAVAILABLE; + +- (instancetype)init NS_UNAVAILABLE; + ++ (instancetype)new NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.m b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.m new file mode 100644 index 000000000..126cf6c67 --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.m @@ -0,0 +1,41 @@ +// 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/language_detector/sources/MPPLanguageDetectorResult.h" + +@implementation MPPLanguagePrediction + +- (instancetype)initWithLanguageCode:(NSString *)languageCode probability:(float)probability { + self = [super init]; + if (self) { + _languageCode = languageCode; + _probability = probability; + } + return self; +} + +@end + +@implementation MPPLanguageDetectorResult + +- (instancetype)initWithLanguagePredictions:(NSArray *)languagePredictions + timestampInMilliseconds:(NSInteger)timestampInMilliseconds { + self = [super initWithTimestampInMilliseconds:timestampInMilliseconds]; + if (self) { + _languagePredictions = languagePredictions; + } + return self; +} + +@end From 0ee9b7f86ef69553e2629a3e7928401404e27f75 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Tue, 3 Oct 2023 19:21:05 +0530 Subject: [PATCH 3/3] Added iOS language detector options helpers --- .../ios/text/language_detector/utils/BUILD | 32 +++++++++++ .../MPPLanguageDetectorOptions+Helpers.h | 27 +++++++++ .../MPPLanguageDetectorOptions+Helpers.mm | 56 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 mediapipe/tasks/ios/text/language_detector/utils/BUILD create mode 100644 mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.h create mode 100644 mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.mm diff --git a/mediapipe/tasks/ios/text/language_detector/utils/BUILD b/mediapipe/tasks/ios/text/language_detector/utils/BUILD new file mode 100644 index 000000000..00a37e940 --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/utils/BUILD @@ -0,0 +1,32 @@ +# 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. + +package(default_visibility = ["//mediapipe/tasks:internal"]) + +licenses(["notice"]) + +objc_library( + name = "MPPLanguageDetectorOptionsHelpers", + srcs = ["sources/MPPLanguageDetectorOptions+Helpers.mm"], + hdrs = ["sources/MPPLanguageDetectorOptions+Helpers.h"], + deps = [ + "//mediapipe/framework:calculator_options_cc_proto", + "//mediapipe/tasks/cc/components/processors/proto:classifier_options_cc_proto", + "//mediapipe/tasks/cc/text/text_classifier/proto:text_classifier_graph_options_cc_proto", + "//mediapipe/tasks/ios/common/utils:NSStringHelpers", + "//mediapipe/tasks/ios/core:MPPTaskOptionsProtocol", + "//mediapipe/tasks/ios/core/utils:MPPBaseOptionsHelpers", + "//mediapipe/tasks/ios/text/language_detector:MPPLanguageDetectorOptions", + ], +) diff --git a/mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.h b/mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.h new file mode 100644 index 000000000..5406e901d --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.h @@ -0,0 +1,27 @@ +// 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/calculator_options.pb.h" +#import "mediapipe/tasks/ios/core/sources/MPPTaskOptionsProtocol.h" +#import "mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MPPLanguageDetectorOptions (Helpers) + +- (void)copyToProto:(::mediapipe::CalculatorOptions *)optionsProto; + +@end + +NS_ASSUME_NONNULL_END diff --git a/mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.mm b/mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.mm new file mode 100644 index 000000000..9d75105b4 --- /dev/null +++ b/mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+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/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.h" + +#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h" +#import "mediapipe/tasks/ios/core/utils/sources/MPPBaseOptions+Helpers.h" + +#include "mediapipe/tasks/cc/components/processors/proto/classifier_options.pb.h" +#include "mediapipe/tasks/cc/text/text_classifier/proto/text_classifier_graph_options.pb.h" + +namespace { +using CalculatorOptionsProto = ::mediapipe::CalculatorOptions; +using TextClassifierGraphOptionsProto = + ::mediapipe::tasks::text::text_classifier::proto::TextClassifierGraphOptions; +using ClassifierOptionsProto = ::mediapipe::tasks::components::processors::proto::ClassifierOptions; +} // namespace + +@implementation MPPLanguageDetectorOptions (Helpers) + +- (void)copyToProto:(CalculatorOptionsProto *)optionsProto { + TextClassifierGraphOptionsProto *graphOptions = + optionsProto->MutableExtension(TextClassifierGraphOptionsProto::ext); + [self.baseOptions copyToProto:graphOptions->mutable_base_options()]; + + ClassifierOptionsProto *classifierOptionsProto = graphOptions->mutable_classifier_options(); + classifierOptionsProto->Clear(); + + if (self.displayNamesLocale) { + classifierOptionsProto->set_display_names_locale(self.displayNamesLocale.cppString); + } + + classifierOptionsProto->set_max_results((int)self.maxResults); + classifierOptionsProto->set_score_threshold(self.scoreThreshold); + + for (NSString *category in self.categoryAllowlist) { + classifierOptionsProto->add_category_allowlist(category.cppString); + } + + for (NSString *category in self.categoryDenylist) { + classifierOptionsProto->add_category_denylist(category.cppString); + } +} + +@end