Skip to content

Commit

Permalink
modifying some class attributes names to be more clear, added documen…
Browse files Browse the repository at this point in the history
…tation to clarify each path.
  • Loading branch information
AisenGinn committed Nov 17, 2022
1 parent ba3bb2f commit f6decd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
36 changes: 21 additions & 15 deletions smarts/env/wrappers/gif_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# THE SOFTWARE.
import os
import sys
import typing
import gym
import numpy as np

try:
from moviepy.editor import ImageClip, ImageSequenceClip
Expand All @@ -39,38 +42,41 @@ class GifRecorder:
Use images(rgb_array) to create a gif file.
"""

def __init__(self, dir, env):
def __init__(self, video_name_folder: str, env: gym.Env):
timestamp_str = time.strftime("%Y%m%d-%H%M%S")
self.dir = dir + "_" + timestamp_str
self.frame_folder = (
video_name_folder + "_" + timestamp_str
) # folder that uses to contain temporary frame images, will be deleted after the gif is created.
self.env = env

try:
os.mkdir(self.dir)
except:
pass
Path.mkdir(
Path(self.frame_folder), exist_ok=True
) # create temporary frame images folder if not exists.

self._dir_name = str(Path(dir).name)
self._video_root_path = str(
Path(video_name_folder).parent
) # path of the video file
self._video_name = str(Path(video_name_folder).name) # name of the video

def capture_frame(self, step_num, image):
def capture_frame(self, step_num: int, image: np.ndarray):
"""
Create image according to the rgb_array and store it with step number in the destinated folder
"""
with ImageClip(image) as image_clip:
image_clip.save_frame(f"{self.dir}/{self._dir_name}_{step_num}.jpeg")
image_clip.save_frame(
f"{self.frame_folder}/{self._video_name}_{step_num}.jpeg"
)

def generate_gif(self):
"""
Use the images in the same folder to create a gif file.
"""
with ImageSequenceClip(self.dir, fps=10) as clip:
clip.write_gif(f"videos/{self._dir_name}.gif")
with ImageSequenceClip(self.frame_folder, fps=10) as clip:
clip.write_gif(f"{self._video_root_path}/{self._video_name}.gif")
clip.close()

def close_recorder(self):
"""
close the recorder by deleting the image folder.
"""
try:
shutil.rmtree(self.dir)
except:
pass
shutil.rmtree(self.frame_folder, ignore_errors=True)
22 changes: 14 additions & 8 deletions smarts/env/wrappers/recorder_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import typing

import gym
import gym.envs

from pathlib import Path
from smarts.env.wrappers.gif_recorder import GifRecorder


Expand All @@ -32,16 +34,20 @@ class RecorderWrapper(gym.Wrapper):
A Wrapper that interacts the gym environment with the GifRecorder to record video step by step.
"""

def __init__(self, dir, env):
def __init__(self, video_name: str, env: gym.Env):

try:
os.mkdir("videos")
except:
pass
root_path = Path(__file__).parents[3] # smarts main repo path
video_folder = os.path.join(
root_path, "videos"
) # video folder for all video recording file (.gif)
Path.mkdir(
Path(video_folder), exist_ok=True
) # create video folder if not exist

super().__init__(env)
# assert "rgb_array" in env.metadata.get("render_modes", [])
self.dir = "videos/" + dir
self.video_name_folder = os.path.join(
video_folder, video_name
) # frames folder that uses to contain temporary frame images, will be created using video name and current time stamp in gif_recorder when recording starts
self.gif_recorder = None
self.recording = False
self.current_frame = -1
Expand All @@ -61,7 +67,7 @@ def start_recording(self):
Start the gif recorder and capture the first frame.
"""
if self.gif_recorder is None:
self.gif_recorder = GifRecorder(self.dir, self.env)
self.gif_recorder = GifRecorder(self.video_name_folder, self.env)
image = super().render(mode="rgb_array")
self.gif_recorder.capture_frame(self.next_frame_id(), image)
self.recording = True
Expand Down

0 comments on commit f6decd0

Please sign in to comment.