ROS1不能使用Anaconda环境的python3订阅图像信息显示

7 篇文章 0 订阅

首先说明,ROS使用的是系统默认的python,而且现在不兼容python3,下面都是个人的实践,证明了就不是可以。
当我们安装了Anaconda之后

(base) asber@asber-X550VX:~/catkin_ws/src$ rospack depends1 beginner_tutorials 
ImportError: No module named rosdep2.rospack
ImportError: No module named rosdep2.rospack
[rospack] Error: could not find python module 'rosdep2.rospack'. is rosdep up-to-date (at least 0.10.4)?
(base) asber@asber-X550VX:~/catkin_ws/src$ conda deactivate
asber@asber-X550VX:~/catkin_ws/src$ rospack depends1 beginner_tutorials 
roscpp
rospy
std_msgs

但是roscore反而都可以运行,看来和python的关系比较小,应该只有在节点涉及到python pkg相关的时候会报错,而之前网上有说,其实在什么环境都可以,只需要把依赖包补全就可以运行。
那么我们分而化之,将分步完成
1.在ROS中实现python脚本对topic发布与订阅
2.在ROS中实现调用Anaconda创建的python环境调用topic收发脚本
3.实现ROS接受摄像头topic并且显示。


1.在ROS中实现python脚本对topic发布与订阅

在系统python环境下
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
然后按照此处进行编写catkin_make之后报错,这个不论shell在系统python还是conda(不论有没有pip install -U rosdep rosinstall_generator wstool rosinstall six vcstools,所有conda环境都一样)环境下都是这样
Traceback (most recent call last):
File “/opt/ros/kinetic/bin/catkin_make”, line 12, in
from catkin.init_workspace import init_workspace
ImportError: No module named catkin.init_workspace
看了一下 “/opt/ros/kinetic/bin/catkin_make”

#!/usr/bin/python
from __future__ import print_function
import argparse
import subprocess

然后我去掉了bashrc里面的#export PYTHONPATH="/home/asber/Document/anaconda2/envs/maskrcnn/lib/python3.4/site-packages:$PYTHONPATH"之后cm成功
catkin_make -DCATKIN_WHITELIST_PACKAGES=“beginner_tutorials”
在base(conda默认环境下)roscore没问题 rosrun talker.py提示 ImportError: No module named rospkg
而在系统默认python环境下

asber@asber-X550VX:~/catkin_ws$ rosrun beginner_tutorials talker.py 
[INFO] [1580650700.904364]: hello world 1580650700.9
[INFO] [1580650701.004527]: hello world 1580650701.0
[INFO] [1580650701.104569]: hello world 1580650701.1

而在之前pip install -U rosdep rosinstall_generator wstool rosinstall six vcstools的conda换进maskrcnn下rosrun beginner_tutorials talker.py

(maskrcnn) asber@asber-X550VX:~/catkin_ws$ rosrun beginner_tutorials talker.py 
[INFO] [1580650741.783082]: hello world 1580650741.7829542
[INFO] [1580650741.883559]: hello world 1580650741.8832698

证明貌似只要pkg足够,在python3的环境下,只是用script进行python程序的运行是可行的。

2.在ROS中实现调用Anaconda创建的python环境调用topic收发脚本

暂时认定可行,只要相关pkg完整

3.实现ROS接受摄像头topic并且显示

参考:https://blog.csdn.net/xiao__run/article/details/88943473
catkin_make -DCATKIN_WHITELIST_PACKAGES=“tag_detector”
就会出现

在函数‘imageCallback(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)’中:
listen_image.cpp:(.text+0xb0):对‘cv::imshow(cv::String const&, cv::_InputArray const&)’未定义的引用
CMakeFiles/tag_detector_listen.dir/src/listen_image.cpp.o:在函数‘main’中:
listen_image.cppCMakeFiles/tag_detector_pub.dir/src/:image_raw.cpp.o:��(��.��数text+0x405):对‘cv::namedWindow(cv::String ‘main’const中:
image_raw.cpp:&(.text,+0x1fa) :�int��)‘’未定义的引用
listen_image.cppcv::imshow(cv:(.text+0x419):对‘cv::startWindowThread()��::String const&, cv::_InputArray const&)’未定义的引用
image_raw.cpp:(.text+0x409):对‘cv::waitKey(int)’未定义的引用
collect2: error: ld returned 1 exit status
�未定义的引用
listen_image.cpp:(.text+0x66b):对‘cv::destroyWindow(cv::String const&)’未定义的引用
collect2: error: ld returned 1 exit status
tag_detector/CMakeFiles/tag_detector_pub.dir/build.make:128: recipe for target '/home/asber/catkin_ws/devel/lib/tag_detector/tag_detector_pub' failed

