How to publish batch of images in a python node with ROS 2?
I have a publisher in ROS 2 which publishes an image message as following:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
import cv2
import numpy as np
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Image, 'Image', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
self.im_list = []
self.cv_image = cv2.imread('test.jpg') ### an RGB image
self.bridge = CvBridge()
def timer_callback(self):
self.publisher_.publish(self.bridge.cv2_to_imgmsg(np.array(self.cv_image), "bgr8"))
self.get_logger().info('Publishing an image')
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
It works fine with a single image. But I want to publish a batch of images with the given shape:
[12, 3, 224, 224] => [batch, channel, width, height]
I tried to send it as a list of images but it failed. Also, I wanted to build a custom message as following but it doesn't work too:
uint32 height 224
uint32 width 224
string encoding "bgr8"
uint8[150528] data
More info:
python 3.6
ROS 2 - eloquent (build from source)
Ubuntu 18
How should I pass a batch of images to the subscriber?
I wouldn't know what's causing this, but for anyone to be able to help you: please describe what you observed.
"It doesn't work" or "it failed" is insufficient.
Thank you for your advice. It's fixed now and I'll add the answer asap. Unfortunately, I can't reproduce the error now.