Skip to content

pixano.data.item.item_view

ItemView(**data)

Bases: BaseModel

View type for DatasetItem

Attributes:

Name Type Description
id str

View ID

type str

View type ("image", "video", "point_cloud")

url str

View URI

thumbnail str

View thumbnail as base 64 URL

frame_number int

View frame number

total_frames int

View total frames

features dict[str, ItemFeature]

View features

Raises ValidationError if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            category=None,
        )

from_pyarrow(table, schema, media_dir, media_features=False) staticmethod

Create dictionary of ItemView from PyArrow Table

Parameters:

Name Type Description Default
table dict[str, Any]

PyArrow table

required
schema schema

PyArrow schema

required
media_dir Path

Dataset media directory

required
media_features bool

Load media features like image width and height (slow for large item batches)

False

Returns:

Type Description
dict[ItemView]

Dictionary of ItemView

Source code in pixano/data/item/item_view.py
@staticmethod
def from_pyarrow(
    table: pa.Table,
    schema: pa.schema,
    media_dir: Path,
    media_features: bool = False,
) -> dict[str, "ItemView"]:
    """Create dictionary of ItemView from PyArrow Table

    Args:
        table (dict[str, Any]): PyArrow table
        schema (pa.schema): PyArrow schema
        media_dir (Path): Dataset media directory
        media_features (bool, optional): Load media features like image width and height (slow for large item batches)

    Returns:
        dict[ItemView]: Dictionary of ItemView
    """

    # NOTE: Potential change to flattened view fields with one row per view
    item = table.to_pylist()[0] if len(table.to_pylist()) > 0 else None
    views = {}

    # Iterate on fields
    for field in schema:
        # Image
        if is_image_type(field.type):
            if item is not None:
                im = (
                    item[field.name]
                    if isinstance(item[field.name], Image)
                    else Image.from_dict(item[field.name])
                )
                im.uri_prefix = media_dir.absolute().as_uri()
                api_uri = (
                    (media_dir / im.uri).get_presigned_url()
                    if isinstance(media_dir, S3Path)
                    else f"data/{media_dir.parent.name}/media/{im.uri}"
                )
                image_view = ItemView(
                    id=field.name,
                    type="image",
                    uri=api_uri,
                    thumbnail=im.preview_url,
                )
                image_view.features = {}
                if media_features:
                    image_view.features["width"] = ItemFeature(
                        name="width",
                        dtype="int",
                        value=im.width,
                    )

                    image_view.features["height"] = ItemFeature(
                        name="height",
                        dtype="int",
                        value=im.height,
                    )
                views[field.name] = image_view
            else:
                views[field.name] = ItemView(id=field.name, type="image", uri="")

        # NOTE: Future support for videos and 3D point clouds

    return views