Video Reader

class apps.expert.data.video_reader.Cache(capacity: int)[source]

Bases: object

Caching class for decoding videos.

If the same video frame is cached and used a second time, there is no need to decode it twice.

Parameters:

capacity (int) – Buffer size for storing frames.

Raises:

ValueError – If “capacity” is not a positive integer.

class apps.expert.data.video_reader.VideoReader(filename: str | PathLike, cache_capacity: int = 10)[source]

Bases: object

Class for decoding video to a list object.

This video wrapper class decodes the video and provides access to frames.

Parameters:
  • filename (str | PathLike) – Path to local video file.

  • cache_capacity (int, optional) – Buffer size for storing frames. Defaults to 10.

Raises:
  • IndexError – If the entered frame index is outside the allowed range of integer values.

  • IndexError – If the entered frame index is out of range.

  • StopIteration – If the end of the video has been reached.

property vcap: VideoCapture

Get VideoCapture object.

Returns:

Raw VideoCapture object.

Return type:

cv2.VideoCapture

property opened: bool

Check whether the video is opened.

Returns:

Indicate whether the video is opened.

Return type:

bool

property width: int

Get width of video frames.

Returns:

Width of video frames.

Return type:

int

property height: int

Get height of video frames.

Returns:

Height of video frames.

Return type:

int

property resolution: Tuple[int, int]

Get Video resolution (width, height).

Returns:

Video resolution (width, height).

Return type:

Tuple

property fps: float

Get FPS of the video.

Returns:

FPS of the video.

Return type:

float

property frame_cnt: int

Get total number frames.

Returns:

Total frames of the video.

Return type:

int

property fourcc: str

Get four character code.

Returns:

“Four character code” of the video.

Return type:

str

property position: int

Get current cursor position.

Returns:

Current cursor position, indicating frame decoded.

Return type:

int

read() np.ndarray | None[source]

Read the next frame.

If the next frame have been decoded before and in the cache, then return it directly, otherwise decode, cache and return it.

Returns:

Returns the frame if successful, otherwise returns None.

Return type:

ndarray or None

get_frame(frame_id: int) np.ndarray | None[source]

Get frame by index.

Parameters:

frame_id (int) – Index of the expected frame, 0-based.

Returns:

Returns the frame if successful, otherwise returns None.

Return type:

ndarray or None

current_frame() np.ndarray | None[source]

Get the current frame (the frame is just visited).

Returns:

If the video is fresh returns None, otherwise returns the frame.

Return type:

ndarray or None