如何使用 ROS 2 在 python 节点中发布一批图像?
How to publish batch of images in a python node with ROS 2?
我在 ROS 2 中有一个发布者,发布如下图像消息:
#!/usr/bin/env python3
# Revision $Id$
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()
单张图片效果很好。但是我想发布一批具有给定形状的图像:
[12, 3, 224, 224] => [batch, channel, width, height]
我试图将其作为图像列表发送,但失败了。如何操作?
更多信息:
python 3.6
ROS 2 - eloquent (build from source)
Ubuntu 18
最后,我建立了一条自定义消息如下:
sensor_msgs/Image[ 2 ] data
批量大小为 2。
然后,将其添加到我的发布者中,如下所示:
#!/usr/bin/env python3
# Revision $Id$
import rclpy
from rclpy.node import Node
from custom_batch.msg import Batch #### import 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) #### change 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 batch of images')
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()
您可以在 ROS2 中找到有关自定义消息的有用说明 here。
我在 ROS 2 中有一个发布者,发布如下图像消息:
#!/usr/bin/env python3
# Revision $Id$
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()
单张图片效果很好。但是我想发布一批具有给定形状的图像:
[12, 3, 224, 224] => [batch, channel, width, height]
我试图将其作为图像列表发送,但失败了。如何操作?
更多信息:
python 3.6
ROS 2 - eloquent (build from source)
Ubuntu 18
最后,我建立了一条自定义消息如下:
sensor_msgs/Image[ 2 ] data
批量大小为 2。
然后,将其添加到我的发布者中,如下所示:
#!/usr/bin/env python3
# Revision $Id$
import rclpy
from rclpy.node import Node
from custom_batch.msg import Batch #### import 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) #### change 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 batch of images')
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()
您可以在 ROS2 中找到有关自定义消息的有用说明 here。