WebCam.tensor#
- WebCam.tensor(device: str = 'cpu', normalize='imagenet', dtype: torch.dtype | None = None) torch.Tensor#
Convert all images from this source into a single 4D PyTorch tensor.
- Parameters:
device (str, optional) – target PyTorch device, e.g.
"cpu","cuda"or"mps", defaults to"cpu"normalize (str, tuple, or None, optional) – normalisation to apply to each frame; passed directly to
Image.tensor, defaults to"imagenet"dtype (torch.dtype or None, optional) – output tensor dtype, for example
torch.float32; passed directly toImage.tensor, defaults to None
- Raises:
ImportError – if PyTorch is not installed
TypeError – if the source is not finite (no
__len__)TypeError – if any yielded item is not an
ImageValueError – if any frame has a different shape to the first
- Returns:
tensor of shape
(N, C, H, W)- Return type:
torch.Tensor
Images are decoded one at a time and written directly into a pre-allocated tensor, so peak memory is one decoded frame plus the output tensor — the full source need not reside in memory at once. This is particularly useful for
VideoFileandZipArchive.Example:
>>> from machinevisiontoolbox import VideoFile >>> t = VideoFile("traffic_sequence.mpg").tensor(normalize=None) >>> t.shape torch.Size([N, 3, H, W])