Skip to content

fix(vision): correct LCM serialization for detection class_id and results_length#1368

Open
jongmoon-konglabs wants to merge 3 commits intodimensionalOS:mainfrom
jongmoon-konglabs:fix/vision-msg-detection-class-id-str
Open

fix(vision): correct LCM serialization for detection class_id and results_length#1368
jongmoon-konglabs wants to merge 3 commits intodimensionalOS:mainfrom
jongmoon-konglabs:fix/vision-msg-detection-class-id-str

Conversation

@jongmoon-konglabs
Copy link

Problem

The robot's detection data was not being correctly transmitted via LCM due to two primary serialization bugs:

  1. Type Mismatch: ObjectHypothesis.class_id is defined as a string in the LCM schema, but we were passing an int. This caused an AttributeError: 'int' object has no attribute 'encode' during the lcm_encode() process.
  2. Incorrect Length: results_length in the Detection2D message was either hardcoded or incorrectly handled, leading to empty detection arrays or index errors during deserialization.
  3. Incomplete Restoration: When decoding from LCM, the class_id (string) was not being converted back to an int, causing type consistency issues within the Detection2DBBox object.

Solution

  • Type Casting: Modified to_ros_detection2d in both Detection2DBBox and Detection2DPoint to explicitly cast class_id to str() for LCM encoding.
  • Dynamic Results Length: Updated the ROSDetection2D message creation to set results_length=len(results) dynamically, ensuring the LCM library knows exactly how many objects to serialize.
  • Data Restoration: Updated from_ros_detection2d to cast the decoded string class_id back to an int to maintain compatibility with the rest of the perception pipeline.
  • Unit Testing: Added a suite of mock-based unit tests in test_imageDetections2D.py that validates the full LCM round-trip (encode -> decode) without requiring a physical camera or heavy fixtures.

Breaking Changes

None

How to Test

  1. Unit Tests: Run the new serialization tests to verify the fix:
    pytest dimos/perception/detection/type/detection2d/test_imageDetections2D.py

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 25, 2026

Greptile Summary

Fixed critical LCM serialization bugs that prevented detection data transmission. The PR correctly addresses three issues: converts class_id from int to string for LCM encoding, sets results_length dynamically instead of hardcoding, and restores class_id back to int after LCM decoding.

Key Changes:

  • to_ros_detection2d() in both Detection2DBBox and Detection2DPoint now explicitly converts class_id to string using str() before LCM serialization
  • Added dynamic results_length=len(results) to ensure correct array sizing for LCM
  • from_ros_detection2d() converts decoded string class_id back to int with validation using .lstrip("-").isdigit()
  • Comprehensive unit tests validate the full encode → decode roundtrip for multiple class IDs

Minor Note:
The validation logic in bbox.py:376 uses .lstrip("-").isdigit() which could theoretically accept malformed strings like "--5". Consider using a more robust pattern like str(hypothesis.class_id).lstrip("-").replace("-", "", 1).isdigit() or exception handling with try/except for int conversion.

Confidence Score: 4/5

  • This PR is safe to merge with one minor consideration regarding edge case handling
  • The changes correctly address the LCM serialization issues with comprehensive unit tests. The string conversion logic handles negative numbers properly, and the dynamic results_length fixes the array length bug. One minor concern is the validation logic uses .lstrip("-").isdigit() which could accept strings like "--5", though this is unlikely in practice.
  • Pay close attention to bbox.py line 376 - the string validation logic could be more robust

Important Files Changed

Filename Overview
dimos/perception/detection/type/detection2d/bbox.py Adds proper class_id string conversion for LCM serialization and dynamic results_length - includes string parsing logic with .lstrip("-").isdigit() for robustness
dimos/perception/detection/type/detection2d/point.py Adds class_id string conversion and dynamic results_length for LCM serialization - straightforward change matching bbox.py pattern
dimos/perception/detection/type/detection2d/test_imageDetections2D.py Adds comprehensive unit tests for LCM serialization roundtrip with parametrized test cases - validates encoding, decoding, and type conversions

Last reviewed commit: 1c0ff7d

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

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.

1 participant