torchvision.io¶
该软件包提供用于执行 IO 的函数
操作。它们目前特定于读取和写入视频和
图像。torchvision.io
视频¶
-
torchvision.io.
read_video
(文件名: str, start_pts: int = 0, end_pts: Union[float, NoneType] = None, pts_unit: str = 'pts') → Tuple[torch.Tensor、torch 的 Tensor 和 Torch 的 T张量, Dict[str, Any]][来源]¶ 从文件中读取视频,同时返回视频帧和 音频帧
参数: 返回: - vframes (Tensor[T, H, W, C]) – T 视频帧
- aframes (Tensor[K, L]) – 音频帧,其中 K 是声道数,L 是 点数
- info (Dict) – 视频和音频的元数据。可以包含字段 video_fps (float) 和 audio_fps (int)
-
torchvision.io.
read_video_timestamps
(文件名: str, pts_unit: str = 'pts') → Tuple[List[int], Union[float, NoneType]][源代码]¶ 列出视频帧时间戳。
请注意,该函数会逐帧解码整个视频。
参数: 返回: - pts (List[int] if pts_unit = 'pts') – List[Fraction] 如果 pts_unit = 'sec' 视频中每个帧的演示时间戳。
- video_fps (float, optional) – 视频的帧速率
-
torchvision.io.
write_video
(文件名:str,video_array:torch。张量,fps:浮点数,video_codec:str = 'libx264',选项:Union[Dict[str, Any], NoneType] = None) → None[来源]¶ 在视频文件中写入 [T, H, W, C] 格式的 4d 张量
参数: - filename (str) – 保存视频的路径
- video_array (Tensor[T, H, W, C]) – 包含单个帧的张量,作为 [T, H, W, C] 格式的 uint8 张量
- fps (Number) – 每秒帧数
细粒度视频 API¶
除了功能之外,我们还提供高性能
与函数相比,用于更精细的控制。
它在完全支持 torchscript 的同时完成所有这些工作。read_video
read_video
-
class (path, stream='video')[来源]
torchvision.io.
VideoReader
¶ 细粒度视频阅读 API。 支持从单个视频中逐帧读取各种流 容器。
例
以下示例创建一个对象,查找到 2 秒 point 并返回单个帧:
VideoReader
import torchvision video_path = "path_to_a_test_video" reader = torchvision.io.VideoReader(video_path, "video") reader.seek(2.0) frame = next(reader)
VideoReader
实现可迭代 API,使其适合 将其与itertools
以获得更高级的阅读。 因此,我们可以在 for 循环中使用实例:VideoReader
reader.seek(2) for frame in reader: frames.append(frame['data']) # additionally, `seek` implements a fluent API, so we can do for frame in reader.seek(2): frames.append(frame['data'])
跟
itertools
,我们可以使用 以下代码:for frame in itertools.takewhile(lambda x: x['pts'] <= 5, reader.seek(2)): frames.append(frame['data'])
同样,可以实现在 2 秒时间戳后读取 10 帧 如下:
for frame in itertools.islice(reader.seek(2), 10): frames.append(frame['data'])
注意
每个流描述符由两部分组成:流类型(例如 'video')和 唯一的 Stream ID(由视频编码决定)。 这样,如果视频容器包含多个 相同类型的流,用户可以访问他们想要的流。 如果仅传递流类型,则解码器会自动检测该类型的第一个流。
参数: - path (string) – 支持格式的视频文件的路径
- stream (string, optional) – 所需流的描述符,后跟流 ID,
格式为 .默认为 。
当前可用的选项包括
{stream_type}:{stream_id}
"video:0"
['video', 'audio']
-
seek
(time_s:float)[来源]¶ 在当前流中查找。
参数: time_s (float) – 寻道时间(以秒为单位) 注意
当前的实现是所谓的 precise seek。这 表示跟随 seek,调用 to 将返回 frame 替换为确切的时间戳(如果存在)或 时间戳大于 .
next()
time_s
-
set_current_stream
(stream: str)[来源]¶ 设置当前流。 显式定义我们正在作的流。
参数: stream (string) – 所需流的描述符。默认为 Currently available stream types include . 每个描述符由两部分组成:流类型(例如 'video')和 唯一的 Stream ID(由视频编码决定)。 这样,如果视频容器包含多个 相同类型的流,用户可以访问他们想要的流。 如果仅传递流类型,则解码器会自动检测第一个流 并返回它。 "video:0"
['video', 'audio']
返回: 成功时为 True,否则为 False 返回类型: (布尔)
检查视频的示例:
import torchvision
video_path = "path to a test video"
# Constructor allocates memory and a threaded decoder
# instance per video. At the momet 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")
图像¶
-
torchvision.io.
read_image
(path: str) → torch 的 Torch 进行。张量[来源]¶ 将 JPEG 或 PNG 图像读取为 3 维 RGB 张量。 输出张量的值为 uint8,介于 0 和 255 之间。
参数: path (str) – JPEG 或 PNG 图像的路径。 返回: 输出 返回类型: 张量[3, image_height, image_width]
-
torchvision.io.
decode_image
(输入:Torch。Tensor) → torch 的 Tensor 中。张量[来源]¶ 检测图像是 JPEG 还是 PNG,并执行适当的 作将图像解码为 3 维 RGB Tensor。
输出张量的值为 uint8,介于 0 和 255 之间。
参数: input (Tensor) – 一个一维 uint8 张量,包含 PNG 或 JPEG 图像。 返回: 输出 返回类型: 张量[3, image_height, image_width]
-
torchvision.io.
encode_jpeg
(输入:Torch。Tensor,质量:int = 75)→ torch。张量[来源]¶ 采用 CHW 布局中的输入张量,并返回包含内容的缓冲区 的相应 JPEG 文件。
参数: - input (Tensor[channels, image_height, image_width])) - c 通道的 int8 图像张量,其中 c 必须为 1 或 3。
- quality (int) – 生成的 JPEG 文件的质量,它必须是介于 1 和 100。默认值:75
返回: output – 一个一维 int8 张量,其中包含 JPEG 文件。
返回类型: 张量[1]
-
torchvision.io.
encode_png
(输入:Torch。Tensor,compression_level:int = 6) → torch。张量[来源]¶ 采用 CHW 布局中的输入张量,并返回包含内容的缓冲区 。
参数: - input (Tensor[channels, image_height, image_width]) - c 通道的 int8 图像张量,其中 c 必须为 3 或 1。
- compression_level (int) – 结果文件的压缩系数,它必须是一个数字 介于 0 和 9 之间。默认值:6
返回: output – 一个一维 int8 张量,其中包含 PNG 文件。
返回类型: 张量[1]