-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7872b7
commit 0c9744e
Showing
3 changed files
with
18 additions
and
16 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
"""Create a movie of an agent interacting with an environment.""" | ||
|
||
import cv2 | ||
import torch | ||
|
||
|
||
def create_movie(environment, agent, path, fps=60): | ||
def create_movie(environment, agent, path="./live-preview.gif", skip=4, fps=50): | ||
"""Created by Mistral Large.""" | ||
initial = agent.preprocess(environment.reset()[0]) | ||
try: | ||
states = torch.cat([initial] * agent.shape["reshape"][1], dim=1) | ||
except AttributeError: | ||
states = initial | ||
|
||
try: | ||
done = False | ||
done = False | ||
|
||
# Get the dimensions of the first image | ||
height, width, channels = environment.render().shape | ||
height, width, _ = environment.render().shape | ||
fourcc = cv2.VideoWriter_fourcc(*"MJPG") # noqa | ||
movie = cv2.VideoWriter(path, fourcc, fps, (width, height)) | ||
|
||
# Create the VideoWriter object | ||
fourcc = cv2.VideoWriter_fourcc(*"MJPG") # You can change the codec if needed | ||
video_writer = cv2.VideoWriter(path, fourcc, fps, (width, height)) | ||
while not done: | ||
_, states, _, done = agent.observe(environment, states) | ||
video_writer.write(environment.render()) | ||
except Exception as e: | ||
print(f"Error during image generation or writing: {e}") | ||
return | ||
while not done: | ||
_, states, _, done = agent.observe(environment, states, skip) | ||
movie.write(environment.render()) | ||
|
||
cv2.destroyAllWindows() |
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