我估计是Cmakelist那边有问题,没有很好的链接到opencv的库,所以找不到定义。
需要改为

cmake_minimum_required(VERSION 2.8.3)
project(my_image_transport)
find_package(catkin REQUIRED COMPONENTS
  OpenCV
  cv_bridge
  image_transport
)
#catkin_package(
#  LIBRARIES my_image_transport
#  CATKIN_DEPENDS cv_bridge image_transport
#)
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)
include_directories(include ${OpenCV_INCLUDE_DIRS})
#build my_publisher and my_subscriber
add_executable(my_publisher src/my_publisher.cpp)
target_link_libraries(my_publisher ${catkin_LIBRARIES} ${OpenCV_INCLUDE_DIRS})
 
add_executable(my_subscriber src/my_subscriber.cpp)
target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_INCLUDE_DIRS})

如果rosrun找不到,去掉注释然后cm
发送的pkg正确运行,但是
接受之后报错

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window.cpp, line 331
terminate called after throwing an instance of 'cv::Exception'
  what():  /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow
已放弃 (核心已转储)

不知道什么原因,自己的路径也是对的,待解决
参考:ROS中订阅和发布视频中的图像消息

Python实现ROS接受摄像头topic并且显示

Tip:chmod +x xxx.py才可以找到可执行的python脚本
按照此处python2 opencv 实现


暂停一下一切,恢复初始配置

运行发送脚本的时候报错如下

Traceback (most recent call last):
  File "/home/asber/catkin_ws/src/my_image_transport/scripts/my_publisher.py", line 12, in <module>
    import cv2
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type

参考解决方案:使用第二种,在python文件中

import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')

但是还会出现

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

没有cv_brige的情况,这里我们先切换到原本的python环境(系统自带)
参考:编译python3版本的cv_bridge
相同csdn:https://blog.csdn.net/qq_33591712/article/details/84788231

步骤:
0.sudo apt install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-kinetic-cv-bridge
1.cd ~/catkin_ws/
2.sudo catkin config -DPYTHON_EXECUTABLE=/home/asber/Document/anaconda2/envs/maskrcnn/bin/python3 -DPYTHON_INCLUDE_DIR=/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m -DPYTHON_LIBRARY=/home/asber/Document/anaconda2/envs/maskrcnn/lib/libpython3.4m.so
配置工作空间,后面的-DPYTHON_INCLUDE_DIR 什么的是CMake的选项

-------------------------------------------------------------------------------------------------
Profile:                     default
Extending:                   None
Workspace:                   /home/asber/catkin_ws
-------------------------------------------------------------------------------------------------
Build Space:       [missing] /home/asber/catkin_ws/build
Devel Space:       [missing] /home/asber/catkin_ws/devel
Install Space:     [missing] /home/asber/catkin_ws/install
Log Space:         [missing] /home/asber/catkin_ws/logs
Source Space:       [exists] /home/asber/catkin_ws/src
DESTDIR:            [unused] None
-------------------------------------------------------------------------------------------------
Devel Space Layout:          linked
Install Space Layout:        merged
-------------------------------------------------------------------------------------------------
Additional CMake Args:       -DPYTHON_EXECUTABLE=/home/asber/Document/anaconda2/envs/maskrcnn/bin/python3 -DPYTHON_INCLUDE_DIR=/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m -DPYTHON_LIBRARY=/home/asber/Document/anaconda2/envs/maskrcnn/lib/libpython3.4m.so
Additional Make Args:        None
Additional catkin Make Args: None
Internal Make Job Server:    True
Cache Job Environments:      False
-------------------------------------------------------------------------------------------------
Whitelisted Packages:        None
Blacklisted Packages:        None
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
WARNING: Your workspace is not extending any other result space, but it is set to use a `linked`
devel space layout. This requires the `catkin` CMake package in your source space in order to be
built.
-----------------------------------------------------------------------------------------------

3.sudo catkin config --install 将安装切换到指定的安装空间,也就是设置了Install Space: [missing] /home/asber/catkin_ws/install
对catkin的参数了解请看这里
4.git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
5.cd src/vision_opencv/
6.apt-cache show ros-kinetic-cv-bridge | grep Version
7.git checkout 1.12.8
8.cd …/…/ && sudo catkin build cv_bridge

