From 29a1c929ec2c1d19431122753f6f2099f506be8d Mon Sep 17 00:00:00 2001 From: frgfm Date: Tue, 13 Jul 2021 12:21:34 +0200 Subject: [PATCH 01/17] style: Added typing annotations --- torchvision/io/_video_opt.py | 124 +++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index a4a811dec4b..8ab48d2c143 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -3,7 +3,7 @@ import os import warnings from fractions import Fraction -from typing import List, Tuple +from typing import List, Tuple, Dict, Any, Union, Optional import numpy as np import torch @@ -24,21 +24,20 @@ # simple class for torch scripting # the complex Fraction class from fractions module is not scriptable class Timebase(object): - __annotations__ = {"numerator": int, "denominator": int} - __slots__ = ["numerator", "denominator"] + __annotations__: Dict[str, int] = {"numerator": int, "denominator": int} + __slots__: List[str] = ["numerator", "denominator"] def __init__( self, - numerator, # type: int - denominator, # type: int - ): - # type: (...) -> None + numerator: int, + denominator: int, + ) -> None: self.numerator = numerator self.denominator = denominator class VideoMetaData(object): - __annotations__ = { + __annotations__: Dict[str, Any] = { "has_video": bool, "video_timebase": Timebase, "video_duration": float, @@ -48,7 +47,7 @@ class VideoMetaData(object): "audio_duration": float, "audio_sample_rate": float, } - __slots__ = [ + __slots__: List[str] = [ "has_video", "video_timebase", "video_duration", @@ -59,7 +58,7 @@ class VideoMetaData(object): "audio_sample_rate", ] - def __init__(self): + def __init__(self) -> None: self.has_video = False self.video_timebase = Timebase(0, 1) self.video_duration = 0.0 @@ -70,8 +69,7 @@ def __init__(self): self.audio_sample_rate = 0.0 -def _validate_pts(pts_range): - # type: (List[int]) -> None +def _validate_pts(pts_range: List[int]) -> None: if pts_range[1] > 0: assert ( @@ -83,8 +81,14 @@ def _validate_pts(pts_range): ) -def _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration): - # type: (torch.Tensor,torch.Tensor,torch.Tensor,torch.Tensor,torch.Tensor,torch.Tensor) -> VideoMetaData +def _fill_info( + vtimebase: torch.Tensor, + vfps: torch.Tensor, + vduration: torch.Tensor, + atimebase: torch.Tensor, + asample_rate: torch.Tensor, + aduration: torch.Tensor, +) -> VideoMetaData: """ Build update VideoMetaData struct with info about the video """ @@ -113,8 +117,7 @@ def _fill_info(vtimebase, vfps, vduration, atimebase, asample_rate, aduration): return meta -def _align_audio_frames(aframes, aframe_pts, audio_pts_range): - # type: (torch.Tensor, torch.Tensor, List[int]) -> torch.Tensor +def _align_audio_frames(aframes: torch.Tensor, aframe_pts: torch.Tensor, audio_pts_range: List[int]) -> torch.Tensor: start, end = aframe_pts[0], aframe_pts[-1] num_samples = aframes.size(0) step_per_aframe = float(end - start + 1) / float(num_samples) @@ -128,21 +131,21 @@ def _align_audio_frames(aframes, aframe_pts, audio_pts_range): def _read_video_from_file( - filename, - seek_frame_margin=0.25, - read_video_stream=True, - video_width=0, - video_height=0, - video_min_dimension=0, - video_max_dimension=0, - video_pts_range=(0, -1), - video_timebase=default_timebase, - read_audio_stream=True, - audio_samples=0, - audio_channels=0, - audio_pts_range=(0, -1), - audio_timebase=default_timebase, -): + filename: str, + seek_frame_margin: float = 0.25, + read_video_stream: bool = True, + video_width: int = 0, + video_height: int = 0, + video_min_dimension: int = 0, + video_max_dimension: int = 0, + video_pts_range: Tuple[int, int] = (0, -1), + video_timebase: Fraction = default_timebase, + read_audio_stream: bool = True, + audio_samples: int = 0, + audio_channels: int = 0, + audio_pts_range: Tuple[int, int] = (0, -1), + audio_timebase: Fraction = default_timebase, +) -> Tuple[torch.Tensor, torch.Tensor, VideoMetaData]: """ Reads a video from a file, returning both the video frames as well as the audio frames @@ -227,7 +230,7 @@ def _read_video_from_file( return vframes, aframes, info -def _read_video_timestamps_from_file(filename): +def _read_video_timestamps_from_file(filename: str) -> Tuple[List[int], List[int], VideoMetaData]: """ Decode all video- and audio frames in the video. Only pts (presentation timestamp) is returned. The actual frame pixel data is not @@ -263,7 +266,7 @@ def _read_video_timestamps_from_file(filename): return vframe_pts, aframe_pts, info -def _probe_video_from_file(filename): +def _probe_video_from_file(filename: str) -> VideoMetaData: """ Probe a video file and return VideoMetaData with info about the video """ @@ -274,24 +277,23 @@ def _probe_video_from_file(filename): def _read_video_from_memory( - video_data, # type: torch.Tensor - seek_frame_margin=0.25, # type: float - read_video_stream=1, # type: int - video_width=0, # type: int - video_height=0, # type: int - video_min_dimension=0, # type: int - video_max_dimension=0, # type: int - video_pts_range=(0, -1), # type: List[int] - video_timebase_numerator=0, # type: int - video_timebase_denominator=1, # type: int - read_audio_stream=1, # type: int - audio_samples=0, # type: int - audio_channels=0, # type: int - audio_pts_range=(0, -1), # type: List[int] - audio_timebase_numerator=0, # type: int - audio_timebase_denominator=1, # type: int -): - # type: (...) -> Tuple[torch.Tensor, torch.Tensor] + video_data: torch.Tensor, + seek_frame_margin: float = 0.25, + read_video_stream: int = 1, + video_width: int = 0, + video_height: int = 0, + video_min_dimension: int = 0, + video_max_dimension: int = 0, + video_pts_range: Tuple[int, int] = (0, -1), + video_timebase_numerator: int = 0, + video_timebase_denominator: int = 1, + read_audio_stream: int = 1, + audio_samples: int =0, + audio_channels: int = 0, + audio_pts_range: Tuple[int, int] = (0, -1), + audio_timebase_numerator: int = 0, + audio_timebase_denominator: int = 1, +) -> Tuple[torch.Tensor, torch.Tensor]: """ Reads a video from memory, returning both the video frames as well as the audio frames @@ -384,7 +386,9 @@ def _read_video_from_memory( return vframes, aframes -def _read_video_timestamps_from_memory(video_data): +def _read_video_timestamps_from_memory( + video_data: Union[torch.Tensor, np.ndarray], +) -> Tuple[List[int], List[int], VideoMetaData]: """ Decode all frames in the video. Only pts (presentation timestamp) is returned. The actual frame pixel data is not copied. Thus, read_video_timestamps(...) @@ -424,8 +428,9 @@ def _read_video_timestamps_from_memory(video_data): return vframe_pts, aframe_pts, info -def _probe_video_from_memory(video_data): - # type: (torch.Tensor) -> VideoMetaData +def _probe_video_from_memory( + video_data: Union[torch.Tensor, np.ndarray], +) -> VideoMetaData: """ Probe a video in memory and return VideoMetaData with info about the video This function is torchscriptable @@ -438,7 +443,7 @@ def _probe_video_from_memory(video_data): return info -def _convert_to_sec(start_pts, end_pts, pts_unit, time_base): +def _convert_to_sec(start_pts: int, end_pts: int, pts_unit: str, time_base: int) -> Tuple[float, float, str]: if pts_unit == 'pts': start_pts = float(start_pts * time_base) end_pts = float(end_pts * time_base) @@ -446,7 +451,12 @@ def _convert_to_sec(start_pts, end_pts, pts_unit, time_base): return start_pts, end_pts, pts_unit -def _read_video(filename, start_pts=0, end_pts=None, pts_unit="pts"): +def _read_video( + filename: str, + start_pts: int = 0, + end_pts: Optional[float] = None, + pts_unit: str = "pts" +) -> Tuple[torch.Tensor, torch.Tensor, VideoMetaData]: if end_pts is None: end_pts = float("inf") @@ -517,7 +527,7 @@ def get_pts(time_base): return vframes, aframes, _info -def _read_video_timestamps(filename, pts_unit="pts"): +def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[int], Optional[int]]: if pts_unit == "pts": warnings.warn( "The pts_unit 'pts' gives wrong results and will be removed in a " From a3f9748181bdbea77ac93a7ad347d25054d54e28 Mon Sep 17 00:00:00 2001 From: frgfm Date: Mon, 19 Jul 2021 19:17:57 +0200 Subject: [PATCH 02/17] style: Fixed lint --- torchvision/io/_video_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 8ab48d2c143..38cef1a5461 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -288,7 +288,7 @@ def _read_video_from_memory( video_timebase_numerator: int = 0, video_timebase_denominator: int = 1, read_audio_stream: int = 1, - audio_samples: int =0, + audio_samples: int = 0, audio_channels: int = 0, audio_pts_range: Tuple[int, int] = (0, -1), audio_timebase_numerator: int = 0, From 0797f2f044e84234ed06af4d54b5bab25ff027b8 Mon Sep 17 00:00:00 2001 From: frgfm Date: Sat, 31 Jul 2021 15:14:14 +0200 Subject: [PATCH 03/17] style: Fixed typing --- torchvision/io/_video_opt.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 38cef1a5461..ea06df62537 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -3,7 +3,7 @@ import os import warnings from fractions import Fraction -from typing import List, Tuple, Dict, Any, Union, Optional +from typing import List, Tuple, Dict, Any, Union, Optional, Type, cast import numpy as np import torch @@ -24,7 +24,7 @@ # simple class for torch scripting # the complex Fraction class from fractions module is not scriptable class Timebase(object): - __annotations__: Dict[str, int] = {"numerator": int, "denominator": int} + __annotations__: Dict[str, Type[int]] = {"numerator": int, "denominator": int} __slots__: List[str] = ["numerator", "denominator"] def __init__( @@ -69,7 +69,7 @@ def __init__(self) -> None: self.audio_sample_rate = 0.0 -def _validate_pts(pts_range: List[int]) -> None: +def _validate_pts(pts_range: Tuple[int, int]) -> None: if pts_range[1] > 0: assert ( @@ -117,7 +117,11 @@ def _fill_info( return meta -def _align_audio_frames(aframes: torch.Tensor, aframe_pts: torch.Tensor, audio_pts_range: List[int]) -> torch.Tensor: +def _align_audio_frames( + aframes: torch.Tensor, + aframe_pts: torch.Tensor, + audio_pts_range: Tuple[int, int] +) -> torch.Tensor: start, end = aframe_pts[0], aframe_pts[-1] num_samples = aframes.size(0) step_per_aframe = float(end - start + 1) / float(num_samples) @@ -443,7 +447,7 @@ def _probe_video_from_memory( return info -def _convert_to_sec(start_pts: int, end_pts: int, pts_unit: str, time_base: int) -> Tuple[float, float, str]: +def _convert_to_sec(start_pts: float, end_pts: float, pts_unit: str, time_base: Fraction) -> Tuple[float, float, str]: if pts_unit == 'pts': start_pts = float(start_pts * time_base) end_pts = float(end_pts * time_base) @@ -456,7 +460,7 @@ def _read_video( start_pts: int = 0, end_pts: Optional[float] = None, pts_unit: str = "pts" -) -> Tuple[torch.Tensor, torch.Tensor, VideoMetaData]: +) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]: if end_pts is None: end_pts = float("inf") @@ -527,7 +531,7 @@ def get_pts(time_base): return vframes, aframes, _info -def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[int], Optional[int]]: +def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[Union[int, Fraction]], Optional[float]]: if pts_unit == "pts": warnings.warn( "The pts_unit 'pts' gives wrong results and will be removed in a " @@ -540,7 +544,7 @@ def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[i video_time_base = Fraction( info.video_timebase.numerator, info.video_timebase.denominator ) - pts = [x * video_time_base for x in pts] + pts = [x * video_time_base for x in pts] # type: ignore[misc] video_fps = info.video_fps if info.has_video else None From 496984cb2f470bae5503287f700fb832cfb950b7 Mon Sep 17 00:00:00 2001 From: frgfm Date: Sat, 31 Jul 2021 15:16:59 +0200 Subject: [PATCH 04/17] chore: Updated mypy.ini --- mypy.ini | 4 ---- 1 file changed, 4 deletions(-) diff --git a/mypy.ini b/mypy.ini index 040b52dfda4..47b82ebe47b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -4,10 +4,6 @@ files = torchvision show_error_codes = True pretty = True -[mypy-torchvision.io._video_opt.*] - -ignore_errors = True - [mypy-torchvision.io.*] ignore_errors = True From f6489b93db2e778449b71ccddde4c526d3e0ba89 Mon Sep 17 00:00:00 2001 From: frgfm Date: Sat, 31 Jul 2021 15:17:09 +0200 Subject: [PATCH 05/17] style: Fixed typing --- torchvision/io/_video_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index ea06df62537..943cce1b4be 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -548,4 +548,4 @@ def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[U video_fps = info.video_fps if info.has_video else None - return pts, video_fps + return pts, video_fps # type: ignore[return-value] From a6fe0910ea9ef2db8ca48ba9c34cad4f494188b5 Mon Sep 17 00:00:00 2001 From: frgfm Date: Tue, 3 Aug 2021 19:31:00 +0200 Subject: [PATCH 06/17] chore: Updated mypy.ini --- mypy.ini | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 47b82ebe47b..044d2946c40 100644 --- a/mypy.ini +++ b/mypy.ini @@ -4,7 +4,11 @@ files = torchvision show_error_codes = True pretty = True -[mypy-torchvision.io.*] +[mypy-torchvision.io.image.*] + +ignore_errors = True + +[mypy-torchvision.io.video.*] ignore_errors = True From c23cf7d77d4a8191ac4d55ad73b30df1f7b3cb8c Mon Sep 17 00:00:00 2001 From: frgfm Date: Tue, 3 Aug 2021 19:31:19 +0200 Subject: [PATCH 07/17] style: Fixed typing compatibility with jit --- torchvision/io/_video_opt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 943cce1b4be..e5cd3e0e4b5 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -3,7 +3,7 @@ import os import warnings from fractions import Fraction -from typing import List, Tuple, Dict, Any, Union, Optional, Type, cast +from typing import List, Tuple, Dict, Any, Optional, Type, cast import numpy as np import torch @@ -391,7 +391,7 @@ def _read_video_from_memory( def _read_video_timestamps_from_memory( - video_data: Union[torch.Tensor, np.ndarray], + video_data: torch.Tensor, ) -> Tuple[List[int], List[int], VideoMetaData]: """ Decode all frames in the video. Only pts (presentation timestamp) is returned. @@ -433,7 +433,7 @@ def _read_video_timestamps_from_memory( def _probe_video_from_memory( - video_data: Union[torch.Tensor, np.ndarray], + video_data: torch.Tensor, ) -> VideoMetaData: """ Probe a video in memory and return VideoMetaData with info about the video @@ -531,7 +531,7 @@ def get_pts(time_base): return vframes, aframes, _info -def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[Union[int, Fraction]], Optional[float]]: +def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[Fraction], Optional[float]]: if pts_unit == "pts": warnings.warn( "The pts_unit 'pts' gives wrong results and will be removed in a " From 60f5bfe73a85c7c46b0af29961e086d403db631d Mon Sep 17 00:00:00 2001 From: frgfm Date: Mon, 23 Aug 2021 10:01:19 +0200 Subject: [PATCH 08/17] style: Fixed typing --- torchvision/io/_video_opt.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index e5cd3e0e4b5..24e12c84e55 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -24,7 +24,7 @@ # simple class for torch scripting # the complex Fraction class from fractions module is not scriptable class Timebase(object): - __annotations__: Dict[str, Type[int]] = {"numerator": int, "denominator": int} + __annotations__: Dict[str, Type] = {"numerator": int, "denominator": int} __slots__: List[str] = ["numerator", "denominator"] def __init__( @@ -37,7 +37,7 @@ def __init__( class VideoMetaData(object): - __annotations__: Dict[str, Any] = { + __annotations__: Dict[str, Type] = { "has_video": bool, "video_timebase": Timebase, "video_duration": float, @@ -531,21 +531,25 @@ def get_pts(time_base): return vframes, aframes, _info -def _read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[Fraction], Optional[float]]: +def _read_video_timestamps( + filename: str, + pts_unit: str = "pts" +) -> Tuple[Union[List[int], List[Fraction]], Optional[float]]:: if pts_unit == "pts": warnings.warn( "The pts_unit 'pts' gives wrong results and will be removed in a " + "follow-up version. Please use pts_unit 'sec'." ) + pts: Union[List[int], List[Fraction]] pts, _, info = _read_video_timestamps_from_file(filename) if pts_unit == "sec": video_time_base = Fraction( info.video_timebase.numerator, info.video_timebase.denominator ) - pts = [x * video_time_base for x in pts] # type: ignore[misc] + pts = [x * video_time_base for x in pts] video_fps = info.video_fps if info.has_video else None - return pts, video_fps # type: ignore[return-value] + return pts, video_fps From ce47bb1a5f0cc8f43ee447f09c2cd37028cbabc0 Mon Sep 17 00:00:00 2001 From: frgfm Date: Sat, 18 Sep 2021 16:01:24 +0200 Subject: [PATCH 09/17] style: Fixed typing --- torchvision/io/_video_opt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 24e12c84e55..74b27847f12 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -24,8 +24,8 @@ # simple class for torch scripting # the complex Fraction class from fractions module is not scriptable class Timebase(object): - __annotations__: Dict[str, Type] = {"numerator": int, "denominator": int} - __slots__: List[str] = ["numerator", "denominator"] + __annotations__ = {"numerator": int, "denominator": int} + __slots__ = ["numerator", "denominator"] def __init__( self, @@ -37,7 +37,7 @@ def __init__( class VideoMetaData(object): - __annotations__: Dict[str, Type] = { + __annotations__ = { "has_video": bool, "video_timebase": Timebase, "video_duration": float, @@ -534,7 +534,7 @@ def get_pts(time_base): def _read_video_timestamps( filename: str, pts_unit: str = "pts" -) -> Tuple[Union[List[int], List[Fraction]], Optional[float]]:: +) -> Tuple[Union[List[int], List[Fraction]], Optional[float]]: if pts_unit == "pts": warnings.warn( "The pts_unit 'pts' gives wrong results and will be removed in a " From 6cfabdc128e9b118957e0fc41c989bfc32960ea0 Mon Sep 17 00:00:00 2001 From: frgfm Date: Sat, 18 Sep 2021 16:03:59 +0200 Subject: [PATCH 10/17] style: Fixed missing import --- torchvision/io/_video_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 74b27847f12..54aee5050b1 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -3,7 +3,7 @@ import os import warnings from fractions import Fraction -from typing import List, Tuple, Dict, Any, Optional, Type, cast +from typing import List, Tuple, Dict, Any, Optional, Type, cast, Union import numpy as np import torch From 642fa6d2c56a2eb0ba02c31f58f27d2f49c2ae96 Mon Sep 17 00:00:00 2001 From: frgfm Date: Mon, 20 Sep 2021 12:42:58 +0200 Subject: [PATCH 11/17] style: Fixed typing of __iter__ --- torchvision/io/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index d0ec1b406f3..b87f0094aa0 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -127,7 +127,7 @@ def __next__(self) -> Dict[str, Any]: raise StopIteration return {"data": frame, "pts": pts} - def __iter__(self) -> Iterator['VideoReader']: + def __iter__(self) -> Iterator[Dict[str, Any]]: return self def seek(self, time_s: float) -> 'VideoReader': From a4a49f97f55b724384e663cd7ab29179cd5f6aaf Mon Sep 17 00:00:00 2001 From: frgfm Date: Tue, 16 Nov 2021 18:00:26 +0100 Subject: [PATCH 12/17] style: Fixed typing --- torchvision/io/_video_opt.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index c192b21cdfd..ff84e03ceec 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -430,7 +430,12 @@ def _probe_video_from_memory( return info -def _convert_to_sec(start_pts: float, end_pts: float, pts_unit: str, time_base: Fraction) -> Tuple[float, float, str]: +def _convert_to_sec( + start_pts: Union[float, Fraction], + end_pts: Union[float, Fraction], + pts_unit: str, + time_base: Fraction +) -> Tuple[Union[float, Fraction], Union[float, Fraction], str]: if pts_unit == 'pts': start_pts = float(start_pts * time_base) end_pts = float(end_pts * time_base) @@ -441,7 +446,7 @@ def _convert_to_sec(start_pts: float, end_pts: float, pts_unit: str, time_base: def _read_video( filename: str, start_pts: int = 0, - end_pts: Optional[float] = None, + end_pts: Optional[Union[float, Fraction]] = None, pts_unit: str = "pts" ) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]: if end_pts is None: From f6e7e03b34c258250fee5b3d529b65e5ef1530f4 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 17 Nov 2021 01:02:38 +0100 Subject: [PATCH 13/17] style: Fixed lint --- torchvision/io/_video_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index ff84e03ceec..1ae8ae66d6d 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -1,7 +1,7 @@ import math import warnings from fractions import Fraction -from typing import List, Tuple, Dict, Any, Optional, Type, cast, Union +from typing import List, Tuple, Dict, Optional, Union import torch From 0aab92696723a68f6e4b4d9fbce87819597f2b01 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 17 Nov 2021 01:02:52 +0100 Subject: [PATCH 14/17] style: Finished typing --- torchvision/io/__init__.py | 4 ++-- torchvision/io/_video_opt.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index da2fed01f29..f5070a6df90 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Iterator +from typing import Any, Dict import torch @@ -132,7 +132,7 @@ def __next__(self) -> Dict[str, Any]: raise StopIteration return {"data": frame, "pts": pts} - def __iter__(self) -> Iterator["VideoReader"]: + def __iter__(self) -> "VideoReader": return self def seek(self, time_s: float, keyframes_only: bool = False) -> "VideoReader": diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 1ae8ae66d6d..eab38aef698 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -445,7 +445,7 @@ def _convert_to_sec( def _read_video( filename: str, - start_pts: int = 0, + start_pts: Union[float, Fraction] = 0, end_pts: Optional[Union[float, Fraction]] = None, pts_unit: str = "pts" ) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]: From 4c5246cae670361822f3b2267d3132ea2c26e0b5 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 17 Nov 2021 01:11:01 +0100 Subject: [PATCH 15/17] style: ufmt the file --- torchvision/io/_video_opt.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index eab38aef698..91d7b75cb68 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -111,9 +111,7 @@ def _fill_info( def _align_audio_frames( - aframes: torch.Tensor, - aframe_pts: torch.Tensor, - audio_pts_range: Tuple[int, int] + aframes: torch.Tensor, aframe_pts: torch.Tensor, audio_pts_range: Tuple[int, int] ) -> torch.Tensor: start, end = aframe_pts[0], aframe_pts[-1] num_samples = aframes.size(0) @@ -431,12 +429,9 @@ def _probe_video_from_memory( def _convert_to_sec( - start_pts: Union[float, Fraction], - end_pts: Union[float, Fraction], - pts_unit: str, - time_base: Fraction + start_pts: Union[float, Fraction], end_pts: Union[float, Fraction], pts_unit: str, time_base: Fraction ) -> Tuple[Union[float, Fraction], Union[float, Fraction], str]: - if pts_unit == 'pts': + if pts_unit == "pts": start_pts = float(start_pts * time_base) end_pts = float(end_pts * time_base) pts_unit = "sec" @@ -447,7 +442,7 @@ def _read_video( filename: str, start_pts: Union[float, Fraction] = 0, end_pts: Optional[Union[float, Fraction]] = None, - pts_unit: str = "pts" + pts_unit: str = "pts", ) -> Tuple[torch.Tensor, torch.Tensor, Dict[str, float]]: if end_pts is None: end_pts = float("inf") @@ -515,8 +510,7 @@ def get_pts(time_base): def _read_video_timestamps( - filename: str, - pts_unit: str = "pts" + filename: str, pts_unit: str = "pts" ) -> Tuple[Union[List[int], List[Fraction]], Optional[float]]: if pts_unit == "pts": warnings.warn( From c65cb5334fc1091bff732ff60cb2534a0957f3b6 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 17 Nov 2021 23:35:31 +0100 Subject: [PATCH 16/17] style: Removed unnecessary typing --- torchvision/io/_video_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/io/_video_opt.py b/torchvision/io/_video_opt.py index 91d7b75cb68..45bec44ec61 100644 --- a/torchvision/io/_video_opt.py +++ b/torchvision/io/_video_opt.py @@ -44,7 +44,7 @@ class VideoMetaData: "audio_duration": float, "audio_sample_rate": float, } - __slots__: List[str] = [ + __slots__ = [ "has_video", "video_timebase", "video_duration", From b6aca1fe166784351565adc6916f707f8b967d36 Mon Sep 17 00:00:00 2001 From: frgfm Date: Wed, 17 Nov 2021 23:35:48 +0100 Subject: [PATCH 17/17] style: Fixed typing of iterator --- torchvision/io/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index f5070a6df90..382e06fb4f2 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -1,4 +1,4 @@ -from typing import Any, Dict +from typing import Any, Dict, Iterator import torch @@ -132,7 +132,7 @@ def __next__(self) -> Dict[str, Any]: raise StopIteration return {"data": frame, "pts": pts} - def __iter__(self) -> "VideoReader": + def __iter__(self) -> Iterator[Dict[str, Any]]: return self def seek(self, time_s: float, keyframes_only: bool = False) -> "VideoReader":