ROS1系列(三)Python3-ROS-cv_bridge

一、源码编译

1、首先进入python3的环境并安装相关依赖包

sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-${ROS_DISTRO}-cv-bridge

2、创建一个工作空间用于存放待编译的 cv_bridge 文件

mkdir -p catkin_workspace/src

3、指示carkin设置cmake变量

cd catkin_workspace
#注意,该处需要根据你原本自带的系统python版本设定。16.04是python3.5,18.04是python3.6
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so

apt-cache show ros-kinetic-cv-bridge | grep Version

sudo apt-get install ros-${ROS_DISTRO}-cv-bridge

4、Instruct catkin to install built packages into install place。这一步不成功也没关系,可不用。

# Instruct catkin to install built packages into install place. It is $CATKIN_WORKSPACE/install folder
catkin config --install

5、在catkin_workspace工作空间中克隆 cv_bridge src

git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv

6、Find version of cv_bridge in your repository

apt-cache show ros-${ROS_DISTRO}-cv-bridge | grep Version

7、Checkout right version in git repo. In our case it is 1.12.8

#该步骤我是省去的
#必须要编译否则cv_bridge的CMakeLists默认版本为python37
cd src/vision_opencv/
git checkout 1.13.0
cd ../../

8、开始编译

#有可能会有warning,不用管,只要不报错就行
catkin build
或者 catkin build cv_bridge

9、进入python3环境(virtualenv)之后,先进入到catkin_workspace工作目录下,运行下面的source,然后再到相关的节点工作空间(如catkin_ws),就可以启动那些使用到cv_bridge库的相关节点了:

# 打开虚拟环境env_py3和进入catkin_workspace空间进行source。
conda activate pytorch
cd catkin_workspace/
# 这里 --extend 参数的作用是让这次的路径配置不影响之前配置好的路径,否则这一次source会覆盖掉之前配置的路径。
source install/setup.bash --extend #每次都必须先激活然后才能使python3调用cv_bridge
cd ..
# 进入另一个工作空间,该空间含有需要启动的python脚本节点。
cd catkin_ws/src/beginner_tutorials/scripts/
python ImgSub.py

二、报错boost_python3:

编译过程中,如果出现下面的报错(目前好像vision_opencv导出来的boost是boost_python37):

  Could not find the following Boost libraries:

          boost_python3

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.
**原始行:**
find_package(Boost REQUIRED python3)

更改成:

find_package(Boost REQUIRED python-py35)

三、报错libgcc_s.so.1

sudo apt install libgcc1-dbg

四、丢包、延迟等问题

主要原因,可能是因为用的是WiFi无线网络,带宽有限,视频流数据庞大,所以经常会丢包。获取不到话题。
所以没办法,我只能订阅压缩过的话题——

#!/usr/bin/python
# encoding:utf-8
# 导入消息类型,压缩的和未压缩的
import time
from sensor_msgs.msg import CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy
class Detect(object):
    def __init__(self):
        self.start_time = time.time()
        # 订阅网络压缩话题,并且,消息类型换成压缩的
        self.image_sub = rospy.Subscriber("/kinect2/hd/image_color_rect/compressed", CompressedImage, self.rgb_callback,
                                          queue_size=1)
        self.depth_sub = rospy.Subscriber("/kinect2/hd/image_depth_rect/compressed", CompressedImage,
                                          self.depth_callback, queue_size=1)
        self.bridge = CvBridge()

    def rgb_callback(self, image):
        try:
            # 尝试转换压缩图片信息到CV2可以直接用的格式。后面加不加bgr8好像都行。
            self.rgb_image = self.bridge.compressed_imgmsg_to_cv2(image, "bgr8")
            # 用上面的和下面的都行,都是会转成rgb的图,但是对于第二个深度图就不合适了,只能用上面的
        #            np_arr = np.fromstring(image.data,np.uint8)
        # opencv3 is this ,and opencv2 is cv2.CV_LOAD_IMAGE_COLOR
        #            self.rgb_image = cv2.imdecode(np_arr,cv2.IMREAD_COLOR)
        except CvBridgeError as e:
            print(e)
            rospy.loginfo('convert rgb image error')

    def depth_callback(self, depth):
        try:
            print("depth_start:")
            self.depth_image = self.bridge.compressed_imgmsg_to_cv2(depth)
            print(self.depth_image.shape)
        except CvBridgeError as e:
            print(e)
            rospy.loginfo('convert depth image image error')

你可能感兴趣的:(笔记,ubuntu,linux,自动驾驶)