Accelerate deep learning pipelines with toCUDA
mainThe toCUDA function translates video/streams from HWC-uint8 (CPU) to CHW-float32 (CUDA device). This reduces CPU load and is >2x faster than manual conversion.
Requirements: NVIDIA CUDA environment and pycuda (installed via pip install ffmpegcv[cuda]).
Usage Pattern
import ffmpegcv
# Recommended for GPU: use nv12 pix_fmt
cap = ffmpegcv.toCUDA(
ffmpegcv.VideoCaptureNV(file, pix_fmt='nv12'),
tensor_format='chw'
)
for frame_CHW_cuda in cap:
# frame_CHW_cuda is a PyCUDA array or PyTorch tensor
# Perform normalization and model inference directly on GPU
result = model(frame_CHW_cuda)Key Methods for CUDA frames
cap.read(): Returns apycudaarray.cap.read_cudamem(): Returns apycudamemory allocation object.cap.read_torch(): Returns apytorchtensor.cap.read_torch(output_memory): Performs a zero-copy read into existing memory.
cap = ffmpegcv.toCUDA(
ffmpegcv.VideoCaptureNV(file, pix_fmt='nv12'),
tensor_format='chw'
)
for frame_CHW_cuda in cap:
result = model(frame_CHW_cuda)