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])
Metadata
Release files for videoutils 0.0.35
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| videoutils-0.0.35.tar.gz | 6.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| videoutils-0.0.35-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / videoutils-0.0.35.tar.gz
| Download URL | videoutils-0.0.35.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4554aa93173c6de125c258020fc8e7c3b978f23b35a602f37b849ad3b6f1db7a
|
|
BLAKE2b-256 checksum How to use checksums |
705743d7f00059251007d6b441cea680caa57bbb52043dc61bdc48d722ac9076
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / videoutils-0.0.35-py3-none-any.whl
| Download URL | videoutils-0.0.35-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43eee4f4839dc974f03e03a7880c63b49e1eba0a03c45e30fb690ec5e46d2c04
|
|
BLAKE2b-256 checksum How to use checksums |
17c42213bc1d3285a067f2d2180cb47ab3b0c6dc21c30b8eb075102d2cbff626
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for videoutils 0.0.35
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.