ROS1下不能使用Python3.4使用cv_bridge

因为毕业设计要求在ROS1下运行Python3环境下的MaskRCNN,虽然在Anaconda下跑通了DEMO,但是我发现在PYTHON3.4环境下rosrun我读取摄像头的python程序就会报错

Traceback (most recent call last):
  File "/home/asber/catkin_ws/src/my_image_transport/scripts/my_publisher.py", line 16, in 
    from cv_bridge import CvBridge, CvBridgeError
ImportError: No module named 'cv_bridge'

我们按照这里说的小心谨慎的实验一下:

cd /opt/ros/kinetic/lib/python2.7/dist-packages
sudo mv cv2.so cv2_ros.so

移除之前的boost
之前我的usr/local里面有1.66 py2版本的boost
root@asber-X550VX:/usr/local/include# mv boost boost.bak2
root@asber-X550VX:/usr/local/lib# mkdir libboost2
root@asber-X550VX:/usr/local/lib# mv libboost* libboost2
构建针对 python3 的 boost 库
参考:

  1. boost编译libboost_python3
  2. 基于python3.6的libboost库安装(用于anaconda3安装caffe)

./bootstrap.sh --with-python=/home/asber/Document/anaconda2/envs/maskrcnn/bin/python3 --with-python-root=/home/asber/Document/anaconda2/envs/maskrcnn/lib/python3.4 --with-python-version=3.4 --with-libraries=python --with-toolset=gcc

./b2 --with-python include="/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m/" (没有cflags=’-fPIC’ cxxflags=’-fPIC’ 因为报错没有这个fpic文件夹)

sudo ./b2 install
cd /usr/local/lib

发现生成了libboost_python3.so 和 libboost_python3.a

sudo vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
source ~/.bashrc

如果装错了,usr那边处理之后,在boost的编译目录下./b2 clean


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

创建新的工作区(注意,工作区之间切忌不能有同名的pkg,参考here)

mkdir -p ~/test_ws/src
cd ~/test_ws/src
catkin_init_workspace
cd ~/test_ws/
catkin_make
gedit ~/.bashrc
加入source ~/test_ws/devel/setup.bash

根据此处,我们可以选择系统默认的python版本(通过制定bin路径下的python执行文件)

进入root然后# update-alternatives --list python
update-alternatives: 错误: 无 python 的候选项
#update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
update-alternatives: 使用 /usr/bin/python3.5 来在自动模式中提供 /usr/bin/python (python)
# python --version
Python 3.5.2
# update-alternatives --install /usr/bin/python python /home/asber/Document/anaconda2/envs/maskrcnn/bin/python3.4 3
update-alternatives: 使用 /home/asber/Document/anaconda2/envs/maskrcnn/bin/python3.4 来在自动模式中提供 /usr/bin/python (python)
# python --version
Python 3.4.5 :: Continuum Analytics, Inc.
# update-alternatives --list python
/home/asber/Document/anaconda2/envs/maskrcnn/bin/python3.4
/usr/bin/python2.7
/usr/bin/python3.5
# update-alternatives --config python
有 3 个候选项可用于替换 python (提供 /usr/bin/python)。

  选择       路径                                                      优先级  状态
------------------------------------------------------------
* 0            /home/asber/Document/anaconda2/envs/maskrcnn/bin/python3.4   3         自动模式
  1            /home/asber/Document/anaconda2/envs/maskrcnn/bin/python3.4   3         手动模式
  2            /usr/bin/python2.7                                           1         手动模式
  3            /usr/bin/python3.5                                           2         手动模式

要维持当前值[*]请按<回车键>,或者键入选择的编号:1
(maskrcnn) asber@asber-X550VX:~/test_ws/src/vision_opencv$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:47) 
(base) asber@asber-X550VX:~/test_ws/src/vision_opencv$ python
Python 2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 21:51:30) 
asber@asber-X550VX:~/test_ws/src/vision_opencv$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:47) 

现在我们sudo update-alternatives --config python选择我们的python3路径
非常重要 建立软连接(这里我不确定,可能最后这样会让系统的python3可以使用cv_bridge)
(maskrcnn) asber@asber-X550VX:/usr/lib/x86_64-linux-gnu$ ls|grep libboost_python
libboost_python.a
libboost_python-py27.a
libboost_python-py27.so
libboost_python-py27.so.1.58.0
libboost_python-py35.a
libboost_python-py35.so
libboost_python-py35.so.1.58.0
libboost_python.so

sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
cd ~/test_ws
catkin_make
(maskrcnn) asber@asber-X550VX:~/test_ws$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:47:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

from cv_bridge.boost.cv_bridge_boost import getCvType
Traceback (most recent call last):
File “”, line 1, in
ImportError: dynamic module does not define init function (PyInit_cv_bridge_boost)

import cv_bridge
exit()
现在不论哪个版本from cv_bridge import CvBridge, CvBridgeError都可以了。。。
但是就是rosrun的时候不行

(maskrcnn) asber@asber-X550VX:~/catkin_ws/src$ rosrun my_image_transport my_publisher.py 
Traceback (most recent call last):
  File "/home/asber/catkin_ws/src/my_image_transport/scripts/my_publisher.py", line 16, in 
    from cv_bridge import CvBridge, CvBridgeError
ImportError: No module named 'cv_bridge'

然后我发现可能是因为我的那句
sys.path.remove(’/opt/ros/kinetic/lib/python2.7/dist-packages’)
后面加
from cv_bridge import CvBridge, CvBridgeError
所以报错了
但是在sys.path.remove(’/opt/ros/kinetic/lib/python2.7/dist-packages’)前面加上import cv_bridge之后虽然通过了,但是如下报错

Traceback (most recent call last):
  File "/home/asber/catkin_ws/src/my_image_transport/scripts/my_publisher.py", line 69, in 
    webcamImagePub()
  File "/home/asber/catkin_ws/src/my_image_transport/scripts/my_publisher.py", line 61, in webcamImagePub
    msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 259, in cv2_to_imgmsg
    if self.cvtype_to_name[self.encoding_to_cvtype2(encoding)] != cv_type:
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2
    from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define init function (PyInit_cv_bridge_boost)

https://blog.csdn.net/xiangxianghehe/article/details/79430072:按照这里的方法不行,修改bashrc下的PYTHONPATH ROSCORE的如果出现其他的路径就会报错。

所以现在还没一个标准的解决方案,彻底放弃python3的MASKRCNN吧。换一个C++或者python2下运行的实例分割


and 我们完成python3的cv_bridge之后应该像here这里说的一样,添加一下路径使得python可以找到cv_bridge

你可能感兴趣的:(Robot,CV)