0
$\begingroup$

I'm currently working with ROS2 and Python to process image data stored in a ROS2 bag file. My task involves extracting images from the bag, performing modifications, and then reinserting them. I am using the rosbags.rosbag2 library to interact with the ROS2 bag files and cv_bridge to convert ROS2 images to OpenCV format (and vice versa).

Here is the specific part of my environment and task setup:

  • Operating System: Ubuntu 22.04
  • Programming Language: Python 3.10.12

However, I've encountered an AttributeError when attempting to serialize the modified image back into the ROS2 message format. Here is the relevant portion of my code:

from cv_bridge import CvBridge
from rosbags.rosbag2 import Reader, Writer
from rosbags.typesys import Stores, get_typestore

bridge = CvBridge()
typestore = get_typestore(Stores.LATEST)
Image = typestore.types["sensor_msgs/msg/Image"]

with Reader(INPUT_BAGFILE) as reader, Writer(OUTPUT_BAGFILE) as writer:
    writer_connection = writer.add_connection("/d435i/color/image_raw", 
                                              "sensor_msgs/msg/Image", 
                                              typestore=typestore)
    
    for connection, timestamp, rawdata in reader.messages():
        if connection.topic == "/d435i/color/image_raw":
            msg = typestore.deserialize_cdr(rawdata, connection.msgtype)
            img = bridge.imgmsg_to_cv2(msg, desired_encoding="rgb8")

            # modify image here

            bridged_image = bridge.cv2_to_imgmsg(img, encoding="rgb8")
            image_message = Image(
                bridged_image.header,
                bridged_image.height,
                bridged_image.width,
                bridged_image.encoding,
                bridged_image.is_bigendian,
                bridged_image.step,
                bridged_image.data
            )

            new_msg = typestore.serialize_cdr(image_message, "sensor_msgs/msg/Image")
            writer.write(writer_connection, timestamp, new_msg)

In this case, the OUPUT_BAGFILE is a copy of the INPUT_BAGFILE. The error I receive is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [13], in <cell line: 4>()
     13 bridged_image = bridge.cv2_to_imgmsg(img, encoding="rgb8")
     14 image_message = Image(
     15     bridged_image.header,
     16     bridged_image.height,
   (...)
     21     bridged_image.data
     22 )
---> 24 new_msg = typestore.serialize_cdr(image_message, 'sensor_msgs/msg/Image')
     25 writer.write(writer_connection, timestamp, new_msg)

File ~/.local/lib/python3.10/site-packages/rosbags/typesys/store.py:166, in Typestore.serialize_cdr(self, message, typename, little_endian)
    162 pack_into('BB', rawdata, 0, 0, little_endian)
    164 func = msgdef.serialize_cdr_le if little_endian else msgdef.serialize_cdr_be
--> 166 pos = func(rawdata[4:], 0, message, self)
    167 assert pos + 4 == size
    168 return rawdata.toreadonly()

File <string>:20, in serialize_cdr(rawdata, pos, message, typestore)

File <string>:20, in serialize_cdr(rawdata, pos, message, typestore)

File <string>:18, in serialize_cdr(rawdata, pos, message, typestore)

AttributeError: 'Time' object has no attribute 'sec'

I appreciate any insights or suggestions on how to resolve this error.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.