Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video recording utilities #1499

Merged
merged 34 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1899e75
adding gif_recorder and recorder_wrapper into smarts.env.wrapper
Gamenot Jul 11, 2022
b5d5228
Merge branch 'huawei-noah:develop' into develop
AisenGinn Jul 11, 2022
c714a13
Merge branch 'huawei-noah:develop' into develop
AisenGinn Jul 15, 2022
6bec9a9
Merge branch 'huawei-noah:develop' into develop
AisenGinn Jul 19, 2022
52eeafc
Merge branch 'huawei-noah:develop' into develop
AisenGinn Jul 22, 2022
12e99d8
add header to gif_recorder.py and recorder_wrapper.py.
Gamenot Jul 22, 2022
d75e8f2
delete duplicated SMARTS inside example.
Gamenot Jul 22, 2022
ab9a08d
add docstring the gif_recorder.py and recorder_wrapper.py.
Gamenot Jul 22, 2022
9cefb80
adding gif_recorder and recorder_wrapper into smarts.env.wrapper
Gamenot Jul 11, 2022
ece85c8
add header to gif_recorder.py and recorder_wrapper.py.
Gamenot Jul 22, 2022
e8f0b4d
delete duplicated SMARTS inside example.
Gamenot Jul 22, 2022
0e381a2
add docstring the gif_recorder.py and recorder_wrapper.py.
Gamenot Jul 22, 2022
e48499d
modified docstring and setup.py
Gamenot Jul 25, 2022
69c3c09
deal with conflict
Gamenot Jul 25, 2022
7747b23
modified setup.py.
Gamenot Jul 25, 2022
d8f66bb
modified setup.py
Gamenot Jul 25, 2022
350c41b
modified gif_recorder.
Gamenot Jul 25, 2022
788a378
fixing import error
Gamenot Jul 27, 2022
20ee644
fixed import errors.
Gamenot Jul 27, 2022
309f904
GitHub Actions: Update requirements.txt
AisenGinn Aug 16, 2022
bfd428e
GitHub Actions: Format
AisenGinn Aug 16, 2022
ba3bb2f
fetch newest develop branch.
AisenGinn Nov 17, 2022
f4326a0
GitHub Actions: Update requirements.txt
AisenGinn Nov 17, 2022
f6decd0
modifying some class attributes names to be more clear, added documen…
AisenGinn Nov 17, 2022
d54f0ab
Merge branch 'develop' of https://github.com/AisenGinn/SMARTS into de…
AisenGinn Nov 17, 2022
e5c8c8f
modified import error notification in gif_recorder.py
AisenGinn Nov 17, 2022
174f7ba
Merge branch 'develop' of https://github.com/huawei-noah/SMARTS into …
AisenGinn Nov 17, 2022
111d9c5
added changedlog.
AisenGinn Nov 21, 2022
43c4a3b
deleted unwanted file.
AisenGinn Nov 21, 2022
6b4ddf3
modified workflows.
AisenGinn Nov 21, 2022
a543bb8
GitHub Actions: Update requirements.txt
AisenGinn Nov 21, 2022
6c3a0ef
GitHub Actions: Format
AisenGinn Nov 21, 2022
b30cf41
moved import gif_recorder inside start_recording function to pass the…
AisenGinn Nov 21, 2022
741cc6c
revert changing.
AisenGinn Nov 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/SMARTS
Submodule SMARTS added at 9d9700
40 changes: 40 additions & 0 deletions smarts/env/wrappers/gif_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

from moviepy.editor import *
import gym.envs
import shutil


class GifRecorder:
def __init__(self, dir, env):
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
self.dir = dir
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
self.env = env

try:
os.mkdir(self.dir)
except:
pass

self.dir_name = None
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
if "/" not in dir:
self.dir_name = dir
else:
last_index = dir.rindex("/")
self.dir_name = dir[-(len(dir) - last_index - 1) :]
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved

def capture_frame(self, step_num):
image = self.env.render(mode="rgb_array")
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved

with ImageClip(image) as image_clip:
image_clip.save_frame(f"{self.dir}/{self.dir_name}_{step_num}.jpeg")

def generate_gif(self):
with ImageSequenceClip(self.dir, fps=10) as clip:
clip.write_gif(f"videos/{self.dir_name}.gif")
clip.close()

def close_recorder(self):
try:
shutil.rmtree(self.dir)
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
except:
pass
60 changes: 60 additions & 0 deletions smarts/env/wrappers/recorder_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os

from moviepy.editor import *
import gym
import gym.envs

from smarts.env.wrappers.gif_recorder import GifRecorder


class RecorderWrapper(gym.Wrapper):
def __init__(self, dir, env):

try:
os.mkdir("videos")
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
except:
pass

super().__init__(env)
# assert "rgb_array" in env.metadata.get("render_modes", [])
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved
self.dir = "videos/" + dir
self.gif_recorder = None
self.recording = False
self.current_frame = -1

def reset(self, **kwargs):
observations = super().reset(**kwargs)
if self.recording == False:
self.start_recording()

return observations

def start_recording(self):
if self.gif_recorder is None:
self.gif_recorder = GifRecorder(self.dir, self.env)
self.gif_recorder.capture_frame(self.next_frame_id())
self.recording = True

def stop_recording(self):
self.recording = False

def step(self, action):
observations, rewards, dones, infos = super().step(action)
if self.recording == True:
self.gif_recorder.capture_frame(self.next_frame_id())

return observations, rewards, dones, infos

def next_frame_id(self):
self.current_frame += 1
return self.current_frame

def close(self):
self.gif_recorder.close_recorder()
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved

def __del__(self):
self.gif_recorder.close_recorder()
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved

def close_recorder(self):
self.gif_recorder.generate_gif()
self.gif_recorder.close_recorder()
AisenGinn marked this conversation as resolved.
Show resolved Hide resolved