Getting error while publishing sensor_msgs/Image.msg type on topic and recording in bag file.

asked 2019-04-04 04:02:09 -0500

Ghost gravatar image

updated 2019-04-04 06:12:25 -0500

Hi, I am trying to record bag file through the command

rosbag record -O /home/maq/MAQ/my_seq3.bag /camera/rgb/image_color amera/rgb/camera_info /camera/depth/image

from terminal. I am running my nodes and publish on the above mentioned topics, for which the code is,

rgb_image = rgb_image.astype('uint8')
rgb = CvBridge().cv2_to_imgmsg(rgb_image, encoding="rgb8")
depth = CvBridge().cv2_to_imgmsg(depth_gt, encoding="32FC1")

depth_msg = make_depth_msg(depth, rgb_time_sec, rgb_time_nsec, test_image)
rgb_msg = make_rgb_msg(rgb, rgb_time_sec, rgb_time_nsec, test_image)

pub_depth = rospy.Publisher('/camera/depth/image', Image, queue_size=1000)
pub_camera_info = rospy.Publisher('/camera/rgb/camera_info', CameraInfo, queue_size=1000)
pub_rgb = rospy.Publisher('/camera/rgb/image_color', Image, queue_size=1000)

pub_depth.publish(depth_msg)
pub_camera_info.publish(camera_info_msg)
pub_rgb.publish(rgb_msg)

Once I run this python file in pycharm it is publishing on all the topics without any error, but once I try to record them in bag file through the above mentioned command from terminal I get the following error.

Traceback (most recent call last):
 File "/home/maq/PycharmProjects/Deeper-Depth-Prediction_2/pytorch/result_collection_sequence.py", line 46, in <module>
   pub_depth.publish(depth_msg)
 File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 886, in publish
   raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data must be a list or tuple type

Can anyone help me please. Regards.

EDIT 1:

Sorry i should have added definitions of

make_depth_msg(..)

and

make_rgb_msg(..)

earlier:

def make_rgb_msg(rgb, rgb_time_sec, rgb_time_nsec, test_image):

   rgb_msg = Image()

   rgb_msg.header.seq = test_image
   rgb_msg.header.frame_id = "/openni_rgb_optical_frame"
   rgb_msg.header.stamp.secs = rgb_time_sec
   rgb_msg.header.stamp.nsecs = rgb_time_nsec

   rgb_msg.step = 1920
   rgb_msg.is_bigendian = 0
   rgb_msg.encoding = 'rgb8'

   rgb_msg.data = rgb

   return rgb_msg


def make_depth_msg(depth, rgb_time_sec, rgb_time_nsec, test_image):

   depth_msg = Image()

   depth_msg.header.seq = test_image
   depth_msg.header.frame_id = "/openni_rgb_optical_frame"
   depth_msg.header.stamp.secs = rgb_time_sec
   depth_msg.header.stamp.nsecs = rgb_time_nsec-20000000

   depth_msg.step = 2560
   depth_msg.is_bigendian = 0
   depth_msg.encoding = '32FC1'

   depth_msg.data = depth

   return depth_msg

And gvdhoorn is right with rostopic echo /camera/depth/imagesame problems occurs.

Now added .step, .is_bigendian and .encoding as well in each definition. But problem is same.

edit retag flag offensive close merge delete

Comments

I have the impression the issue is in your script, not in rosbag. The title of your question seems to suggest there is a problem with recording (ie: with rosbag).

It's likely that using rostopic echo .. will result in the same problem.

I suspect make_depth_msg(..) and/or make_rgb_msg(..).

gvdhoorn gravatar image gvdhoorn  ( 2019-04-04 05:00:23 -0500 )edit

Thanks for early reply. I have edited my question with their definitions.

Ghost gravatar image Ghost  ( 2019-04-04 05:17:38 -0500 )edit

Could be this line in make_depth_msg(..):

depth_msg.data = depth

If you don't set depth_msg.data, does the problem still occur?

And just to check: test_image is an integer, correct?

gvdhoorn gravatar image gvdhoorn  ( 2019-04-04 05:33:46 -0500 )edit

Yes it is this line depth_msg.data = depth and rgb_msg.data = rgb this line in definitions. But by these i want to save depth and msg data in sensor_msgs/Image.msg format and then publish it. What is wrong in the way i am doing. And yes test_image is an integer.

Thanks again

Ghost gravatar image Ghost  ( 2019-04-04 05:40:46 -0500 )edit

I don't know if you have already solved it, but for others, I solved this issue by converting the image into list() format before assigning to data field: rgb_msg.data = rgb.ravel().tolist() and depth_msg.data = depth.ravel().tolist().

francesco94 gravatar image francesco94  ( 2020-05-25 08:34:18 -0500 )edit