<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L204-L325) |

Class that predicts the language of an input text.

```python
mp.tasks.text.LanguageDetector(
    lib: serial_dispatcher.SerialDispatcher, handle: ctypes.c_void_p
)
```

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.

<br />

<br />

<br />

| ## Input tensors ||
|---|---|
| (kTfLiteString) - 1 input tensor that is scalar or has shape \[1\] containing the input string. ||

<br />

<br />

<br />

<br />

| ## Output tensor ||
|---|---|
| (kTfLiteFloat32) - 1 output tensor of shape`[1 x N]` where `N` is the number of languages. ||

<br />

## Methods

### `close`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L304-L310)

```python
close()
```

Shuts down the MediaPipe task instance.

### `create_from_model_path`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L228-L249)

```python
@classmethod
create_from_model_path(
    model_path: str
) -> 'LanguageDetector'
```

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

<br />

<br />

<br />

| Args ||
|---|---|
| `model_path` | Path to the model. |

<br />

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If failed to create `LanguageDetector` object from the provided file such as invalid file path. |
| `RuntimeError` | If other types of error occurred. |

<br />

### `create_from_options`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L251-L277)

```python
@classmethod
create_from_options(
    options: `mp.tasks.text.LanguageDetectorOptions`
) -> 'LanguageDetector'
```

Creates the `LanguageDetector` object from language detector options.

<br />

<br />

<br />

| Args ||
|---|---|
| `options` | Options for the language detector task. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `LanguageDetector` object that's created from `options`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If failed to create `LanguageDetector` object from `LanguageDetectorOptions` such as missing the model. |
| `RuntimeError` | If other types of error occurred. |

<br />

### `detect`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L279-L302)

```python
detect(
    text: str
) -> `mp.tasks.text.LanguageDetectorResult`
```

Predicts the language of the input `text`.

<br />

<br />

<br />

| Args ||
|---|---|
| `text` | The input text. |

<br />

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If any of the input arguments is invalid. |
| `RuntimeError` | If language detection failed to run. |

<br />

### `__enter__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L312-L314)

```python
__enter__()
```

Returns `self` upon entering the runtime context.

### `__exit__`

[View source](https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/tasks/python/text/language_detector.py#L316-L322)

```python
__exit__(
    unused_exc_type, unused_exc_value, unused_traceback
)
```

Shuts down the MediaPipe task instance on exit of the context manager.

<br />

<br />

<br />

| Raises ||
|---|---|
| `RuntimeError` | If the MediaPipe LanguageDetector task failed to close. |

<br />