mp.tasks.text.LanguageDetector

Class that predicts the language of an input text.

This API expects a TFLite model with TFLite Model Metadata that contains the mandatory (described below) input tensors, output tensor, and the language codes in an AssociatedFile.

(kTfLiteString)

  • 1 input tensor that is scalar or has shape [1] containing the input string.

(kTfLiteFloat32)

  • 1 output tensor of shape[1 x N] where N is the number of languages.

graph_configThe mediapipe text task graph config proto.

Methods

close

View source

Shuts down the mediapipe text task instance.

Raises
RuntimeErrorIf the mediapipe text task failed to close.

create_from_model_path

View source

Creates an LanguageDetector object from a TensorFlow Lite model and the default LanguageDetectorOptions.

Args
model_pathPath to the model.

Returns
LanguageDetector object that's created from the model file and the default LanguageDetectorOptions.

Raises
ValueErrorIf failed to create LanguageDetector object from the provided file such as invalid file path.
RuntimeErrorIf other types of error occurred.

create_from_options

View source

Creates the LanguageDetector object from language detector options.

Args
optionsOptions for the language detector task.

Returns
LanguageDetector object that's created from options.

Raises
ValueErrorIf failed to create LanguageDetector object from LanguageDetectorOptions such as missing the model.
RuntimeErrorIf other types of error occurred.

detect

View source

Predicts the language of the input text.

Args
textThe input text.

Returns
A LanguageDetectorResult object that contains a list of languages and scores.

Raises
ValueErrorIf any of the input arguments is invalid.
RuntimeErrorIf language detection failed to run.

__enter__

View source

Returns self upon entering the runtime context.

__exit__

View source

Shuts down the mediapipe text task instance on exit of the context manager.

Raises
RuntimeErrorIf the mediapipe text task failed to close.