Language Detector

The MediaPipe Language Detector task lets you identify the language of a piece of text. This task operates on text data with a machine learning (ML) model and outputs a list of predictions, where each prediction consists of an ISO 639-1 language code and a probability.

Quickstart

Install the SDK for your platform, initialize the task with a model, and run inference.

Web

npmnpm install @mediapipe/tasks-textWeb demo·Edit in StackBlitz·Setup guide
import { FilesetResolver, LanguageDetector } from "@mediapipe/tasks-text";

const MODEL_URL = "https://storage.googleapis.com/mediapipe-models/language_detector/language_detector/float32/1/language_detector.tflite";

const textFiles = await FilesetResolver.forTextTasks(
  "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-text@latest/wasm"
);
const languageDetector = await LanguageDetector.createFromOptions(textFiles, {
  baseOptions: { modelAssetPath: MODEL_URL },
});

const inputText = "Bonjour tout le monde, comment allez-vous ?";
const detectionResult = languageDetector.detect(inputText);

Android (Kotlin)

Gradleimplementation("com.google.mediapipe:tasks-text:latest.release")Sample code·Setup guide
import com.google.mediapipe.tasks.core.BaseOptions
import com.google.mediapipe.tasks.text.languagedetector.LanguageDetector
import com.google.mediapipe.tasks.text.languagedetector.LanguageDetector.LanguageDetectorOptions

val baseOptions = BaseOptions.builder()
    .setModelAssetPath("language_detector.tflite")
    .build()
val options = LanguageDetectorOptions.builder()
    .setBaseOptions(baseOptions)
    .setScoreThreshold(0.5f)
    .build()

val languageDetector = LanguageDetector.createFromOptions(context, options)

val inputText = "Bonjour tout le monde, comment allez-vous ?"
val result = languageDetector.detect(inputText)

Android (Java)

Gradleimplementation 'com.google.mediapipe:tasks-text:latest.release'Sample code·Setup guide
import com.google.mediapipe.tasks.core.BaseOptions;
import com.google.mediapipe.tasks.text.languagedetector.LanguageDetector;
import com.google.mediapipe.tasks.text.languagedetector.LanguageDetector.LanguageDetectorOptions;
import com.google.mediapipe.tasks.text.languagedetector.LanguageDetectorResult;

BaseOptions baseOptions = BaseOptions.builder()
    .setModelAssetPath("language_detector.tflite")
    .build();
LanguageDetectorOptions options = LanguageDetectorOptions.builder()
    .setBaseOptions(baseOptions)
    .setScoreThreshold(0.5f)
    .build();

LanguageDetector languageDetector = LanguageDetector.createFromOptions(context, options);

String inputText = "Bonjour tout le monde, comment allez-vous ?";
LanguageDetectorResult result = languageDetector.detect(inputText);

iOS

SwiftPMhttps://github.com/google-ai-edge/mediapipe → MediaPipeTasksTextPackage.swift·Sample code·Setup guide
import Foundation
import MediaPipeTasksText

guard let modelPath = Bundle.main.path(forResource: "language_detector", ofType: "tflite") else { return }

let options = LanguageDetectorOptions()
options.baseOptions.modelAssetPath = modelPath
options.scoreThreshold = 0.5

let languageDetector = try LanguageDetector(options: options)

let inputText = "Bonjour tout le monde, comment allez-vous ?"
let result = try languageDetector.detect(text: inputText)

Python

pippip install mediapipeSample code·Setup guide
import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
LanguageDetector = mp.tasks.text.LanguageDetector
LanguageDetectorOptions = mp.tasks.text.LanguageDetectorOptions

model_path = "language_detector.tflite"
options = LanguageDetectorOptions(base_options=BaseOptions(model_asset_path=model_path), score_threshold=0.5)

input_text = "Bonjour tout le monde, comment allez-vous ?"
with LanguageDetector.create_from_options(options) as detector:
    detection_result = detector.detect(input_text)

Task details

This section describes the capabilities, inputs, outputs, and configuration options of this task.

Features

  • Score threshold - Filter results based on prediction scores
  • Label allowlist and denylist - Specify the categories detected
