ros-perception / vision_opencv

Apache License 2.0
546 stars 599 forks source link

cv_bridge crash with ROS2 Python APIs #251

Open alsora opened 5 years ago

alsora commented 5 years ago

Hi,

I'm using cv_bridge in some ROS2 Bouncy projects.

If I use the C++ APIs, everything works fine. On the other hand with the Python ones my program crashes with no error message.

import numpy as np
import cv2
import rclpy
from cv_bridge import CvBridge

def main(args=None):

    #rclpy.init(args=args)

    br = CvBridge()

    dtype, n_channels = br.encoding_to_dtype_with_channels('8UC3')

    im = np.ndarray(shape=(480, 640, n_channels), dtype=dtype)

    msg = br.cv2_to_imgmsg(im)
    im2 = br.imgmsg_to_cv2(msg)

if __name__ == '__main__':
    main()

If I try to run the following code, it crashes during br.encoding_to_dtype_with_channels('8UC3').

Moreover note that I'm not able to call rclpy.init(args=args) if I'm importing CvBridge.

I tried to run the unit tests contained in this repo using colcon test and they fail too.

The system is generated through this Dockerfile

As a recap, I am using

gaoethan commented 5 years ago

It works to call rclpy.init() after importing CvBridge before, maybe you can refer to an example https://github.com/ros-perception/vision_opencv/blob/ros2/opencv_tests/nodes/source.py. thanks !

alsora commented 5 years ago

Hi @gaoethan, I tried to run the example and I get a segmentation fault error during rclpy.init().

I noticed that if I put from cv_bridge import CvBridge after rclpy.init(), the error disappears and I'm able to run the example.

I updated my snippet

import numpy as np
import cv2
import rclpy

def main(args=None):

    rclpy.init(args=args)

    from cv_bridge import CvBridge
    br = CvBridge()

    dtype, n_channels = br.encoding_to_dtype_with_channels('8UC3')

    im = np.ndarray(shape=(480, 640, n_channels), dtype=dtype)

    msg = br.cv2_to_imgmsg(im)
    im2 = br.imgmsg_to_cv2(msg)

if __name__ == '__main__':
    main()

Now I can use rclpy.init(args=args), but i still get a segmentation fault during dtype, n_channels = br.encoding_to_dtype_with_channels('8UC3')

Do you have any idea why?

EDIT: It looks like it's a Boost problem. The segmentation fault happens during the call to the functions binded from the C++ code. Which version of Boost are you using?

gaoethan commented 5 years ago

It's a little strange, because it works before and not sure whether it relates to the other latest changes, it needs further investigation, on the other hand, actually the ROS2 version has almost removed the using of Boost and used C++ 11 or later instead.

alsora commented 5 years ago

I tried to build and run the tests checking out old commits. Tags 2.0.5 and 2.0.4 are not working either.

I also tried updating boost to 1.6.3 but the result does not change

8489faustn commented 5 years ago

Were you ever able to figure out the fix? Currently having the same exact issue when trying to run camera calibration for a kinect camera. Keeps giving me the segmentation error when trying to run self.br.encoding_to_dtype_with_channels(msg.encoding)[0]

ixtiyoruz commented 4 years ago

I also have the same issue. Is this bug resolved?