在这里出现一些我觉得有必要了解为什么的tips
WARNING: Your workspace is not extending any other result space, but it is set to use a `linked`
devel space layout. This requires the `catkin` CMake package in your source space in order to be
built.
[build] Found '88' packages in 0.0 seconds.                                                     
Starting  >>> catkin_tools_prebuild                                                             
________________________________________________________________________________________________
Errors     << catkin_tools_prebuild:cmake /home/asber/catkin_ws/logs/catkin_tools_prebuild/build.cmake.000.log
CMake Error at /home/asber/catkin_ws/build/catkin_tools_prebuild/CMakeLists.txt:12 (message):
  The catkin CMake module was not found, but it is required to build a linked
  workspace.  To resolve this, please do one of the following, and try
  building again.



   1. Source the setup.sh file from an existing catkin workspace:
      source SETUP_FILE



   2. Extend another catkin workspace's result (install or devel) space:
      catkin config --extend RESULT_SPACE



   3. Set `catkin_DIR` to the directory containing `catkin-config.cmake`:
      catkin config --cmake-args -Dcatkin_DIR=CATKIN_CMAKE_CONFIG_PATH



   4. Add the catkin source package to your workspace's source space:
      cd SOURCE_SPACE && git clone https://github.com/ros/catkin.git


cd /home/asber/catkin_ws/build/catkin_tools_prebuild; catkin build --get-env catkin_tools_prebuild | catkin env -si  /usr/bin/cmake /home/asber/catkin_ws/build/catkin_tools_prebuild --no-warn-unused-cli -DCATKIN_DEVEL_PREFIX=/home/asber/catkin_ws/devel/.private/catkin_tools_prebuild -DCMAKE_INSTALL_PREFIX=/home/asber/catkin_ws/install -DPYTHON_EXECUTABLE=/home/asber/Document/anaconda2/envs/maskrcnn/bin/python3 -DPYTHON_INCLUDE_DIR=/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m -DPYTHON_LIBRARY=/home/asber/Document/anaconda2/envs/maskrcnn/lib/libpython3.4m.so; cd -
................................................................................................
Failed     << catkin_tools_prebuild:cmake          [ Exited with code 1 ]                       
Failed    <<< catkin_tools_prebuild                [ 1.1 seconds ]                              
Abandoned <<< cv_bridge                            [ Unrelated job failed ]                     
[build] Summary: 0 of 2 packages succeeded.                                                     
[build]   Ignored:   87 packages were skipped or are blacklisted.                               
[build]   Warnings:  None.                                                                      
[build]   Abandoned: 1 packages were abandoned.                                                 
[build]   Failed:    1 packages failed.                                                         
[build] Runtime: 1.4 seconds total.         

然后sudo catkin clean -y
然后我sudo catkin config --extend /opt/ros/kinetic后显示

-------------------------------------------------------------------------------------------------
Profile:                     default
Extending:        [explicit] /opt/ros/kinetic
Workspace:                   /home/asber/catkin_ws
-------------------------------------------------------------------------------------------------
Build Space:       [missing] /home/asber/catkin_ws/build
Devel Space:       [missing] /home/asber/catkin_ws/devel
Install Space:     [missing] /home/asber/catkin_ws/install
Log Space:         [missing] /home/asber/catkin_ws/logs
Source Space:       [exists] /home/asber/catkin_ws/src
DESTDIR:            [unused] None
-------------------------------------------------------------------------------------------------
Devel Space Layout:          linked
Install Space Layout:        merged
-------------------------------------------------------------------------------------------------
Additional CMake Args:       -DPYTHON_EXECUTABLE=/home/asber/Document/anaconda2/envs/maskrcnn/bin/python3 -DPYTHON_INCLUDE_DIR=/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m -DPYTHON_LIBRARY=/home/asber/Document/anaconda2/envs/maskrcnn/lib/libpython3.4m.so
Additional Make Args:        None
Additional catkin Make Args: None
Internal Make Job Server:    True
Cache Job Environments:      False
-------------------------------------------------------------------------------------------------
Whitelisted Packages:        None
Blacklisted Packages:        None
-------------------------------------------------------------------------------------------------
Workspace configuration appears valid.
-----------------------------------------------------------------------------------------------

从之前的Extending: None
到现在的Extending: [explicit] /opt/ros/kinetic
问题貌似解决了,进行sudo catkin build cv_bridge 之后build cv_brige的时候又报错了

Unable to find the requested Boost libraries.

  Boost version: 1.58.0

  Boost include path: /usr/include

  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.
Call Stack (most recent call first):
  CMakeLists.txt:11 (find_package)
安装python3的boost

根据此处此处以及此处
还有此处
执行

./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 --prefix=/usr/local/boost

