-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrecoder.py
29 lines (24 loc) · 886 Bytes
/
recoder.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
import cv2
import imageio
import os
from utils.train import make_dir
class VideoRecorder(object):
def __init__(self, root_dir, height=304, width=304, camera_id=0, fps=5):
self.save_dir = make_dir(root_dir, 'video') if root_dir else None
self.height = height
self.width = width
self.camera_id = camera_id
self.fps = fps
self.frames = []
def init(self, enabled=True):
self.frames = []
self.enabled = self.save_dir is not None and enabled
def record(self, env):
if self.enabled:
frame = env.render(mode='rgb_array')
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
self.frames.append(frame)
def save(self, file_name):
if self.enabled:
path = os.path.join(self.save_dir, file_name)
imageio.mimsave(path, self.frames, fps=self.fps)