解码/编码图像和视频¶
The torchvision.io
包提供用于执行 IO 操作的函数。它们目前专门用于读取和写入图像和视频。
图像¶
|
将 JPEG、PNG 或 GIF 图像读入 3 维 RGB 或灰度张量。 |
|
检测图像是否为 JPEG、PNG 或 GIF,并执行相应的操作以将图像解码为 3 维 RGB 或灰度张量。 |
|
接受 CHW 布局的 (输入张量) 列表,并返回包含相应 JPEG 文件内容的 (缓冲区) 列表。 |
|
将 JPEG 图像解码为 3 维 RGB 或灰度张量。 |
|
接受 CHW 布局的输入张量,并将其保存到 JPEG 文件中。 |
|
将 GIF 图像解码为 3 或 4 维 RGB 张量。 |
|
接受 CHW 布局的输入张量,并返回包含其对应 PNG 文件内容的缓冲区。 |
|
将 PNG 图像解码为 3 维 RGB 或灰度张量。 |
|
接受 CHW 布局的输入张量(或灰度图像的 HW 布局),并将其保存到 PNG 文件中。 |
|
读取并输出文件字节内容,作为具有一个维度的 uint8 张量。 |
|
将具有一个维度的 uint8 张量的内容写入文件。 |
|
支持读取图像时的各种模式。 |
视频¶
|
从文件读取视频,返回视频帧和音频帧。 |
|
列出视频帧的时间戳。 |
|
将 [T, H, W, C] 格式的 4d 张量写入视频文件。 |
细粒度视频 API¶
除了 read_video
函数外,我们还提供了一个高性能的低级 API,与 read_video
函数相比,它提供了更细粒度的控制。它在完全支持 torchscript 的同时完成了所有这些工作。
警告
细粒度视频 API 处于 Beta 阶段,不保证向后兼容性。
|
细粒度视频读取 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")