Video

Interface to support treating videos like arrays using OpenCV

pydantic model VideoJsonDict[source]

Json-able roundtrip representation of a video file

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.

Fields:
field file: str [Required]
field type: Literal['video'] [Required]
to_array_input() VideoProxy[source]

Construct a VideoProxy

class VideoProxy(path: Path | None = None, video: VideoCapture | None = None)[source]

Passthrough proxy class to interact with videos as arrays

classmethod proxy_for() type[VideoInterface][source]

Declare this class as a proxy for the VideoInterface

property video: VideoCapture

Opened video capture object

close() None[source]

Close the opened VideoCapture object

property sample_frame: ndarray

A stored frame from the video to use when calculating shape and dtype

property shape: tuple[int, ...]

Shape of video like (n_frames, height, width, channels)

Note that this order flips the order of height and width from typical resolution specifications: eg. 1080p video is typically 1920x1080, but here it would be 1080x1920. This follows opencv’s ordering, which matches expectations when eg. an image is read and plotted with matplotlib: the first index is the position in the 0th dimension - the height, or “y” axis - and the second is the width/x.

property dtype: dtype

Numpy dtype (from sample_frame )

property n_frames: int

Try to get number of frames using opencv metadata, and manually count if no t

__array__() ndarray[source]

Whole video as a numpy array

__eq__(other: VideoProxy) bool[source]

Check if this is a proxy to the same video file

__len__() int[source]

Number of frames in the video

class VideoInterface(shape: tuple[int, ...] | Any = typing.Any, dtype: str | type | Any | generic = typing.Any)[source]

OpenCV interface to treat videos as arrays.

return_type

alias of VideoProxy

json_model

alias of VideoJsonDict

classmethod enabled() bool[source]

Check if opencv-python is available in the environment

classmethod check(array: Any) bool[source]

Check if array is a string or Path with a supported video extension, or an opened VideoCapture object

before_validation(array: Any) VideoProxy[source]

Get a VideoProxy object for this video

classmethod to_json(array: VideoProxy, info: SerializationInfo) list | VideoJsonDict[source]

Return a json-representation of a video