Skip to content

Commit

Permalink
Add depth rendering from camera (openai#1114)
Browse files Browse the repository at this point in the history
* Add depth rendering from camera

Allow the mujoco renderer to return the depth image using the `depth_array` mode. 

It follows the `rgb_array` extraction method.

* Depth data is a 2d array.

* Add depth_array to render modes.
  • Loading branch information
23pointsNorth authored and pzhokhov committed Sep 21, 2018
1 parent 924d134 commit 7bbe486
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions gym/envs/mujoco/mujoco_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, model_path, frame_skip):
self._viewers = {}

self.metadata = {
'render.modes': ['human', 'rgb_array'],
'render.modes': ['human', 'rgb_array', 'depth_array'],
'video.frames_per_second': int(np.round(1.0 / self.dt))
}

Expand Down Expand Up @@ -112,6 +112,13 @@ def render(self, mode='human', width=DEFAULT_SIZE, height=DEFAULT_SIZE):
data = self._get_viewer(mode).read_pixels(width, height, depth=False)
# original image is upside-down, so flip it
return data[::-1, :, :]
elif mode == 'depth_array':
self._get_viewer(mode).render(width, height)
# window size used for old mujoco-py:
# Extract depth part of the read_pixels() tuple
data = self._get_viewer(mode).read_pixels(width, height, depth=True)[1]
# original image is upside-down, so flip it
return data[::-1, :]
elif mode == 'human':
self._get_viewer(mode).render()

Expand All @@ -126,8 +133,10 @@ def _get_viewer(self, mode):
if self.viewer is None:
if mode == 'human':
self.viewer = mujoco_py.MjViewer(self.sim)
elif mode == 'rgb_array':

elif mode == 'rgb_array' or mode == 'depth_array':
self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, -1)

self.viewer_setup()
self._viewers[mode] = self.viewer
return self.viewer
Expand Down

0 comments on commit 7bbe486

Please sign in to comment.