ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
2

How to publish batch of images in a python node with ROS 2?

asked 2020-08-09 04:41:22 -0600

Mahsa gravatar image

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?

edit retag flag offensive close merge delete

Comments

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.

gvdhoorn gravatar image gvdhoorn  ( 2020-08-10 00:44:58 -0600 )edit

Thank you for your advice. It's fixed now and I'll add the answer asap. Unfortunately, I can't reproduce the error now.

Mahsa gravatar image Mahsa  ( 2020-08-10 00:49:51 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-08-10 01:01:26 -0600

Mahsa gravatar image

Finally, I built a custom message as follows:

sensor_msgs/Image[ 2 ] data

where batch size is 2.
Then, add it to my publisher as follows:

#!/usr/bin/env python3
# Revision $Id$


import rclpy
from rclpy.node import Node
from custom_batch.msg import Batch #### custom message
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(Batch, 'Image', 10) ###### here
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0
        self.im_list = []
        self.cv_image1 = cv2.imread('3.jpg')
        self.cv_image2 = cv2.imread('2.jpg')
        self.bridge = CvBridge()

    def timer_callback(self):

        #### custom message
        my_msg = Batch()
        my_msg.data[0] = self.bridge.cv2_to_imgmsg(np.array(self.cv_image1), "bgr8")
        my_msg.data[1] = self.bridge.cv2_to_imgmsg(np.array(self.cv_image2), "bgr8")
        #####
        self.publisher_.publish(my_msg) ## custom message
        self.get_logger().info('Publishing a perfect pistachio ')

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()
edit flag offensive delete link more

Comments

Thank you for sharing your answer. Would you please publish your publish/subscribe on github? This will greatly help.

Hagar Usama gravatar image Hagar Usama  ( 2021-05-23 08:23:00 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-08-09 04:38:46 -0600

Seen: 2,954 times

Last updated: Aug 10 '20