ROS CV_Bridge编译

Cyan Infinite - Compiling ROS cv_bridge with Python 3

Compiling ROS cv_bridge with Python 3

Wanted to compile ROS packages with Python 3 on ROS Melodic, only realizing that catkin_make only compiles Python 2 scripts instead after a while? No worries! In this tutorial, we’ll be looking exactly into that.

Contents

Since ROS does not support Python 3 by default, we would have to compile cv_bridge via another method if want to use OpenCV 4 with Python 3.

Fun fact: ROS2 supports Python 3 by default.

Furthermore, as the support of Python 2.7 ended last year (December 2019), it is recommended to discontinue the use of Python 2.7 as it would not be updated anymore and move on to Python 3.

Therefore, this tutorial would be covering how to compile a Python 3 ROS package with ROS Melodic, specifically cv_bridge. With that, let’s get started!

Dependencies

We’ll first download the python build tools:

$ sudo apt-get install python3-pip python-catkin-tools python3-dev python3-numpy
$ sudo pip3 install rospkg catkin_pkg

Workspace

Next, create a separate workspace to compile the bridge_cv ROS package. We would be naming the directory cvbridge_build_ws.

$ mkdir -p ~/cvbridge_build_ws/src
$ cd ~/cvbridge_build_ws/src

After the workspace has been setup, we’ll clone the open_vision repository into ~/cvbridge_build_ws/src:

$ git clone -b noetic https://github.com/ros-perception/vision_opencv.git

We would be downloading the noetic branch instead of the melodic branch as it supports for both OpenCV 4 and Python 3.If you try compiling immediately, you would get this error:

Therefore, we need to make a slight change to the cv_bridge CMakeLists.txt file before proceeding as Ubuntu 18.04 is not be able to recognise python37 by default.
Open CMakeLists.txt with you favorite text editor and make the following modification at Line 11, changing:

find_package(Boost REQUIRED python37)

to

find_package(Boost REQUIRED python3)

Save and exit the text editor.

Compilation

After the dependencies and workspace have been setup, we would proceed to configure the workspace for compilation:

$ cd ~/cvbridge_build_ws
$ catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so这里未用到
$ catkin config -DPYTHON_EXECUTABLE=/home/kevalen/miniconda3/bin/python3 -DPYTHON_INCLUDE_DIR=/home/kevalen/miniconda3/include/python3.9 -DPYTHON_LIBRARY=/home/kevalen/miniconda3/lib/python3.9/site-packages
$ catkin config --install

I just wanted to add that if you are using Ubuntu the DPYTHON_LIBRARY flag should begin with
/usr/lib/x86_64-linux-gnu/… 

Note: Change the python path accordingly, you could check out the location via Python 3.

$ python3
>>> import sys
>>> print(sys.executable) #Print python3 executable path
>>> print(sys.path) #Print python3 library path
>>> quit()

To find where the include files are:

$ python3-config --includes

After the configuration is completed, build the package:

$ catkin build cv_bridge -DSETUPTOOLS_DEB_LAYOUT=OFF

上面添加-DSETUPTOOLS_DEB_LAYOUT=OFF 的原因是解决error: option --install-layout not recognized 。参见连接Building catkin Workspace fails - install-layout not recognized · Issue #863 · ros/catkin · GitHub

To use the package, you could source it via:

$ source install/setup.bash --extend

And Viola! You have “compiled” the cv_bridge package via Python 3.

You could add it to .bashrc if you use the ROS package frequently.

References

  • https://wiki.ros.org/UsingPython3
  • https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
  • https://answers.ros.org/question/350904/cv_bridge-throws-boost-import-error-in-python-3-and-ros-melodic/
  • https://stackoverflow.com/questions/49221565/unable-to-use-cv-bridge-with-ros-kinetic-and-python3
  • https://stackoverflow.com/questions/35071192/how-to-find-out-where-the-python-include-directory-is
  • https://stackoverflow.com/questions/6767283/find-where-python-is-installed-if-it-isnt-default-dir#6767329

终极解决方案:

"cv_bridge" throws boost import error in Python 3 and ROS Melodic - ROS Answers: Open Source Q&A Forum

YOLOV4_Deepsort_ROS/track_deep.py at main · leicheng5/YOLOV4_Deepsort_ROS · GitHub 

"""
    Provides conversions between OpenCV and ROS image formats in a hard-coded way.  
    CV_Bridge, the module usually responsible for doing this, is not compatible with Python 3,
     - the language this all is written in.  So we create this module, and all is... well, all is not well,
     - but all works.  :-/
"""
import sys
import numpy as np
from sensor_msgs.msg import Image

