A sequence of pre-rendered frames used in integration tests.
| 68 | |
| 69 | |
| 70 | class _FrameSequence: |
| 71 | """A sequence of pre-rendered frames used in integration tests.""" |
| 72 | |
| 73 | _ASSETS_DIR = 'assets' |
| 74 | _FRAMES_DIR = 'frames' |
| 75 | _FILENAME_TEMPLATE = 'frame_{frame_num:03}.png' |
| 76 | |
| 77 | def __init__(self, |
| 78 | name, |
| 79 | xml_string, |
| 80 | camera_specs, |
| 81 | num_frames=20, |
| 82 | steps_per_frame=10, |
| 83 | seed=0): |
| 84 | """Initializes a new `_FrameSequence`. |
| 85 | |
| 86 | Args: |
| 87 | name: A string containing the name to be used for the sequence. |
| 88 | xml_string: An MJCF XML string containing the model to be rendered. |
| 89 | camera_specs: A list of `_CameraSpec` instances specifying the cameras to |
| 90 | render on each frame. |
| 91 | num_frames: The number of frames to render. |
| 92 | steps_per_frame: The interval between frames, in simulation steps. |
| 93 | seed: Integer or None, used to initialize the random number generator for |
| 94 | generating actions. |
| 95 | """ |
| 96 | self._name = name |
| 97 | self._xml_string = xml_string |
| 98 | self._camera_specs = camera_specs |
| 99 | self._num_frames = num_frames |
| 100 | self._steps_per_frame = steps_per_frame |
| 101 | self._seed = seed |
| 102 | |
| 103 | @property |
| 104 | def num_cameras(self): |
| 105 | return len(self._camera_specs) |
| 106 | |
| 107 | def iter_render(self): |
| 108 | """Returns an iterator that yields newly rendered frames as numpy arrays.""" |
| 109 | random_state = np.random.RandomState(self._seed) |
| 110 | physics = mujoco.Physics.from_xml_string(self._xml_string) |
| 111 | action_spec = mujoco.action_spec(physics) |
| 112 | for _ in range(self._num_frames): |
| 113 | for _ in range(self._steps_per_frame): |
| 114 | actions = random_state.uniform(action_spec.minimum, action_spec.maximum) |
| 115 | physics.set_control(actions) |
| 116 | physics.step() |
| 117 | for camera_spec in self._camera_specs: |
| 118 | yield physics.render(**camera_spec._asdict()) |
| 119 | |
| 120 | def iter_load(self): |
| 121 | """Returns an iterator that yields saved frames as numpy arrays.""" |
| 122 | for directory, filename in self._iter_paths(): |
| 123 | path = os.path.join(directory, filename) |
| 124 | yield _load_pixels(path) |
| 125 | |
| 126 | def save(self): |
| 127 | """Saves a new set of golden output frames to disk.""" |
no outgoing calls
no test coverage detected
searching dependent graphs…