OpenCV utilities for reading video frames
Project description
Video Utilities for OpenCV
videoutilslets you get rid of writing boilerplate code for reading video and adds some convenience on top of that.
Install
pip install videoutils
How to use
from videoutils.io import read_video, as_tensor, bgr2rgb, resize
fname = 'files/interstellar-waves-edit.mp4'
x = read_video(fname)
len(x)
x[0].shape
1578
(480, 720, 3)
By default, read_video returns a list of np.arrays of shape (height, width, channels).
However, you can define precisely which frames you'd like to grab in a number of ways. This is done by using either the {start_idx, end_idx, frame_stride} or target_frames arguments.
Grab the first n frames
n = 50
x = read_video(fname, end_idx=n)
x2 = read_video(fname, target_frames=(0,n))
len(x)
len(x) == len(x2)
50
True
Grab every nth frame
n=5
x = read_video(fname, frame_stride=n, end_idx=50)
len(x)
10
x = read_video(fname, frame_stride=50) # total frames = 1578
len(x)
32
Grab frames at specific indices
x = read_video(fname, target_frames=[10, 50, 76, 420])
len(x)
4
x = read_video(fname, start_idx=10, end_idx=15)
x2 = read_video(fname, target_frames=(10, 15))
len(x)
len(x) == len(x2)
5
True
Return as torch.Tensor
You can pass any function that transforms a np.array of shape (height, width, channels) to the apply argument. videoutils provides as_tensor for convenience -- if you use this function, read_video will automatically call torch.stack and return the collection of frames as a 4D tensor, else it will return a list of 3D arrays/tensors.
import torch
from functools import partial
x = read_video(fname, end_idx=10, apply=as_tensor)
x2 = read_video(fname, end_idx=10, apply=partial(as_tensor, normalise=True))
x2 = torch.stack(x2) # since we aren't using `as_tensor`, but a partial (thus different) function
x.shape
x.shape == x2.shape
x.mean(), x2.mean()
torch.Size([10, 480, 720, 3])
True
(tensor(36.8276), tensor(0.1443))
Resize Video
read_video has an optional argument resize_func which is meant to be a function that resizes a np.array of shape (height, width, channels).
You can use the predefined resize function or pass in a custom function here.
help(resize)
Help on function resize in module videoutils.utils:
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`
x = read_video(fname, target_frames=[0,1,2], apply=as_tensor,
resize_func=partial(resize, scale_factor=2.))
x.shape
torch.Size([3, 960, 1440, 3])
x = read_video(fname, target_frames=[0,1,2], apply=as_tensor,
resize_func=partial(resize, width=200, height=100, keep_aspect_ratio=False))
x.shape
torch.Size([3, 100, 200, 3])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file videoutils-0.0.35.tar.gz.
File metadata
- Download URL: videoutils-0.0.35.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4554aa93173c6de125c258020fc8e7c3b978f23b35a602f37b849ad3b6f1db7a
|
|
| MD5 |
5703ae9e1c759f6cb7b19dd5e5a4de0a
|
|
| BLAKE2b-256 |
705743d7f00059251007d6b441cea680caa57bbb52043dc61bdc48d722ac9076
|
File details
Details for the file videoutils-0.0.35-py3-none-any.whl.
File metadata
- Download URL: videoutils-0.0.35-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43eee4f4839dc974f03e03a7880c63b49e1eba0a03c45e30fb690ec5e46d2c04
|
|
| MD5 |
196e6904935ea21350257696717be41e
|
|
| BLAKE2b-256 |
17c42213bc1d3285a067f2d2180cb47ab3b0c6dc21c30b8eb075102d2cbff626
|