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 automaticaly cast into torch.uint8.

Attributes

channelsint

The numbers of layers (readonly):

  • 1 -> grayscale

  • 2 -> grayscale, alpha

  • 3 -> blue, green, red

  • 4 -> blue, green, red, 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.randint(0, 256, (480, 720, 1), dtype=torch.uint8))
>>> ref_gray_alpha = FrameVideo(0, torch.randint(0, 256, (480, 720, 2), dtype=torch.uint8))
>>> ref_bgr = FrameVideo(0, torch.randint(0, 256, (480, 720, 3), dtype=torch.uint8))
>>> ref_bgr_alpha = FrameVideo(0, torch.randint(0, 256, (480, 720, 4), dtype=torch.uint8))
>>>
>>> # 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], 255).all()
tensor(True)
>>> bgr = ref_gray.convert(3)
>>> bgr.channels
3
>>> torch.equal(bgr[..., 0], ref_gray[..., 0])
True
>>> torch.equal(bgr[..., 1], ref_gray[..., 0])
True
>>> torch.equal(bgr[..., 2], ref_gray[..., 0])
True
>>> bgr_alpha = ref_gray.convert(4)
>>> bgr_alpha.channels
4
>>> torch.equal(bgr_alpha[..., 0], ref_gray[..., 0])
True
>>> torch.equal(bgr_alpha[..., 1], ref_gray[..., 0])
True
>>> torch.equal(bgr_alpha[..., 2], ref_gray[..., 0])
True
>>> torch.eq(bgr_alpha[..., 3], 255).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
>>> bgr = ref_gray_alpha.convert(3)
>>> bgr.channels
3
>>> torch.equal(bgr[..., 0],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0), 0, ref_gray_alpha[..., 0]))
True
>>> torch.equal(bgr[..., 1],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0), 0, ref_gray_alpha[..., 0]))
True
>>> torch.equal(bgr[..., 2],
...     torch.where(torch.eq(ref_gray_alpha[..., 1], 0), 0, ref_gray_alpha[..., 0]))
True
>>> bgr_alpha = ref_gray_alpha.convert(4)
>>> bgr_alpha.channels
4
>>> torch.equal(bgr_alpha[..., 0], ref_gray_alpha[..., 0])
True
>>> torch.equal(bgr_alpha[..., 1], ref_gray_alpha[..., 0])
True
>>> torch.equal(bgr_alpha[..., 2], ref_gray_alpha[..., 0])
True
>>> torch.equal(bgr_alpha[..., 3], ref_gray_alpha[..., 1])
True
>>>
>>> # case 3 -> 1, 2, 4
>>> gray = ref_bgr.convert(1)
>>> gray.channels
1
>>> gray_alpha = ref_bgr.convert(2)
>>> gray_alpha.channels
2
>>> torch.eq(gray_alpha[..., 1], 255).all()
tensor(True)
>>> bgr_alpha = ref_bgr.convert(4)
>>> bgr_alpha.channels
4
>>> torch.equal(bgr_alpha[..., 0], ref_bgr[..., 0])
True
>>> torch.equal(bgr_alpha[..., 1], ref_bgr[..., 1])
True
>>> torch.equal(bgr_alpha[..., 2], ref_bgr[..., 2])
True
>>> torch.eq(bgr_alpha[..., 3], 255).all()
tensor(True)
>>>
>>> # case 4 -> 1, 2, 3
>>> gray = ref_bgr_alpha.convert(1)
>>> gray.channels
1
>>> gray_alpha = ref_bgr_alpha.convert(2)
>>> gray_alpha.channels
2
>>> torch.equal(gray_alpha[..., 1], ref_bgr_alpha[..., 3])
True
>>> bgr = ref_bgr_alpha.convert(3)
>>> bgr.channels
3
>>> torch.equal(bgr[..., 0],
...     torch.where(torch.eq(ref_bgr_alpha[..., 3], 0), 0, ref_bgr_alpha[..., 0]))
True
>>> torch.equal(bgr[..., 1],
...     torch.where(torch.eq(ref_bgr_alpha[..., 3], 0), 0, ref_bgr_alpha[..., 1]))
True
>>> torch.equal(bgr[..., 2],
...     torch.where(torch.eq(ref_bgr_alpha[..., 3], 0), 0, ref_bgr_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_bgr(contiguous=False) ndarray[uint8][source]

Return the 3 channels uint8 numpy frame representation.

Parameters

contiguousboolean, default=False

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

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>>
>>> # from float32
>>> frame = FrameVideo(0, torch.zeros(480, 720, 3)).to_numpy_bgr()  # classical bgr
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>> frame = FrameVideo(0, torch.zeros(480, 720, 3)).to_numpy_bgr()  # 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_bgr()  # alpha channel
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>>
>>> # from uint8
>>> frame = FrameVideo(0, torch.empty(480, 720, 3, dtype=torch.uint8)).to_numpy_bgr()
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>> frame = FrameVideo(0, torch.empty(480, 720, 3, dtype=torch.uint8)).to_numpy_bgr()
>>> type(frame), frame.shape, frame.dtype
(<class 'numpy.ndarray'>, (480, 720, 3), dtype('uint8'))
>>> frame = FrameVideo(0, torch.empty(480, 720, 3, dtype=torch.uint8)).to_numpy_bgr()
>>> 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
>>>