Robotics StackExchange | Archived questions

rospy - strange behavior while publishing images

Dear all,

I have a problem with the following code. In particular, I would like to have a node made by a simple publisher/subscriber in order to read images, do some processing and publish an output.

image_pub = rospy.Publisher("/mycode/outputImage", Image, queue_size=1)

def main():
   rospy.init_node('node', anonymous=True)
   print('Subscribed. Waiting for images.')
   rospy.Subscriber("/usb_cam/image_raw", Image, image_callback, None, queue_size=1)
   rospy.spin()

def image_callback(msg):
   cv2_img = bridge.imgmsg_to_cv2(msg, "bgr8")
   time.sleep(1)
   image_pub.publish(bridge.cv2_to_imgmsg(cv2_img, "bgr8"))

In the node that I created to visualize the output (but also if I publish other message made by simple vectors, I see a continuous delay of around 10 seconds, instead of a static delay of 1 second as I was expecting. Moreover, it seems that it is not throwing frames and process the last one, but all the frames that are being processed are the old one captured. If I remove the sleep(1), I get a correct streaming effect. Is this a normal effect? In case it is, since my goal is to do some processing expected to run at 4/5 fps without ROS, how can I do in order to process the last frame grabbed instead of these very old frames?

Thank you

Asked by darioc1985 on 2019-02-12 09:24:37 UTC

Comments

this is weird. try rospy.sleep(1.). Totally not sure if this will help :)

Asked by kolya_rage on 2019-02-12 17:36:02 UTC

thank you for your help. The problem is that if I put any form of computation, I see this delay. In my code, I have some computation (of around 200msec) instead of the sleep. But with both sleep and my code cause that the next frame processed is some that seems old instead of the current one.

Asked by darioc1985 on 2019-02-12 18:32:47 UTC

Answers

Declaring the subscriber in such a way

self.subscriber = rospy.Subscriber("/usb_cam/image_raw/compressed", CompressedImage, self.image_callback, None, queue_size=1, buff_size=921600)

the program seems to have the right behavior. I am wondering if the buffer size must always be properly passed, or it is an anmalous behavior of ROS, also in the light of https://answers.ros.org/question/50112/unexpected-delay-in-rospy-subscriber/

Asked by darioc1985 on 2019-02-14 03:38:51 UTC

Comments