-
Notifications
You must be signed in to change notification settings - Fork 190
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
Changes from 21 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 b5d5228
Merge branch 'huawei-noah:develop' into develop
AisenGinn c714a13
Merge branch 'huawei-noah:develop' into develop
AisenGinn 6bec9a9
Merge branch 'huawei-noah:develop' into develop
AisenGinn 52eeafc
Merge branch 'huawei-noah:develop' into develop
AisenGinn 12e99d8
add header to gif_recorder.py and recorder_wrapper.py.
Gamenot d75e8f2
delete duplicated SMARTS inside example.
Gamenot ab9a08d
add docstring the gif_recorder.py and recorder_wrapper.py.
Gamenot 9cefb80
adding gif_recorder and recorder_wrapper into smarts.env.wrapper
Gamenot ece85c8
add header to gif_recorder.py and recorder_wrapper.py.
Gamenot e8f0b4d
delete duplicated SMARTS inside example.
Gamenot 0e381a2
add docstring the gif_recorder.py and recorder_wrapper.py.
Gamenot e48499d
modified docstring and setup.py
Gamenot 69c3c09
deal with conflict
Gamenot 7747b23
modified setup.py.
Gamenot d8f66bb
modified setup.py
Gamenot 350c41b
modified gif_recorder.
Gamenot 788a378
fixing import error
Gamenot 20ee644
fixed import errors.
Gamenot 309f904
GitHub Actions: Update requirements.txt
AisenGinn bfd428e
GitHub Actions: Format
AisenGinn ba3bb2f
fetch newest develop branch.
AisenGinn f4326a0
GitHub Actions: Update requirements.txt
AisenGinn f6decd0
modifying some class attributes names to be more clear, added documen…
AisenGinn d54f0ab
Merge branch 'develop' of https://github.com/AisenGinn/SMARTS into de…
AisenGinn e5c8c8f
modified import error notification in gif_recorder.py
AisenGinn 174f7ba
Merge branch 'develop' of https://github.com/huawei-noah/SMARTS into …
AisenGinn 111d9c5
added changedlog.
AisenGinn 43c4a3b
deleted unwanted file.
AisenGinn 6b4ddf3
modified workflows.
AisenGinn a543bb8
GitHub Actions: Update requirements.txt
AisenGinn 6c3a0ef
GitHub Actions: Format
AisenGinn b30cf41
moved import gif_recorder inside start_recording function to pass the…
AisenGinn 741cc6c
revert changing.
AisenGinn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# MIT License | ||
# | ||
# Copyright (C) 2022. Huawei Technologies Co., Ltd. All rights reserved. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
import os | ||
import sys | ||
|
||
try: | ||
from moviepy.editor import ImageClip, ImageSequenceClip | ||
except (ImportError, ModuleNotFoundError): | ||
print(sys.exc_info()) | ||
print( | ||
"You may not have installed the [gym] dependencies required to capture the video. Install them first using the command `pip install -e .[gym]` at the source directory." | ||
) | ||
import shutil | ||
import time | ||
from pathlib import Path | ||
|
||
|
||
class GifRecorder: | ||
""" | ||
Use images(rgb_array) to create a gif file. | ||
""" | ||
|
||
def __init__(self, dir, env): | ||
AisenGinn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timestamp_str = time.strftime("%Y%m%d-%H%M%S") | ||
self.dir = dir + "_" + timestamp_str | ||
self.env = env | ||
|
||
try: | ||
os.mkdir(self.dir) | ||
except: | ||
pass | ||
|
||
self._dir_name = str(Path(dir).name) | ||
|
||
def capture_frame(self, step_num, image): | ||
AisenGinn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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") | ||
|
||
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") | ||
AisenGinn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clip.close() | ||
|
||
def close_recorder(self): | ||
""" | ||
close the recorder by deleting the image folder. | ||
""" | ||
try: | ||
shutil.rmtree(self.dir) | ||
AisenGinn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# MIT License | ||
# | ||
# Copyright (C) 2022. Huawei Technologies Co., Ltd. All rights reserved. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
import os | ||
|
||
import gym | ||
import gym.envs | ||
|
||
from smarts.env.wrappers.gif_recorder import GifRecorder | ||
|
||
|
||
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): | ||
|
||
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): | ||
""" | ||
Reset the gym environment and restart recording. | ||
""" | ||
observations = super().reset(**kwargs) | ||
if self.recording == False: | ||
self.start_recording() | ||
|
||
return observations | ||
|
||
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) | ||
image = super().render(mode="rgb_array") | ||
self.gif_recorder.capture_frame(self.next_frame_id(), image) | ||
self.recording = True | ||
|
||
def stop_recording(self): | ||
""" | ||
Stop recording. | ||
""" | ||
self.recording = False | ||
|
||
def step(self, action): | ||
""" | ||
Step the environment using the action and record the next frame. | ||
""" | ||
observations, rewards, dones, infos = super().step(action) | ||
if self.recording == True: | ||
image = super().render(mode="rgb_array") | ||
self.gif_recorder.capture_frame(self.next_frame_id(), image) | ||
|
||
return observations, rewards, dones, infos | ||
|
||
def next_frame_id(self): | ||
""" | ||
Get the id for next frame. | ||
""" | ||
self.current_frame += 1 | ||
return self.current_frame | ||
|
||
def close(self): | ||
""" | ||
Close the recorder by deleting the image folder and generate the gif file. | ||
""" | ||
if self.gif_recorder is not None: | ||
self.gif_recorder.generate_gif() | ||
self.gif_recorder.close_recorder() | ||
self.gif_recorder = None | ||
self.recording = False | ||
|
||
def __del__(self): | ||
self.close() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my last concern here is that this
try/catch
swallows the import error. I think we still want to raise an error here. Aside from that we should not expect that the user is working from a dev platform.