Ros melodic python3环境解决cv_bridge问题

Ros melodic python3环境解决cv_bridge问题

巨坑,翻遍所有csdn,找了许多方法,搞了好久终于解决了,现在记录一下。
博主版本:ubuntu18.04+Ros melodic+python3.6.9

问题描述

代码报错最后两行:

 from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)

网上很多教程都说ros+python3不行,是要建立虚拟空间。我尝试过虚拟空间但一样会报错,这里找到了一个方法并不需要建立虚拟空间。

解决方案

一、编译cv_bridge包与使用依赖cv_bridge包:

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

sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge

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

mkdir -p catkin_workspace/src

3、指示carkin设置cmake变量

cd catkin_workspace
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

4、指示catkin将内置包安装到安装位置,这一步不成功也没关系,可不用。

catkin config --install

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

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

6、在存储库中查找cv_bridge的版本

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

7、在git repo中签出正确的版本。在我们的例子中是1.13.0

cd src/vision_opencv/
git checkout 1.13.0
cd ../../

8、开始编译

catkin build 或者 catkin build cv_bridge

耐心等待系统编译完。

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

cd catkin_workspace/
source install/setup.bash --extend

二、包的转移:

我们把src文件夹下除了图像发布和接收共2个文件夹外在加一个上面用到过的文件夹vision_opencv,然后删除掉之前的build,devel文件夹,进行catkin_make(代替了之前的catkin build操作)这样,就在我们的工作空间里,实现了Python3.6+ros+opencv成功完成了图像发布和接收。

1、把catkin_workspace所有的文件build、devel、install、logs
Ros melodic python3环境解决cv_bridge问题_第1张图片全部复制到我运行Ros传输图像的功能包catkin_ws中,将原来的build、devel文件夹全部删掉,然后。同时将catkin_ws中的/src/vision_opencv移到/catkin_ws/src里面。
2、最终catkin_ws结构图
Ros melodic python3环境解决cv_bridge问题_第2张图片Ros melodic python3环境解决cv_bridge问题_第3张图片3、 不用编译catkin_ws功能包,直接运行图像发布和接收代码

三、图像发布和接收代码

发布代码:

#!/usr/bin/env python
# license removed for brevity
import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge
import numpy as np

def talker():
     pub = rospy.Publisher('/img_tr', Image, queue_size=1) 
     rospy.init_node('img_tr', anonymous=True) 
     rate = rospy.Rate(30)
     bridge = CvBridge()
     #Video = cv2.VideoCapture(0)
     Video = cv2.VideoCapture('./vedio/1.avi')
     while not rospy.is_shutdown():
         ret, img = Video.read()
         cv2.imshow("talker", img)
         cv2.waitKey(3)
         #print(img.shape)
         pub.publish(bridge.cv2_to_imgmsg(img, "bgr8"))
         rate.sleep()

if __name__ == '__main__':
     try:
         talker()
     except rospy.ROSInterruptException:
         pass

接收代码:

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge

def callback(imgmsg):
    bridge = CvBridge()
    img = bridge.imgmsg_to_cv2(imgmsg, "bgr8")
    #print('******************')
    #print(img.shape)
    cv2.imshow("listener", img)
    cv2.waitKey(3)

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # node are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("/img_tr", Image, callback)
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()
if __name__ == '__main__':
    listener()

你可能感兴趣的:(ROS,python)