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.
- 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
|
| 1961 |
-
#
|
| 1962 |
-
|
| 1963 |
-
|
| 1964 |
-
|
| 1965 |
-
|
| 1966 |
-
|
| 1967 |
-
|
| 1968 |
-
|
| 1969 |
-
|
| 1970 |
-
|
| 1971 |
-
|
| 1972 |
-
|
| 1973 |
-
|
| 1974 |
-
|
| 1975 |
-
depth_16bit = (
|
| 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)
|