def imgmsg_to_cv2(img_msg):
    if img_msg.encoding != "bgr8":
        rospy.logerr("This Coral detect node has been hardcoded to the 'bgr8' encoding.  Come change the code if you're actually trying to implement a new camera")
    dtype = np.dtype("uint8") # Hardcode to 8 bits...
    dtype = dtype.newbyteorder('>' if img_msg.is_bigendian else '<')
    image_opencv = np.ndarray(shape=(img_msg.height, img_msg.width, 3), # and three channels of data. Since OpenCV works with bgr natively, we don't need to reorder the channels.
                    dtype=dtype, buffer=img_msg.data)
    # If the byt order is different between the message and the system.
    if img_msg.is_bigendian == (sys.byteorder == 'little'):
        image_opencv = image_opencv.byteswap().newbyteorder()
    return image_opencv

def cv2_to_imgmsg(cv_image):
    img_msg = Image()
    img_msg.height = cv_image.shape[0]
    img_msg.width = cv_image.shape[1]
    img_msg.encoding = "bgr8"
    img_msg.is_bigendian = 0
    img_msg.data = cv_image.tostring()
    img_msg.step = len(img_msg.data) // img_msg.height # That double line is actually integer division, not a comment
    return img_msg

https://github.com/Leonardo-Blanger/detr_tensorflow/issues/3

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CV_BridgeROS中常用的一个包,可以将ROS系统中的图像数据与OpenCV中的图像数据进行转换。在Ubuntu 18.04和ROS Melodic环境下,编译CV_Bridge包时出现错误,可能是由于缺少依赖库或者包的路径设置不正确导致的。 以下是可能的解决方案: 1. 安装依赖库 首先,您需要检查是否已经安装了必要的依赖库。CV_Bridge包需要OpenCV和Boost库。您可以使用以下命令安装它们: ``` sudo apt-get install libopencv-dev sudo apt-get install libboost-all-dev ``` 2. 设置PKG_CONFIG_PATH环境变量 如果您已经安装了OpenCV和Boost库,但是仍然遇到编译错误,可能是因为您的PKG_CONFIG_PATH环境变量没有设置正确。您可以使用以下命令设置它: ``` export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH ``` 3. 更新CATKIN_IGNORE文件 如果以上方法都不能解决问题,您可以尝试更新CV_Bridge包中的CATKIN_IGNORE文件。在CV_Bridge包的根目录中,打开CATKIN_IGNORE文件,并将所有的“opencv2”和“opencv”删除,然后保存文件并重新编译CV_Bridge包。 以上是一些常见的解决方案。如果问题仍然存在,您可以尝试在ROS论坛上寻求帮助,或者参考ROS Wiki上的CV_Bridge页面。 ### 回答2: 在Ubuntu 18.04 melodic环境下,如果编译cv_bridge包时出现错误,可以尝试以下解决方法: 1. 确认已安装必需的依赖项:首先确保已正确安装了OpenCV和cv_bridge的相关依赖项。可以使用以下命令安装: ``` sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml python3-rospkg-modules python3-dev ``` 2. 更新ROS packages:更新ROS packages可解决编译问题,使用以下命令更新: ``` sudo apt-get update sudo apt-get upgrade ``` 3. 检查CMakeLists.txt文件中的设置:查看cv_bridge的CMakeLists.txt文件,确认编译选项和依赖项是否正确设置。特别注意检查OpenCV的版本和路径是否正确。可以尝试重新配置和构建项目: ``` catkin_make clean catkin_make ``` 4. 检查Python版本:确认使用的Python版本是否与cv_bridge的版本兼容。在melodic环境下,建议使用Python3。 5. 安装可能缺失的软件包:如果编译错误报告缺少特定软件包,可以尝试通过以下命令来安装: ``` sudo apt-get install <package-name> ``` 其中,"<package-name>"是缺失的软件包名称。 6. 查阅错误报告和文档:细读错误报告和相关文档,可能会提供更多关于问题的信息和解决方法。 如果仍然无法解决问题,建议查阅ROScv_bridge的官方文档,或在ROS的官方论坛或社区中提问寻求进一步的帮助。 ### 回答3: 在Ubuntu 18.04 Melodic环境下,如果在编译cv_bridge包时遇到报错,可以尝试以下解决方法: 1. 确保已经安装了所有依赖项和必要的软件包。使用以下命令安装所需软件包: ``` sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge ``` 2. 检查工作空间的设置。确保在构建过程中使用正确的CMakeLists.txt文件。可以尝试重新创建工作空间,并在更高级别的目录中运行以下命令: ``` source /opt/ros/melodic/setup.bash mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catkin_make ``` 3. 清除先前的构建和开发文件。在工作空间目录下运行以下命令: ``` catkin clean -b catkin clean --devel ``` 4. 更新ROS软件包。运行以下命令更新ROS Melodic软件包: ``` sudo apt-get update sudo apt-get upgrade ``` 5. 检查cv_bridge包的版本兼容性。确保使用的cv_bridge包版本与ROS Melodic版本兼容。根据需要,可以尝试卸载并重新安装正确的版本。 通过以上方法,您应该能够解决Ubuntu 18.04 Melodic环境下cv_bridge编译报错的问题。如果问题仍然存在,请参阅ROS Melodic和cv_bridge的官方文档和论坛,以获得更多帮助和支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值