读取/写入图像和视频¶
该 torchvision.io 包提供了执行IO操作的函数。它们目前专门用于读取和写入视频和图像。
视频¶
|
从文件中读取视频,返回视频帧和音频帧。 |
|
列出视频帧的时间戳。 |
|
以[T, H, W, C]格式写入4D张量到视频文件中 |
细粒度视频API¶
除了read_video函数之外,我们还提供了一个高性能的低级API,与read_video函数相比,它提供了更精细的控制。它在完全支持torchscript的同时实现了这一切。
警告
细粒度视频API尚处于测试阶段,不保证向后兼容性。
|
细粒度视频读取API。 |
查看视频示例:
import torchvision
video_path = "path to a test video"
# Constructor allocates memory and a threaded decoder
# instance per video. At the moment it takes two arguments:
# path to the video file, and a wanted stream.
reader = torchvision.io.VideoReader(video_path, "video")
# The information about the video can be retrieved using the
# `get_metadata()` method. It returns a dictionary for every stream, with
# duration and other relevant metadata (often frame rate)
reader_md = reader.get_metadata()
# metadata is structured as a dict of dicts with following structure
# {"stream_type": {"attribute": [attribute per stream]}}
#
# following would print out the list of frame rates for every present video stream
print(reader_md["video"]["fps"])
# we explicitly select the stream we would like to operate on. In
# the constructor we select a default video stream, but
# in practice, we can set whichever stream we would like
video.set_current_stream("video:0")
图像¶
支持以各种模式读取图像。 |
|
读取一张 JPEG 或 PNG 图像并将其转换为三维的 RGB 或灰度 Tensor。 |
|
检测图像是否为 JPEG 或 PNG,并执行适当的操作以将其解码为三维的 RGB 或灰度 Tensor。 |
|
接受一个布局为 CHW 的输入张量,并返回其对应 JPEG 文件的内容缓冲区。 |
|
将JPEG图像解码为3维的RGB或灰度Tensor。 |
|
将布局为 CHW 的输入张量保存为 JPEG 文件。 |
|
接受一个布局为 CHW 的输入张量,并返回其对应 PNG 文件的内容缓冲区。 |
|
将一张 PNG 图像解码为三维的 RGB 或灰度 Tensor。 |
|
接受一个布局为 CHW 的输入张量(对于灰度图像为 HW),并将其保存为 PNG 文件。 |
|
读取并输出文件的字节内容为一维的 uint8 张量。 |
|
将一个一维的 uint8 张量的内容写入文件。 |