ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Fastest way of publishing large ndarray image data as string message on ros2 rclpy

asked 2019-05-20 19:37:32 -0500

John Doenut gravatar image

This is a bit of a continuation from this question that I have asked earlier.

I'm trying to publish a message on ROS2 crystal, python3 with rclpy which is a cv2 image file or numpy nd.array data in the end. The size is a 3D array (1280, 720, 3) with (width, height, color channel) RGB8 which runs at 30 fps. The value in each matrix element is 0~255 so it fits right in uint8. I've been sending test data but realized the rate of publishing is way too slow for real time. The basic setup is

import cv2
import numpy as np
import rclpy
import sys
from std_msgs.msg import String

np.set_printoptions(threshold=sys.maxsize)
...
rclpy.init(args=None)
node = rclpy.create_node('cam_publish')
publisher = node.create_publisher(String, 'cam')
msg = String()

img = cv2.resize(raw_img, (960, 720))
msg.data = np.array2string(img)
publisher.publish(msg)

which hangs too long at the publisher.publish stage, taking something like 10~20 seconds to publish the data. The conversion from array to string is actually surprising fast which I thought would be the bottleneck. My ultimate goal is to capture this message in ros2 bag record.

How could I speed up the publishing of this data? Is there a way to say configure the QoS or another way to send message and publish data much faster? Could I encode the uint8 data into a String data to compress the data size? Any general suggestion to the approach to the problem is fantastic. I've heard in previous post that there are plans on implementing nd.array on the next ros2 release and this is actually an interesting problem to me personally that I'd like to work on, how could I contribute to current and future setup of ros2 rclpy message and publisher?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-05-21 07:55:16 -0500

Frank Dekervel gravatar image

You might want to run the master branch of ros2 instead of crystal (not too difficult to install) which already turns arrays in messages into numpy arrays.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-05-20 19:37:32 -0500

Seen: 1,151 times

Last updated: May 21 '19