You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
we are publishing an image in ROS2 with a resolution of 600 by 600. Using rclpy in Python, the callback is very slow. We ran the ros2/ros1 bridge and made a very similar ROS1 subscriber and the performance is perfect.
We tried using opensplice and fastRTPS, but since using the ROS2/ROS1 bridge callback works perfectly, we are confident it isn't the transport layer and the problem is probably with the python subscriber cash.
The cam2image example uses such a low resolution that the problem doesn't appear.
I'm experiencing quite similar issue, although the enviroment is a bit different. My setup is VREP->ROS1->ROS2->rclpy. I receive decent fps with max resolution of 64*64. Anything more than that results in huge frame skips.
It seems that that VREP->ROS1 works ok. So does ROS1->ROS2 bridge.
Since environment is a bit tricky to reproduce, I'll try to come up with simple MWE.
MWE:
Publisher: ros2 run image_tools cam2image -x 1024 -y 512
Subscriber:
# coding=utf-8
import rclpy
import rclpy.node as node
import cv2
import numpy as np
import sensor_msgs.msg as msg
import std_msgs.msg as std_msg
import rclpy.qos as qos
import third_party.ros.ros as ros
class TestDisplayNode(node.Node):
def __init__(self):
super().__init__('IProc_TestDisplayNode')
self.__window_name = "img"
#profile = qos.QoSProfile(history=qos.QoSHistoryPolicy.RMW_QOS_POLICY_HISTORY_KEEP_ALL)
profile = qos.QoSProfile()
self.sub = self.create_subscription(msg.Image, '/image', self.msg_callback, qos_profile=profile)
def print_header_info(self, m : std_msg.Header):
print("received image stamp {}".format(m.frame_id))
def msg_callback(self, m : msg.Image):
self.print_header_info(m.header)
np_img = np.reshape(m.data, (m.height, m.width, 3)).astype(np.uint8)
self.display(np_img)
def display(self, img : np.ndarray):
cv2.imshow(self.__window_name, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
cv2.waitKey(1)
def main():
ros_core = ros.IProc_Ros2Core()
node = TestDisplayNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == "__main__":
main()
Output:
received image stamp 118
received image stamp 126
received image stamp 134
received image stamp 141
received image stamp 148
received image stamp 155
received image stamp 162
received image stamp 169
Note:
Frame skip is a bit less then for vrep, although it's too much to be good enough to work with vision based applications.
I do realize that I have output in the same thread, which increases latency, but I tried it with different client code, where processing was moved out to separate thread at first place, with exactly the same result.
Looks like I've found an issue. I still need to run a couple of tests, though, just in order to be sure that nothing else causes delay.
_image.py, which seems to be generated file, contains quite complex assert. For my distro it starts on line 199, and lines which are actually responsible for lag are 205, 206:
assert \
((isinstance(value, Sequence) or
isinstance(value, Set) or
isinstance(value, UserList)) and
not isinstance(value, str) and
not isinstance(value, UserString) and
all(isinstance(v, int) for v in value) and ######## bottleneck
all(val >= 0 and val < 256 for val in value)), \ ########
"The 'data' field must be a set or sequence and each value of type 'int' and each unsigned integer in [0, 255]"
Since it needs to be fixed in generator config, I cannot provide actual patch yet.
UPDATE:
Well, I was looking into that code quite late at night) It's the code for debugging version. Since I used default build command, I assume it's FAD behavior.
We have tried multiple things, QoS, change the cache size, etc. the thing we are doing is publishing a 600*600 image from ros2 in windows, and bridge it to ros1 in ubuntu.
# rospy for the subscriberimportrospy# ROS Image messagefromsensor_msgs.msgimportImage# ROS Image message -> OpenCV2 image converterfromcv_bridgeimportCvBridge, CvBridgeError# OpenCV2 for saving an imageimportcv2# Instantiate CvBridgebridge=CvBridge()
i=0defimage_callback(msg):
print("Received an image: {0}".format(i))
# image_np = np.array(msg.data, np.uint8)# #image_np = cv2.imdecode(image_np, cv2.IMREAD_COLOR)# image_np = image_np.reshape((msg.height, msg.width, 3)) #this might be wrong order# cv2.imshow('image', image_np)# cv2.waitKey(1)try:
# Convert your ROS Image message to OpenCV2cv2_img=bridge.imgmsg_to_cv2(msg, "bgr8")
exceptCvBridgeError, e:
print(e)
else:
#Save your OpenCV2 image as a jpegcv2.imwrite('camera_image.jpeg', cv2_img)
cv2.imshow('image', cv2_img)
cv2.waitKey(1)
defmain():
rospy.init_node('image_listener')
# Define your image topicimage_topic="/vehicle_leftImage"# Set up your subscriber and define its callbackrospy.Subscriber(image_topic, Image, image_callback)
# Spin until ctrl + crospy.spin()
if__name__=='__main__':
main()
Try publish a higher resolution, say 600*600 picture, to see that drop on fps.
@rancheng, I won't be able to test your example untill monday. Just as an idea, did you try to run python interpreter with -O option? I did not notice that the code I mentioned was under if __debug__: last time. There's way more asserts which slow down execution.
-O option allowed me to run ros2 run image_tools cam2image -x 1920 -y 1080 and receive images with allmost 30 fps (some frame skipping is still there, but it's quite ok: I've got 15 frames skipped for 1000 frame test run). Note, that I've switched off all output and image.reshape code, so only logging was still there, since it that particulat test numpy and cv output has become abottleneck.
did you try to run python interpreter with -O option? I did not notice that the code I mentioned was under if __debug__: last time.
👍 We put all the setter checks in if __debug__ blocks to work around this type of performance issue. Note that this is only available as of ROS Bouncy (and not in Ardent).
btw, if others are bitten by this issue, a temporary workaround is creating a small binding using pybind11 using buffer protocol to access array members as numpy arrays. we do this for the data-intensive python nodes now.
Why is this issue closed? I'm still experiencing this. I thought it might be related to the DDS implementation (based on this issue), so I tested FastRTPS and OpenSplice using cam2image and my own python script which just shows a preview. I used ros2 topic hz /image to measure the rate, for which the results are shown below.
edit: Using -O or -OO on the python side helps a little, but not much.
edit2: These results are for QoS set to reliable. No images come through when using best effort on either rmw.
Activity
dirk-thomas commentedon Jun 20, 2018
Please provide a reproducible example.
dirk-thomas commentedon Jul 5, 2018
Closing due to no response. Please feel free to comment with the requested information and this ticket can be reopened.
Farrel7786729 commentedon Jul 15, 2018
I'm experiencing quite similar issue, although the enviroment is a bit different. My setup is VREP->ROS1->ROS2->rclpy. I receive decent fps with max resolution of 64*64. Anything more than that results in huge frame skips.
It seems that that VREP->ROS1 works ok. So does ROS1->ROS2 bridge.
With default setup my python code receives only each 10th image (my test resolution is 1024*512). When I set up QoS with KEEP_ALL policy, frameskip disappears, but my test module starts to "leak" memory, due to growing message queue, I assume. Although all frames are received, frame rate remains 1-2 fps. Here's my question on ros forum for reference https://answers.ros.org/question/296715/ros-image-message-rate-is-low/.
Since environment is a bit tricky to reproduce, I'll try to come up with simple MWE.
MWE:
Publisher:
ros2 run image_tools cam2image -x 1024 -y 512Subscriber:
Output:
Note:
Farrel7786729 commentedon Jul 25, 2018
Looks like I've found an issue. I still need to run a couple of tests, though, just in order to be sure that nothing else causes delay.
_image.py, which seems to be generated file, contains quite complex assert. For my distro it starts on line 199, and lines which are actually responsible for lag are 205, 206:
Since it needs to be fixed in generator config, I cannot provide actual patch yet.
UPDATE:
Well, I was looking into that code quite late at night) It's the code for debugging version. Since I used default build command, I assume it's FAD behavior.
rancheng commentedon Jul 26, 2018
We have tried multiple things, QoS, change the cache size, etc. the thing we are doing is publishing a 600*600 image from ros2 in windows, and bridge it to ros1 in ubuntu.
Try publish a higher resolution, say 600*600 picture, to see that drop on fps.
Farrel7786729 commentedon Jul 26, 2018
@rancheng, I won't be able to test your example untill monday. Just as an idea, did you try to run python interpreter with -O option? I did not notice that the code I mentioned was under
if __debug__:last time. There's way more asserts which slow down execution.-O option allowed me to run
ros2 run image_tools cam2image -x 1920 -y 1080and receive images with allmost 30 fps (some frame skipping is still there, but it's quite ok: I've got 15 frames skipped for 1000 frame test run). Note, that I've switched off all output and image.reshape code, so only logging was still there, since it that particulat test numpy and cv output has become abottleneck.mikaelarguedas commentedon Jul 30, 2018
👍 We put all the setter checks in
if __debug__blocks to work around this type of performance issue. Note that this is only available as of ROS Bouncy (and not in Ardent).kervel commentedon Nov 30, 2018
btw, if others are bitten by this issue, a temporary workaround is creating a small binding using pybind11 using buffer protocol to access array members as numpy arrays. we do this for the data-intensive python nodes now.
https://gist.github.com/kervel/75d81b8c34e45a19706c661b90d02548
JacobJBeck commentedon Feb 26, 2019
Why is this issue closed? I'm still experiencing this. I thought it might be related to the DDS implementation (based on this issue), so I tested FastRTPS and OpenSplice using
cam2imageand my own python script which just shows a preview. I usedros2 topic hz /imageto measure the rate, for which the results are shown below.Versions used:
rclpy: 9efd331
rmw_fastrtps: ea4fb7cfaa671a2118a7d54203c786d890d8ed38
rmw_opensplice: 25287a690e8e91e6f56da51944829957c629708f
edit: Using
-Oor-OOon the python side helps a little, but not much.edit2: These results are for QoS set to
reliable. No images come through when usingbest efforton either rmw.dirk-thomas commentedon Mar 27, 2019
The performance bottle neck to convert the incoming message into a Python object has been resolved in ros2/rosidl_python#35.