Inputs
Language Detector accepts the following input data type:
  • String
Outputs
The Language Detector outputs a list of predictions containing:
  • Language code: An ISO 639-1 language / locale code (e.g. "en" for English, "uz" for Uzbek, "ja-Latn" for Japanese (romaji)) as a string.
  • Probability: the confidence score for this prediction, expressed as a probability between zero and one as floating point value.

Configuration options

This task has the following configuration options:

Web

Option Name Description Value Range Default Value
displayNamesLocale Locale code for display names provided in the model metadata, if available. Locale code "en"
maxResults Maximum number of top-scored language predictions to return (-1 returns all results). Integer -1
scoreThreshold Prediction score threshold overriding the model metadata default. Predictions lower than this score are rejected. Float [0.0, 1.0] Not set
categoryAllowlist Optional list of allowed BCP 47 language codes. Mutually exclusive with categoryDenylist. List of strings Not set
categoryDenylist Optional list of excluded BCP 47 language codes. Mutually exclusive with categoryAllowlist. List of strings Not set

Android

Option Name Description Value Range Default Value
displayNamesLocale Locale code for display names provided in the model metadata, if available. Locale code "en"
maxResults Maximum number of top-scored language predictions to return (-1 returns all results). Integer -1
scoreThreshold Prediction score threshold overriding the model metadata default. Predictions lower than this score are rejected. Float [0.0, 1.0] Not set
categoryAllowlist Optional list of allowed BCP 47 language codes. Mutually exclusive with categoryDenylist. List of strings Not set
categoryDenylist Optional list of excluded BCP 47 language codes. Mutually exclusive with categoryAllowlist. List of strings Not set

iOS

Option Name Description Value Range Default Value
displayNamesLocale Locale code for display names provided in the model metadata, if available. Locale code "en"
maxResults Maximum number of top-scored language predictions to return (-1 returns all results). Integer -1
scoreThreshold Prediction score threshold overriding the model metadata default. Predictions lower than this score are rejected. Float [0.0, 1.0] Not set
categoryAllowlist Optional list of allowed BCP 47 language codes. Mutually exclusive with categoryDenylist. List of strings Not set
categoryDenylist Optional list of excluded BCP 47 language codes. Mutually exclusive with categoryAllowlist. List of strings Not set

Python

Option Name Description Value Range Default Value
display_names_locale Locale code for display names provided in the model metadata, if available. Locale code "en"
max_results Maximum number of top-scored language predictions to return (-1 returns all results). Integer -1
score_threshold Prediction score threshold overriding the model metadata default. Predictions lower than this score are rejected. Float [0.0, 1.0] Not set
category_allowlist Optional list of allowed BCP 47 language codes. Mutually exclusive with category_denylist. List of strings Not set
category_denylist Optional list of excluded BCP 47 language codes. Mutually exclusive with category_allowlist. List of strings Not set

Task output

Each LanguageDetectorResult returns a list of languages (LanguagePrediction) ordered by descending probability:

Field Type / Range Description
languageCode string (BCP-47 / ISO 639-1) Predicted language code, such as "fr", "en", "ja", or "und" when below threshold (language_code in Python).
probability float ([0.0, 1.0]) Confidence score for the predicted language (score on Android).

The following example shows the language prediction for "Il y a beaucoup de bouches qui parlent et fort peu de têtes qui pensent.":

{
  languages: [
    { languageCode: "fr", probability: 0.9998 }
  ]
}

Models

Language Detector pipeline overview

We offer a default, recommended model when you start developing with this task.

This model is built to be lightweight (315 KB) and uses embedding-based, neural network classification architecture. The model identifies language using an ISO 639-1 language code, and can identify 110 languages. For a list of languages supported by the model, see the label file, which lists languages by their ISO 639-1 code.

Model name Input shape Quantization type Model card Versions
Language Detector string UTF-8 none (float32) info Latest

Task benchmarks

Here are the task benchmarks for the whole pipeline based on the preceding pre-trained models. The latency result is the average latency on Pixel 6 using CPU / GPU.

Model name Device CPU latency GPU latency
Language Detector Pixel 6 0.31ms -