2727import os
2828from tempfile import TemporaryDirectory
2929from typing import Optional , Tuple
30+ import warnings
3031
3132import numpy as np
3233from tqdm .auto import tqdm
@@ -78,7 +79,8 @@ def __call__(self, x: np.ndarray, y: Optional[np.ndarray] = None) -> Tuple[np.nd
7879 """
7980 Apply video compression to sample `x`.
8081
81- :param x: Sample to compress of shape NCFHW or NFHWC. `x` values are expected to be in the data range [0, 255].
82+ :param x: Sample to compress of shape NCFHW or NFHWC. `x` values are expected to be either in range [0, 1] or
83+ [0, 255].
8284 :param y: Labels of the sample `x`. This function does not affect them in any way.
8385 :return: Compressed sample.
8486 """
@@ -92,6 +94,9 @@ def compress_video(x: np.ndarray, video_format: str, constant_rate_factor: int,
9294 video_path = os .path .join (dir_ , f"tmp_video.{ video_format } " )
9395 _ , height , width , _ = x .shape
9496
97+ if (height % 2 ) != 0 or (width % 2 ) != 0 :
98+ warnings .warn ("Codec might require even number of pixels in height and width." )
99+
95100 # numpy to local video file
96101 process = (
97102 ffmpeg .input ("pipe:" , format = "rawvideo" , pix_fmt = "rgb24" , s = f"{ width } x{ height } " )
@@ -118,11 +123,19 @@ def compress_video(x: np.ndarray, video_format: str, constant_rate_factor: int,
118123 x = np .transpose (x , (0 , 2 , 3 , 4 , 1 ))
119124
120125 # apply video compression per video item
126+ scale = 1
127+ if x .min () >= 0 and x .max () <= 1.0 :
128+ scale = 255
129+
121130 x_compressed = x .copy ()
122131 with TemporaryDirectory (dir = config .ART_DATA_PATH ) as tmp_dir :
123132 for i , x_i in enumerate (tqdm (x , desc = "Video compression" , disable = not self .verbose )):
133+ x_i *= scale
124134 x_compressed [i ] = compress_video (x_i , self .video_format , self .constant_rate_factor , dir_ = tmp_dir )
125135
136+ x_compressed = x_compressed / scale
137+ x_compressed = x_compressed .astype (x .dtype )
138+
126139 if self .channels_first :
127140 x_compressed = np .transpose (x_compressed , (0 , 4 , 1 , 2 , 3 ))
128141
0 commit comments