Skip to content

fix: parse ROS image trailers and expose RealSense depth units - #155

Open
theluckystrike wants to merge 1 commit into
petercorke:mainfrom
theluckystrike:fix/rosbag-realsense-trailer
Open

theluckystrike wants to merge 1 commit into
petercorke:mainfrom
theluckystrike:fix/rosbag-realsense-trailer

Conversation

@theluckystrike

Copy link
Copy Markdown

Closes #151

ROSBag guessed how many trailing bytes to strip from each image message, so RealSense bags lost their depth units footer and a RealSense bag with no trailer (Intel's single_depth_color_640x480.bag layout) would have had 4 real bytes chopped off.

What changed, following the checklist in the issue

  • Image and CompressedImage are parsed by hand up to the end of the data array (header, height, width, encoding, is_bigendian, step, then the array length), for both ROS1 and CDR. Only raw[:end] goes to the type store, n == height * step is checked, and legal CDR alignment padding is accepted. The old 0/4/8 probe is kept only as a fallback for other message types.
  • RealSense style fixtures (with a 4-byte footer and with none) are generated in tests/create_ros_fixture.py, with a regression test for a bag that has StreamInfo and 0 extra bytes.
  • A 4-byte footer is kept as float32 depth_units on the sample (depth frames carry the scale, colour frames 0.0).
  • Non image connections such as StreamInfo and CameraInfo go through the fallback trailer check.
  • print in this path is replaced with logging.

Depth units API

ROSBag(..., depth_units=None | "auto" | float, invalid_is_nan=True), where depth_units is metres per native unit as in librealsense. The docstring notes that this is the reciprocal of the Open3D and TUM RGB-D depth_scale (1000 and 5000).

The scale comes from, in order, a user float, the per message footer, the bag's .../option/Depth_Units/value topic, the REP 118 encoding (16UC1 is millimetres, 32FC1 is metres), then unknown.

  • None (default) returns the native dtype unchanged with depth_units attached as metadata.
  • "auto" or a float converts to float32 metres, and raw 0 becomes NaN when invalid_is_nan is true. For 32FC1 a user float acts as a gain.

Type registration for bag connections now distinguishes ROS1 .msg from ROS2 IDL definitions, registers only missing types and skips incompatible typestore candidates during auto detection, so an unrelated or custom IDL type can't abort reading the images.

Tests

pytest -q tests/test_ros.py

301 tests pass in tests/test_ros.py and the full suite passes locally. Ruff reports no new findings in the changed files.

One related thing I left alone because it behaves the same on main. The per message handlers catch KeyError and AssertionError but not rosbags' SerdeError, so a bag with an undecodable non image message still stops iteration. I can follow up in #150 if you want that handled.

ROSBag no longer guesses how many trailing bytes to strip. Image and CompressedImage messages are parsed up to the end of their data array for ROS1 and CDR, legal CDR padding is accepted, and a 4-byte RealSense footer is decoded as depth_units instead of being dropped. ROSBag gains depth_units (None, 'auto' or a float, metres per unit) and invalid_is_nan, with the precedence described in the issue.

Closes petercorke#151
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 64 complexity · 0 duplication

Metric Results
Complexity 64
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ROSBag: RealSense bags have a 4-byte depth-units trailer that is guessed, not parsed

1 participant