然后project-config.jam这个文件python.configured的部分

    using python : 3.4 : "/home/asber/Document/anaconda2/envs/maskrcnn/lib/python3.4" :"/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m" :"/home/asber/Document/anaconda2/envs/maskrcnn/lib";

后执行

  1. ./b2 cflags=’-fPIC’ cxxflags=’-fPIC’ --with-python include="/home/asber/Document/anaconda2/envs/maskrcnn/include/python3.4m/"
The following directory should be added to compiler include paths:

    /home/asber/Documents/boost_1_67_0

The following directory should be added to linker library paths:

    /home/asber/Documents/boost_1_67_0/stage/lib
    去这里可以查看libboost_python27.a  .so什么的
  1. sudo ./b2 install

执行这两个命令正式的进行编译,时间有些长。生成的库文件在./stage/lib/中,名字为libboost_python34.a libboost_python34.so libboost_python34.so.1.72.0
sudo cp libboost_python34* /usr/local/lib
cd /usr/local/lib
sudo ln -s libboost_python34.so libboost_python3.so
sudo ln -s libboost_python34.a libboost_python3.a
sudo vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
source ~/.bashrc

至此应该安装成功

然后到catkin_ws下sudo catkin build cv_bridge

________________________________________________________________________________________________
Errors     << cv_bridge:cmake /home/asber/catkin_ws/logs/cv_bridge/build.cmake.001.log          
CMake Error at /usr/local/lib/cmake/Boost-1.72.0/BoostConfig.cmake:120 (find_package):
  Could not find a package configuration file provided by "boost_python3"
  (requested version 1.72.0) with any of the following names:

    boost_python3Config.cmake
    boost_python3-config.cmake

  Add the installation prefix of "boost_python3" to CMAKE_PREFIX_PATH or set
  "boost_python3_DIR" to a directory containing one of the above files.  If
  "boost_python3" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  /usr/local/lib/cmake/Boost-1.72.0/BoostConfig.cmake:185 (boost_find_component)
  /usr/share/cmake-3.5/Modules/FindBoost.cmake:245 (find_package)
  CMakeLists.txt:11 (find_package)

可能是boost版本太高了,去这里下载

Anaconda与ROS共存问题

对于纯的Python代码同时支持Python3和Python2.7是比较容易的,基本上ROS的代码也都是支持的。问题在于包含了C++或者C的那部分Python代码。Python2.7和Python3的c module代码相差很大。一次只能编译其中的一种版本。而且很多module没有做好Python3的支持。在Python3环境下也无法编译。这就是ROS无法支持Python3的原因。----出处
但是其实是可以运行python3的程序的,网上有很多参考方法

1.切换python版本(base)
临时:输入$ conda deactivate,(base)消失
永久:conda config --set auto_activate_base false
但是这样使用不了conda下的环境maskrcnn,所以不用。
2.在anaconda版本的python中下载库

$ conda install setuptools
$ pip install -U rosdep rosinstall_generator wstool rosinstall six vcstools
实验证明在anaconda环境下pip install -U rosdep rosinstall_generator wstool rosinstall six vcstools之后rospack还是报错如下

ImportError: No module named rosdep2.rospack
[rospack] Error: could not find python module 'rosdep2.rospack'. is rosdep up-to-date (at least 0.10.4)?

按照此处终端输入命令sudo apt-get install python3-rosdep

下列软件包有未满足的依赖关系:
 python3-rosdep : 依赖: python3-catkin-pkg 但是它将不会被安装
                  依赖: python3-rospkg 但是它将不会被安装
                  依赖: python3-rosdep-modules (>= 0.18.0) 但是它将不会被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。

故我认为不能直接暴力的在conda的环境里安装即可,需要通过其他的方式隔绝python2,然后单纯使用python3的环境,不过上述只是rospack不能运行,并不能说明其他的pkg不能运行python3

3.修改系统软链接
我看了一下的/usr/bin下的确有很多python的版本,但是貌似没有anaconda下面的,我看了一下anaconda的python解释器在anaconda2/bin下,其他环境的(比如maskrcnn)在anaconda2/envs/maskrcnn/bin下。
4.使用virtualenv创建一个Python3的环境,在这个环境中编译安装自己需要的软件包
5.直接在要执行的python脚本中添加python解释其调用的注释

参考:
https://blog.csdn.net/lixujie666/article/details/89183941
https://blog.csdn.net/weixin_42828571/article/details/102844532
https://blog.csdn.net/weixin_30342827/article/details/96302774
https://blog.csdn.net/hjw756517/article/details/81563614?utm_source=blogxgwz8
https://blog.csdn.net/LutherK

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值