Merge pull request #4851 from priankakariatyml:ios-language-detector-containers

PiperOrigin-RevId: 574954531
This commit is contained in:
Copybara-Service 2023-10-19 11:58:54 -07:00
commit 02e0ce3f87
8 changed files with 360 additions and 0 deletions

View File

@ -0,0 +1,33 @@
# 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"],
)
objc_library(
name = "MPPLanguageDetectorResult",
srcs = ["sources/MPPLanguageDetectorResult.m"],
hdrs = ["sources/MPPLanguageDetectorResult.h"],
deps = [
"//mediapipe/tasks/ios/core:MPPTaskResult",
],
)

View File

@ -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 <Foundation/Foundation.h>
#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 <NSCopying>
/**
* 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<NSString *> *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<NSString *> *categoryDenylist;
@end
NS_ASSUME_NONNULL_END

View File

@ -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

View File

@ -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 <Foundation/Foundation.h>
#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<MPPLanguagePrediction *> *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<MPPLanguagePrediction *> *)languagePredictions
timestampInMilliseconds:(NSInteger)timestampInMilliseconds;
- (instancetype)initWithTimestampInMilliseconds:(NSInteger)timestampInMilliseconds NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END

View File

@ -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<MPPLanguagePrediction *> *)languagePredictions
timestampInMilliseconds:(NSInteger)timestampInMilliseconds {
self = [super initWithTimestampInMilliseconds:timestampInMilliseconds];
if (self) {
_languagePredictions = languagePredictions;
}
return self;
}
@end

View File

@ -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",
],
)

View File

@ -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) <MPPTaskOptionsProtocol>
- (void)copyToProto:(::mediapipe::CalculatorOptions *)optionsProto;
@end
NS_ASSUME_NONNULL_END

View File

@ -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