为什么ROS publisher不发布第一条消息?

2024-09-29 01:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个出版商出版了两个主题为“图像”和“深度”的图像,还有一个订阅者正在收听这两个主题。在

发布程序从两个文件夹中读取图像,并在同一个循环中发布这两个文件夹。每个图像都有一个对应的深度,这两个图像都用相同的名称映射。发布速率为1hz。在

订阅者没有得到第一对图像。我试图将订阅的图像转储到一个文件夹中。所有发布的图片都被丢弃了,除了第一对图片。在

这是出版商的代码

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import os


def talker():

    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(1) # 1hz
    bridge = CvBridge()
    hello_str = "hello world %s" % rospy.get_time()
    rospy.loginfo(hello_str)
    path = "/home/test_img/"
    imglist = os.listdir(path)
    path_depth = "/home/out_depth/"


    for i in range(0,len(imglist)):
        topic = 'image'
        print(topic)
        pub = rospy.Publisher(topic, Image, queue_size=10)
        fn = path+imglist[i]
        print(fn)
        img = cv2.imread(fn)
        imgMsg = bridge.cv2_to_imgmsg(img, "bgr8")
        pub.publish(imgMsg)




        topic = 'depth'
        print(topic)
        pub = rospy.Publisher(topic, Image, queue_size=10)
        fn = path_depth+imglist[i]
        print(fn)
        img = cv2.imread(fn)
        imgMsg = bridge.cv2_to_imgmsg(img, "bgr8")
        pub.publish(imgMsg)
        rate.sleep()




if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

这是订户的代码

^{pr2}$

执行发布程序时,将列出所有图像(从打印(fn)行)。但是订阅者没有得到第一对图像。在

我试过用“rosrecord”记录信息。它也没有得到第一对。在

原因是什么?在


Tags: path图像import文件夹imgtopiccv2rospy