# ultralytics_ndjson_parser

Python API: `luxonis_ml.data.parsers.ultralytics_ndjson_parser`

## Classes

| Name | Description |
| --- | --- |
|
[`UltralyticsNDJSONParser`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Parse Ultralytics NDJSON datasets into LDF. |

### class `UltralyticsNDJSONParser`

```python
class UltralyticsNDJSONParser(BaseParser)
```

Parse Ultralytics NDJSON datasets into LDF.

NDJSON records may carry their own split names. When no split is present on an image record, the image is assigned to `"train"`.
`"valid"` and `"validation"` are normalized to `"val"`.

An image record locates its image through two fields:

 * `file` names a local image. A relative path resolves against the directory that holds the `.ndjson` manifest. An absolute path
   is used as it is.
 * `url` names a remote image. The parser downloads it, and `file` then supplies only the cached file name. The parser appends a
   hash of the URL to that name and writes the image to a per-split cache directory.

A record that holds a `url` therefore never reads a local `file` path. One manifest can describe a fully remote dataset.

Methods

| Name | Description |
| --- | --- |
|
[`from_dir`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Parse an Ultralytics NDJSON file into dataset records. |
|
[`from_split`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Parse a single Ultralytics NDJSON file. |
|
[`parse_dir`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Parse a full NDJSON dataset and preserve record-level splits. |
|
[`parse_split`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Parse an NDJSON file that represents a single parser input. |
|
[`validate`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Validate whether the dataset directory has the expected format. |
|
[`validate_split`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/parsers/ultralytics_ndjson_parser.md)
| Validate whether a split directory has the expected format. |

#### method `from_dir`

```python
def from_dir(
    dataset_dir: Path,
    reuse_cached: bool = True,
    **kwargs,
) -> tuple[list[Path], list[Path], list[Path]]
```

Parse an Ultralytics NDJSON file into dataset records.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `dataset_dir` | `Path` | Directory containing exactly one `.ndjson` file, or a direct path to an `.ndjson` file. | required |
| `reuse_cached` | `bool` | Whether to reuse cached remote images if they already exist. | `True` |
| `**kwargs` |  | Parser-specific arguments. |  |

Returns

| Type | Description |
| --- | --- |
| `tuple[list[Path], list[Path], list[Path]]` | Added images for the train, validation, and test splits. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If no NDJSON dataset file can be resolved, the resolved file has an invalid header, a remote image download
directory already exists, or pose annotations cannot infer keypoint dimensionality. |

#### method `from_split`

```python
def from_split(ndjson_path: Path, reuse_cached: bool = True) -> ParserOutput
```

Parse a single Ultralytics NDJSON file.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `ndjson_path` | `Path` | Path to an Ultralytics NDJSON file. | required |
| `reuse_cached` | `bool` | Whether to reuse cached remote images if they already exist. | `True` |

Returns

| Type | Description |
| --- | --- |
| `ParserOutput` | Parser output containing annotation records, empty skeleton metadata, and added images. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the file has an invalid header, a remote image download directory already exists, or pose annotations cannot
infer keypoint dimensionality. |

#### method `parse_dir`

```python
def parse_dir(dataset_dir: Path, reuse_cached: bool = True, **kwargs) -> BaseDataset
```

Parse a full NDJSON dataset and preserve record-level splits.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `dataset_dir` | `Path` | Directory containing exactly one `.ndjson` file, or a direct path to an `.ndjson` file. | required |
| `reuse_cached` | `bool` | Whether to reuse cached remote images if they already exist. | `True` |
| `**kwargs` |  | Parser-specific arguments. `split_ratios` may be supplied to resample split assignments. |  |

Returns

| Type | Description |
| --- | --- |
| `BaseDataset` | Dataset with parsed images and annotations. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the NDJSON file cannot be resolved or parsed as a valid Ultralytics dataset. |

#### method `parse_split`

```python
def parse_split(
    split: str | None = None,
    random_split: bool = True,
    split_ratios: dict[str, float | int] | None = None,
    reuse_cached: bool = True,
    **kwargs,
) -> BaseDataset
```

Parse an NDJSON file that represents a single parser input.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `split` | `str | None` | Optional split name to assign to all parsed images. | `None` |
| `random_split` | `bool` | Whether percentage `split_ratios` should resample all images. | `True` |
| `split_ratios` | `dict[str, float | int] | None` | Optional ratios or counts. Float values are treated as ratios; integer values
are treated as counts. | `None` |
| `reuse_cached` | `bool` | Whether to reuse cached remote images if they already exist. | `True` |
| `**kwargs` |  | Parser-specific arguments. Must include `ndjson_path`. |  |

Returns

| Type | Description |
| --- | --- |
| `BaseDataset` | Dataset with parsed images and annotations. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If `ndjson_path` is missing, the file has an invalid header, a remote image download directory already exists, or
pose annotations cannot infer keypoint dimensionality. |

#### class method `validate`

```python
def validate(dataset_dir: Path) -> bool
```

Validate whether the dataset directory has the expected format.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `dataset_dir` | `Path` | Source dataset directory. | required |

Returns

| Type | Description |
| --- | --- |
| `bool` | Whether the dataset is in the expected format. |

#### static method `validate_split`

```python
def validate_split(split_path: Path) -> dict[str, Any] | None
```

Validate whether a split directory has the expected format.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `split_path` | `Path` | Path to a split directory. | required |

Returns

| Type | Description |
| --- | --- |
| `dict[str, Any] | None` | Keyword arguments for `from_split`, or `None` if the split is not in the expected format. |
