-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
52 lines (40 loc) · 1.47 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from pathlib import Path
import cv2
import numpy as np
from tqdm.auto import tqdm
def gpu_is_available_tensorflow():
from baselines.common.tf_util import get_available_gpus
return bool(get_available_gpus())
def maybe_tqdm(progress=False):
if progress:
return tqdm
else:
return lambda it, *args, **kwargs: it
def assert_equal(*args):
assert all(args[0] == args[i] for i in range(1, len(args))), args
class VideoWriter:
def __init__(self, path: Path, fps: float = 30.0, fourcc: str = 'FFV1'):
assert len(fourcc) == 4
self.path = path
self.fps = float(fps)
self.fourcc = cv2.VideoWriter_fourcc(*fourcc)
self.out = None
self.frame_size = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self.out is not None:
self.out.release()
def write(self, frame):
assert len(frame.shape) == 3
assert frame.shape[2] == 3
assert frame.dtype == np.uint8
frame_size = (frame.shape[1], frame.shape[0])
if self.out is None:
self.path.parent.mkdir(exist_ok=True, parents=True)
self.frame_size = frame_size
args = [str(self.path), self.fourcc, self.fps, self.frame_size]
self.out = cv2.VideoWriter(*args)
else:
assert self.frame_size == frame_size, f"Wrong frame size: should be {self.frame_size}, got {frame_size}"
self.out.write(frame)