Helper Functions that work on individual frames

Convert Image to Other Formats

_, x = cv2.VideoCapture('files/interstellar-waves-edit.mp4').read()
type(x)
x.shape
numpy.ndarray
(480, 720, 3)

as_tensor[source]

as_tensor(image, dtype:Union[FloatTensor, HalfTensor, NoneType]='FloatTensor', normalise:bool=False)

Convert numpy array to torch tensor

type(as_tensor(x))
as_tensor(x, torch.HalfTensor).dtype
as_tensor(x).shape
as_tensor(x).mean(), as_tensor(x, normalise=True).mean()
torch.Tensor
torch.float16
torch.Size([480, 720, 3])
(tensor(36.6933), tensor(0.1439))

bgr2rgb[source]

bgr2rgb(x:ndarray)

convert cv2 generated image array from BGR to RGB

type(bgr2rgb(x))
imshow(bgr2rgb(x))
numpy.ndarray
<matplotlib.image.AxesImage at 0x132c83940>

bgr2hsv[source]

bgr2hsv(x:ndarray)

convert cv2 generated image array from BGR to HSV

type(bgr2hsv(x))
(bgr2hsv(x)).shape
numpy.ndarray
(480, 720, 3)

lapply[source]

lapply(x:Any, func:Callable)

Apply func to each element in list x

type(x)
[type(i) for i in lapply([x, x, x], as_tensor)]
numpy.ndarray
[torch.Tensor, torch.Tensor, torch.Tensor]

Resize Image

resize[source]

resize(image, height=None, width=None, keep_aspect_ratio=True, scale_factor=1.0)

Resize by scale_factor if preserving aspect ratio else resize by custom height and width

resize(x).shape # scale_factor = 1. by default
resize(x, scale_factor=1.5).shape
resize(x, width=200, height=100, keep_aspect_ratio=False).shape
(480, 720, 3)
(720, 1080, 3)
(100, 200, 3)