cutcutcodec.core.classes.frame_video.FrameVideo

class cutcutcodec.core.classes.frame_video.FrameVideo(time: Fraction | Real | str, data: Tensor | ndarray | Container, **kwargs)[source]

An image with time information for video context.

Behaves like a torch tensor of shape (height, width, channels). The shape is consistent with pyav and cv2. The dtype is always float32.

Attributes

channelsint

The numbers of layers (readonly):

  • 1 -> grayscale

  • 2 -> grayscale, alpha

  • 3 -> red, green, blue

  • 4 -> red, green, blue, alpha

heightint

The dimension i (vertical) of the image in pxl (readonly).

timeFraction

The time of the frame inside the video stream in second (readonly).

widthint

The dimension j (horizontal) of the image in pxl (readonly).

Construct a video frame and normalize the type.

Parameters

timeFraction

The time of the frame inside the video stream in second

dataarraylike

Transmitted to cutcutcodec.core.classes.frame.Frame initialisator.

**kwargsdict

Transmitted to cutcutcodec.core.classes.frame.Frame initialisator.

property channels: int

Return the numbers of layers.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> FrameVideo(0, torch.empty(480, 720, 3)).channels
3
>>>
check_state() None[source]

Apply verifications.

Raises

AssertionError

If something wrong in this frame.

convert(channels: int) Frame[source]

Change the numbers of channels of the frame.

Returns

framecutcutcodec.core.classes.frame_video.FrameVideo

The new frame, be carfull, undergroud data can be shared.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> _ = torch.manual_seed(0)
>>> ref_gray = FrameVideo(0, torch.rand((480, 720, 1)))
>>> ref_gray_alpha = FrameVideo(0, torch.rand((480, 720, 2)))
>>> ref_rgb = FrameVideo(0, torch.rand((480, 720, 3)))
>>> ref_rgb_alpha = FrameVideo(0, torch.rand((480, 720, 4)))
>>>
>>> # case 1 -> 2, 3, 4
>>> gray_alpha = ref_gray.convert(2)
>>> gray_alpha.channels
2
>>> torch.equal(gray_alpha[..., 0], ref_gray[..., 0])
True
>>> torch.eq(gray_alpha[..., 1], 1.0).all()
tensor(True)
>>> rgb = ref_gray.convert(3)
>>> rgb.channels
3
>>> torch.equal(rgb[..., 0], ref_gray[..., 0])
True
>>> torch.equal(rgb[..., 1], ref_gray[..., 0])
True
>>> torch.equal(rgb[..., 2], ref_gray[..., 0])
True
>>> rgb_alpha = ref_gray.convert(4)
>>> rgb_alpha.channels
4
>>> torch.equal(rgb_alpha[..., 0], ref_gray[..., 0])
True
>>> torch.equal(rgb_alpha[..., 1], ref_gray[..., 0])
True
>>> torch.equal(rgb_alpha[..., 2], ref_gray[..., 0])
True
>>> torch.eq(rgb_alpha[..., 3], 1.0).all()
tensor(True)
>>>
>>> # case 2 -> 1, 3, 4
>>> gray = ref_gray_alpha.convert(1)
>>> gray.channels
1
>>> torch.equal(gray[..., 0],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0.), 0., ref_gray_alpha[..., 0]))
True
>>> rgb = ref_gray_alpha.convert(3)
>>> rgb.channels
3
>>> torch.equal(rgb[..., 0],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0.), 0., ref_gray_alpha[..., 0]))
True
>>> torch.equal(rgb[..., 1],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0.), 0., ref_gray_alpha[..., 0]))
True
>>> torch.equal(rgb[..., 2],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0.), 0., ref_gray_alpha[..., 0]))
True
>>> rgb_alpha = ref_gray_alpha.convert(4)
>>> rgb_alpha.channels
4
>>> torch.equal(rgb_alpha[..., 0], ref_gray_alpha[..., 0])
True
>>> torch.equal(rgb_alpha[..., 1], ref_gray_alpha[..., 0])
True
>>> torch.equal(rgb_alpha[..., 2], ref_gray_alpha[..., 0])
True
>>> torch.equal(rgb_alpha[..., 3], ref_gray_alpha[..., 1])
True
>>>
>>> # case 3 -> 1, 2, 4
>>> gray = ref_rgb.convert(1)
>>> gray.channels
1
>>> gray_alpha = ref_rgb.convert(2)
>>> gray_alpha.channels
2
>>> torch.eq(gray_alpha[..., 1], 1.0).all()
tensor(True)
>>> rgb_alpha = ref_rgb.convert(4)
>>> rgb_alpha.channels
4
>>> torch.equal(rgb_alpha[..., 0], ref_rgb[..., 0])
True
>>> torch.equal(rgb_alpha[..., 1], ref_rgb[..., 1])
True
>>> torch.equal(rgb_alpha[..., 2], ref_rgb[..., 2])
True
>>> torch.eq(rgb_alpha[..., 3], 1.0).all()
tensor(True)
>>>
>>> # case 4 -> 1, 2, 3
>>> gray = ref_rgb_alpha.convert(1)
>>> gray.channels
1
>>> gray_alpha = ref_rgb_alpha.convert(2)
>>> gray_alpha.channels
2
>>> torch.equal(gray_alpha[..., 1], ref_rgb_alpha[..., 3])
True
>>> rgb = ref_rgb_alpha.convert(3)
>>> rgb.channels
3
>>> torch.equal(rgb[..., 0],
...     torch.where(torch.eq(ref_rgb_alpha[..., 3], 0.), 0., ref_rgb_alpha[..., 0]))
True
>>> torch.equal(rgb[..., 1],
...     torch.where(torch.eq(ref_rgb_alpha[..., 3], 0.), 0., ref_rgb_alpha[..., 1]))
True
>>> torch.equal(rgb[..., 2],
...     torch.where(torch.eq(ref_rgb_alpha[..., 3], 0.), 0., ref_rgb_alpha[..., 2]))
True
>>>
property height: int

Return the dimension i (vertical) of the image in pxl.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> FrameVideo(0, torch.empty(480, 720, 3)).height
480
>>>
property time: Fraction

Return the time of the frame inside the video stream in second.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> FrameVideo(0, torch.empty(480, 720, 3)).time
Fraction(0, 1)
>>>
to_numpy_uint8(contiguous=True) ndarray[uint8][source]

Return the 3 channels uint8 numpy frame representation.

The colorspace is not changed, apply cutcutcodec.core.filter.video.colorspace.FilterVideoColorspace before if you want to control it.

Parameters

contiguousboolean, default=True

If True, guaranti that the returned numpy array is c-contiguous.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>>
>>> frame = FrameVideo(0, torch.zeros(480, 720, 3)).to_numpy_uint8()  # classical rgb
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>> frame = FrameVideo(0, torch.zeros(480, 720, 3)).to_numpy_uint8()  # grayscale
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>> frame = FrameVideo(0, torch.zeros(480, 720, 3)).to_numpy_uint8()  # alpha channel
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>>
property width: int

Return the dimension j (horizontal) of the image in pxl.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> FrameVideo(0, torch.empty(480, 720, 3)).width
720
>>>