Georg commited on
Commit
84b67c7
·
1 Parent(s): bec2a82

Refactor depth snapshot processing in mujoco_server.py for metric depth representation

Browse files

- Updated the capture_depth_snapshot function to convert depth values from meters to millimeters, ensuring precision in depth rendering.
- Introduced dynamic handling of maximum depth based on the MuJoCo visualization settings, improving adaptability to different environments.
- Enhanced depth normalization and clipping to handle invalid values more effectively, resulting in more accurate depth images.

Files changed (1) hide show
  1. mujoco_server.py +16 -16
mujoco_server.py CHANGED
@@ -1957,22 +1957,22 @@ def capture_depth_snapshot(camera_name: str) -> Optional[bytes]:
1957
  # Close the temporary renderer
1958
  depth_renderer.close()
1959
 
1960
- # Convert depth to 16-bit PNG for precision
1961
- # Normalize to 0-65535 range
1962
- # Handle invalid/infinite values
1963
- depth_min = np.min(depth_array[np.isfinite(depth_array)])
1964
- depth_max = np.max(depth_array[np.isfinite(depth_array)])
1965
-
1966
- if depth_max > depth_min:
1967
- depth_normalized = np.clip(
1968
- (depth_array - depth_min) / (depth_max - depth_min),
1969
- 0, 1
1970
- )
1971
- else:
1972
- depth_normalized = np.zeros_like(depth_array)
1973
-
1974
- # Convert to 16-bit
1975
- depth_16bit = (depth_normalized * 65535).astype(np.uint16)
1976
 
1977
  # Encode as PNG
1978
  ret, buffer = cv2.imencode('.png', depth_16bit)
 
1957
  # Close the temporary renderer
1958
  depth_renderer.close()
1959
 
1960
+ # Convert depth to 16-bit PNG in millimeters (metric depth).
1961
+ # MuJoCo renderer returns depth in meters; preserve metric scale.
1962
+ max_depth_m = 10.0
1963
+ try:
1964
+ vis_map = getattr(env.model, "vis", None)
1965
+ vis_map = getattr(vis_map, "map", None) if vis_map is not None else None
1966
+ zfar = getattr(vis_map, "zfar", None) if vis_map is not None else None
1967
+ if zfar is not None:
1968
+ max_depth_m = float(zfar)
1969
+ except Exception:
1970
+ pass
1971
+ # uint16 in mm can represent up to 65.535 m
1972
+ max_depth_m = min(max_depth_m, 65.535)
1973
+ depth_m = np.nan_to_num(depth_array, nan=0.0, posinf=max_depth_m, neginf=0.0)
1974
+ depth_m = np.clip(depth_m, 0.0, max_depth_m)
1975
+ depth_16bit = (depth_m * 1000.0).round().astype(np.uint16)
1976
 
1977
  # Encode as PNG
1978
  ret, buffer = cv2.imencode('.png', depth_16bit)