Merge pull request #4901 from priankakariatyml:ios-language-detector
PiperOrigin-RevId: 578990815
This commit is contained in:
commit
35010894c1
|
@ -31,3 +31,28 @@ objc_library(
|
||||||
"//mediapipe/tasks/ios/core:MPPTaskResult",
|
"//mediapipe/tasks/ios/core:MPPTaskResult",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
objc_library(
|
||||||
|
name = "MPPLanguageDetector",
|
||||||
|
srcs = ["sources/MPPLanguageDetector.mm"],
|
||||||
|
hdrs = ["sources/MPPLanguageDetector.h"],
|
||||||
|
copts = [
|
||||||
|
"-ObjC++",
|
||||||
|
"-std=c++17",
|
||||||
|
"-x objective-c++",
|
||||||
|
],
|
||||||
|
module_name = "MPPLanguageDetector",
|
||||||
|
deps = [
|
||||||
|
":MPPLanguageDetectorOptions",
|
||||||
|
":MPPLanguageDetectorResult",
|
||||||
|
"//mediapipe/tasks/cc/text/text_classifier:text_classifier_graph",
|
||||||
|
"//mediapipe/tasks/ios/common/utils:MPPCommonUtils",
|
||||||
|
"//mediapipe/tasks/ios/common/utils:NSStringHelpers",
|
||||||
|
"//mediapipe/tasks/ios/core:MPPTaskInfo",
|
||||||
|
"//mediapipe/tasks/ios/core:MPPTaskOptions",
|
||||||
|
"//mediapipe/tasks/ios/core:MPPTextPacketCreator",
|
||||||
|
"//mediapipe/tasks/ios/text/core:MPPTextTaskRunner",
|
||||||
|
"//mediapipe/tasks/ios/text/language_detector/utils:MPPLanguageDetectorOptionsHelpers",
|
||||||
|
"//mediapipe/tasks/ios/text/language_detector/utils:MPPLanguageDetectorResultHelpers",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
// 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"
|
||||||
|
#import "mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorOptions.h"
|
||||||
|
#import "mediapipe/tasks/ios/text/language_detector/sources/MPPLanguageDetectorResult.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Predicts the language of an input text.
|
||||||
|
*
|
||||||
|
* This API expects a TFLite model with [TFLite Model
|
||||||
|
* Metadata](https://www.tensorflow.org/lite/convert/metadata")that contains the mandatory
|
||||||
|
* (described below) input tensor, output tensor, and the language codes in an AssociatedFile.
|
||||||
|
*
|
||||||
|
* Metadata is required for models with int32 input tensors because it contains the input
|
||||||
|
* process unit for the model's Tokenizer. No metadata is required for models with string
|
||||||
|
* input tensors.
|
||||||
|
*
|
||||||
|
* Input tensor
|
||||||
|
* - One input tensor (`kTfLiteString`) of shape `[1]` containing the input string.
|
||||||
|
*
|
||||||
|
* Output tensor
|
||||||
|
* - One output tensor (`kTfLiteFloat32`) of shape `[1 x N]` where `N` is the number of languages.
|
||||||
|
*/
|
||||||
|
NS_SWIFT_NAME(LanguageDetector)
|
||||||
|
@interface MPPLanguageDetector : NSObject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of `LanguageDetector` from an absolute path to a TensorFlow Lite
|
||||||
|
* model file stored locally on the device and the default `LanguageDetectorOptions`.
|
||||||
|
*
|
||||||
|
* @param modelPath An absolute path to a TensorFlow Lite model file stored locally on the device.
|
||||||
|
* @param error An optional error parameter populated when there is an error in initializing the
|
||||||
|
* language detector.
|
||||||
|
*
|
||||||
|
* @return A new instance of `LanguageDetector` with the given model path. `nil` if there is an
|
||||||
|
* error in initializing the language detector.
|
||||||
|
*/
|
||||||
|
- (nullable instancetype)initWithModelPath:(NSString *)modelPath error:(NSError **)error;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of `LanguageDetector` from the given `LanguageDetectorOptions`.
|
||||||
|
*
|
||||||
|
* @param options The options of type `LanguageDetectorOptions` to use for configuring the
|
||||||
|
* `LanguageDetector`.
|
||||||
|
* @param error An optional error parameter populated when there is an error in initializing the
|
||||||
|
* language detector.
|
||||||
|
*
|
||||||
|
* @return A new instance of `LanguageDetector` with the given options. `nil` if there is an
|
||||||
|
* error in initializing the language detector.
|
||||||
|
*/
|
||||||
|
- (nullable instancetype)initWithOptions:(MPPLanguageDetectorOptions *)options
|
||||||
|
error:(NSError **)error NS_DESIGNATED_INITIALIZER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Predicts the language of the input text.
|
||||||
|
*
|
||||||
|
* @param text The `NSString` for which language is to be predicted.
|
||||||
|
* @param error An optional error parameter populated when there is an error in performing
|
||||||
|
* language prediction on the input text.
|
||||||
|
*
|
||||||
|
* @return A `LanguageDetectorResult` object that contains a list of language predictions.
|
||||||
|
*/
|
||||||
|
- (nullable MPPLanguageDetectorResult *)detectText:(NSString *)text
|
||||||
|
error:(NSError **)error NS_SWIFT_NAME(detect(text:));
|
||||||
|
|
||||||
|
- (instancetype)init NS_UNAVAILABLE;
|
||||||
|
|
||||||
|
+ (instancetype)new NS_UNAVAILABLE;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
|
@ -0,0 +1,96 @@
|
||||||
|
// 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/MPPLanguageDetector.h"
|
||||||
|
|
||||||
|
#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h"
|
||||||
|
#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h"
|
||||||
|
#import "mediapipe/tasks/ios/core/sources/MPPTaskInfo.h"
|
||||||
|
#import "mediapipe/tasks/ios/core/sources/MPPTextPacketCreator.h"
|
||||||
|
#import "mediapipe/tasks/ios/text/core/sources/MPPTextTaskRunner.h"
|
||||||
|
#import "mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorOptions+Helpers.h"
|
||||||
|
#import "mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorResult+Helpers.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
using ::mediapipe::Packet;
|
||||||
|
using ::mediapipe::tasks::core::PacketMap;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
static NSString *const kClassificationsStreamName = @"classifications_out";
|
||||||
|
static NSString *const kClassificationsTag = @"CLASSIFICATIONS";
|
||||||
|
static NSString *const kTextInStreamName = @"text_in";
|
||||||
|
static NSString *const kTextTag = @"TEXT";
|
||||||
|
static NSString *const kTaskGraphName =
|
||||||
|
@"mediapipe.tasks.text.language_detector.LanguageDetectorGraph";
|
||||||
|
|
||||||
|
@interface MPPLanguageDetector () {
|
||||||
|
/** iOS Text Task Runner */
|
||||||
|
MPPTextTaskRunner *_textTaskRunner;
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation MPPLanguageDetector
|
||||||
|
|
||||||
|
- (instancetype)initWithOptions:(MPPLanguageDetectorOptions *)options error:(NSError **)error {
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
MPPTaskInfo *taskInfo = [[MPPTaskInfo alloc]
|
||||||
|
initWithTaskGraphName:kTaskGraphName
|
||||||
|
inputStreams:@[ [NSString stringWithFormat:@"%@:%@", kTextTag, kTextInStreamName] ]
|
||||||
|
outputStreams:@[ [NSString stringWithFormat:@"%@:%@", kClassificationsTag,
|
||||||
|
kClassificationsStreamName] ]
|
||||||
|
taskOptions:options
|
||||||
|
enableFlowLimiting:NO
|
||||||
|
error:error];
|
||||||
|
|
||||||
|
if (!taskInfo) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
_textTaskRunner =
|
||||||
|
[[MPPTextTaskRunner alloc] initWithCalculatorGraphConfig:[taskInfo generateGraphConfig]
|
||||||
|
error:error];
|
||||||
|
|
||||||
|
if (!_textTaskRunner) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)initWithModelPath:(NSString *)modelPath error:(NSError **)error {
|
||||||
|
MPPLanguageDetectorOptions *options = [[MPPLanguageDetectorOptions alloc] init];
|
||||||
|
|
||||||
|
options.baseOptions.modelAssetPath = modelPath;
|
||||||
|
|
||||||
|
return [self initWithOptions:options error:error];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nullable MPPLanguageDetectorResult *)detectText:(NSString *)text error:(NSError **)error {
|
||||||
|
Packet packet = [MPPTextPacketCreator createWithText:text];
|
||||||
|
|
||||||
|
std::map<std::string, Packet> packetMap = {{kTextInStreamName.cppString, packet}};
|
||||||
|
std::optional<PacketMap> outputPacketMap = [_textTaskRunner processPacketMap:packetMap
|
||||||
|
error:error];
|
||||||
|
|
||||||
|
if (!outputPacketMap.has_value()) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
[MPPLanguageDetectorResult languageDetectorResultWithClassificationsPacket:
|
||||||
|
outputPacketMap.value()[kClassificationsStreamName.cppString]];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
|
@ -30,3 +30,15 @@ objc_library(
|
||||||
"//mediapipe/tasks/ios/text/language_detector:MPPLanguageDetectorOptions",
|
"//mediapipe/tasks/ios/text/language_detector:MPPLanguageDetectorOptions",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
objc_library(
|
||||||
|
name = "MPPLanguageDetectorResultHelpers",
|
||||||
|
srcs = ["sources/MPPLanguageDetectorResult+Helpers.mm"],
|
||||||
|
hdrs = ["sources/MPPLanguageDetectorResult+Helpers.h"],
|
||||||
|
deps = [
|
||||||
|
"//mediapipe/framework:packet",
|
||||||
|
"//mediapipe/tasks/cc/components/containers/proto:classifications_cc_proto",
|
||||||
|
"//mediapipe/tasks/ios/components/containers/utils:MPPClassificationResultHelpers",
|
||||||
|
"//mediapipe/tasks/ios/text/language_detector:MPPLanguageDetectorResult",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
#include "mediapipe/framework/packet.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface MPPLanguageDetectorResult (Helpers)
|
||||||
|
|
||||||
|
+ (MPPLanguageDetectorResult *)languageDetectorResultWithClassificationsPacket:
|
||||||
|
(const mediapipe::Packet &)packet;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
|
@ -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 "mediapipe/tasks/ios/components/containers/utils/sources/MPPClassificationResult+Helpers.h"
|
||||||
|
#import "mediapipe/tasks/ios/text/language_detector/utils/sources/MPPLanguageDetectorResult+Helpers.h"
|
||||||
|
|
||||||
|
#include "mediapipe/tasks/cc/components/containers/proto/classifications.pb.h"
|
||||||
|
|
||||||
|
static const int kMicroSecondsPerMilliSecond = 1000;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
using ClassificationResultProto =
|
||||||
|
::mediapipe::tasks::components::containers::proto::ClassificationResult;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
#define int kMicroSecondsPerMilliSecond = 1000;
|
||||||
|
|
||||||
|
@implementation MPPLanguageDetectorResult (Helpers)
|
||||||
|
|
||||||
|
+ (MPPLanguageDetectorResult *)languageDetectorResultWithClassificationsPacket:
|
||||||
|
(const mediapipe::Packet &)packet {
|
||||||
|
MPPClassificationResult *classificationResult = [MPPClassificationResult
|
||||||
|
classificationResultWithProto:packet.Get<ClassificationResultProto>()];
|
||||||
|
|
||||||
|
return [MPPLanguageDetectorResult
|
||||||
|
languageDetectorResultWithClassificationResult:classificationResult
|
||||||
|
timestampInMilliseconds:(NSInteger)(packet.Timestamp().Value() /
|
||||||
|
kMicroSecondsPerMilliSecond)];
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (MPPLanguageDetectorResult *)
|
||||||
|
languageDetectorResultWithClassificationResult:(MPPClassificationResult *)classificationResult
|
||||||
|
timestampInMilliseconds:(NSInteger)timestampInMilliseconds {
|
||||||
|
NSMutableArray<MPPLanguagePrediction *> *languagePredictions =
|
||||||
|
[NSMutableArray arrayWithCapacity:classificationResult.classifications.count];
|
||||||
|
|
||||||
|
if (classificationResult.classifications.count > 0) {
|
||||||
|
for (MPPCategory *category in classificationResult.classifications[0].categories) {
|
||||||
|
MPPLanguagePrediction *languagePrediction =
|
||||||
|
[[MPPLanguagePrediction alloc] initWithLanguageCode:category.categoryName
|
||||||
|
probability:category.score];
|
||||||
|
[languagePredictions addObject:languagePrediction];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [[MPPLanguageDetectorResult alloc] initWithLanguagePredictions:languagePredictions
|
||||||
|
timestampInMilliseconds:timestampInMilliseconds];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
Loading…
Reference in New Issue
Block a user