class_docstr: Render context superclass for offscreen and window rendering.
class_docstr_tok: ["Render","context","superclass","for","offscreen","and","window","rendering","."]
code: def read_pixels(self, depth=True, segmentation=False):
      width, height = self.offwidth, self.offheight
      rect = mujoco.MjrRect(
          left=0, bottom=0, width=width, height=height
      )
  
      rgb_arr = np.zeros(3 * rect.width * rect.height, dtype=np.uint8)
      depth_arr = np.zeros(rect.width * rect.height, dtype=np.float32)
  
      mujoco.mjr_readPixels(rgb_arr, depth_arr, rect, self.con)
      rgb_img = rgb_arr.reshape(rect.height, rect.width, 3)
  
      ret_img = rgb_img
      if segmentation:
          seg_img = (
              rgb_img[:, :, 0]
              + rgb_img[:, :, 1] * (2**8)
              + rgb_img[:, :, 2] * (2**16)
          )
          seg_img[seg_img >= (self.scn.ngeom + 1)] = 0
          seg_ids = np.full(
              (self.scn.ngeom + 1, 2), fill_value=-1, dtype=np.int32
          )
  
          for i in range(self.scn.ngeom):
              geom = self.scn.geoms[i]
              if geom.segid != -1:
                  seg_ids[geom.segid + 1, 0] = geom.objtype
                  seg_ids[geom.segid + 1, 1] = geom.objid
          ret_img = seg_ids[seg_img]
  
      if depth:
          depth_img = depth_arr.reshape(rect.height, rect.width)
          return (ret_img, depth_img)
      else:
          return ret_img
  
imports: import collections
  import os
  import time
  from threading import Lock
  import glfw
  import imageio
  import mujoco
  import numpy
label_desc: Use image node_id 4 for calling the RenderContext obj's underlying member method code with example usage: obj.read_pixels(depth, segmentation) and returns: ret_img,  depth_img, ret_img