简体   繁体   English

无法将cv_bridge与ROS Kinetic和Python3一起使用

[英]Unable to use cv_bridge with ROS Kinetic and Python3

I had a computer vision project on Ubuntu 14.04 with ROS indigo and python3, then I had to move on Ubuntu 16.04 with ROS kinetic. 我在Ubuntu 14.04上有一个使用ROS indigo和python3的计算机视觉项目,然后我不得不使用ROS动能在Ubuntu 16.04上移动。 Here I ran into multiple issues: 在这里我遇到了多个问题:

1) I installed opencv, but couldn't import it in python3, the error message was: 1)我安装了opencv,但无法在python3中导入它,错误信息为:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import cv2
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: 
undefined symbol: PyCObject_Type

What I figured out is just rename cv2.so 我想到的只是重命名cv2.so

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

Then I was able to import cv2 and use it 然后我能够导入cv2并使用它

2) Then I couldn't import rospy, but installation of python3-catkin-pkg-modules and python3-rospkg-modules fixed that problem 2)然后我无法导入rospy,但安装python3-catkin-pkg-modules和python3-rospkg-modules解决了这个问题

3) Finally I ran into an issue with cv_bridge it says: 3)最后我遇到了cv_bridge的问题,它说:

[ERROR] [1520780674.845066]: bad callback: <bound method ViewsBuffer.update of <__main__.ViewsBuffer object at 0x7f5f45a07f28>>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "test.py", line 48, in update
    im = self.bridge.imgmsg_to_cv2(im, "bgr8")
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 163, in imgmsg_to_cv2
    dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/cv_bridge/core.py", line 99, in encoding_to_dtype_with_channels
    return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding))
  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 module export function (PyInit_cv_bridge_boost)

I believe the problem is in the cv_bridge_boost.so file. 我相信问题出在cv_bridge_boost.so文件中。 I also tried to build cv_bridge from https://github.com/ros-perception/vision_opencv , but it automatically builds for python2.7, I was trying to modify CMakeLists.txt a little bit to specify python3 there, but I don't have much experience with CMakeLists, so it didn't work. 我也尝试从https://github.com/ros-perception/vision_opencv构建cv_bridge,但它会自动为python2.7构建,我试图稍微修改一下CMakeLists.txt以指定python3,但我不知道对CMakeLists有很多经验,所以它没有用。 I also tried to copy cv_bridge module to my project folder, but it didn't change much and it still points on that cv_bridge_boost.so file. 我还尝试将cv_bridge模块复制到我的项目文件夹,但它没有太大变化,它仍然指向cv_bridge_boost.so文件。 Another thing to mention is that cv_bridge works fine with python2.7, but my project requires python3.5. 另外要提到的是cv_bridge与python2.7一起工作正常,但我的项目需要python3.5。

You are right, you should build cv_bridge with python3. 你是对的,你应该用python3构建cv_bridge。

You can do it with passing 你可以通过传球

-DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so

args to cmake. args to cmake。 Or, if you are using catkin to build packages, you can do next steps: 或者,如果您使用catkin构建包,则可以执行以下步骤:

# `python-catkin-tools` is needed for catkin tool
# `python3-dev` and `python3-catkin-pkg-modules` is needed to build cv_bridge
# `python3-numpy` and `python3-yaml` is cv_bridge dependencies
# `ros-kinetic-cv-bridge` is needed to install a lot of cv_bridge deps. Probaply you already have it installed.
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-kinetic-cv-bridge
# Create catkin workspace
mkdir catkin_workspace
cd catkin_workspace
catkin init
# Instruct catkin to set cmake variables
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
# Instruct catkin to install built packages into install place. It is $CATKIN_WORKSPACE/install folder
catkin config --install
# Clone cv_bridge src
git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
# Find version of cv_bridge in your repository
apt-cache show ros-kinetic-cv-bridge | grep Version
    Version: 1.12.8-0xenial-20180416-143935-0800
# Checkout right version in git repo. In our case it is 1.12.8
cd src/vision_opencv/
git checkout 1.12.8
cd ../../
# Build
catkin build cv_bridge
# Extend environment with new package
source install/setup.bash --extend

And

$ python3

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from cv_bridge.boost.cv_bridge_boost import getCvType
>>> 

If you encountered an next error 如果您遇到下一个错误

CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1677 (message):
  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)

It is because CMake tries to find libboost_python3.so library, but in ubuntu it is libboost_python-py35.so ( /usr/lib/x86_64-linux-gnu/libboost_python-py35.so ), so you should change line 这是因为CMake试图找到libboost_python3.so库,但是在ubuntu中它是libboost_python-py35.so/usr/lib/x86_64-linux-gnu/libboost_python-py35.so ),所以你应该libboost_python-py35.so

find_package(Boost REQUIRED python3)

to

find_package(Boost REQUIRED python-py35)

in file src/vision_opencv/cv_bridge/CMakeLists.txt and rebuild package. 在文件src/vision_opencv/cv_bridge/CMakeLists.txt和rebuild包中。

I have anaconda3 installed in my system and I face a similar issue while using cv_bridge. 我的系统中安装了anaconda3,在使用cv_bridge时遇到了类似的问题。 When I try to build using catkin build cv_bridge it throws up an error. 当我尝试使用catkin build cv_bridge进行catkin build cv_bridge它会引发错误。 I use python3.7 from conda. 我从conda使用python3.7。 My catkin cofig command is: 我的catkin cofig命令是:

catkin config -DPYTHON_EXECUTABLE=/home/akashbaskaran/anaconda3/bin/python3 -DPYTHON_INCLUDE_DIR=/home/akashbaskaran/anaconda3/include/python3.7m -DPYTHON_LIBRARY=/home/akashbaskaran/anaconda3/lib/libpython3.7m.so

Solution: There are a couple of things which I did and the issue got resolved. 解决方案:我做了几件事,问题得到了解决。

  • since anaconda is being used, the executables and include directorires should be the one inside your current virtual enviroment. 自从anaconda被使用以来,可执行文件和包括导演应该是你当前虚拟环境中的那个。 -DPYTHON_EXECUTABLE=/home/akashbaskaran/anaconda3/envs/tf/bin/python3.6 -DPYTHON_INCLUDE_DIR=/home/akashbaskaran/anaconda3/envs/tf/include/python3.6m -DPYTHON_LIBRARY=/home/akashbaskaran/anaconda3/envs/tf/lib/libpython3.6m.so e -DPYTHON_EXECUTABLE=/home/akashbaskaran/anaconda3/envs/tf/bin/python3.6 -DPYTHON_INCLUDE_DIR=/home/akashbaskaran/anaconda3/envs/tf/include/python3.6m -DPYTHON_LIBRARY=/home/akashbaskaran/anaconda3/envs/tf/lib/libpython3.6m.so e
  • I was having build issues when I tried catkin build cv_bridge. 当我尝试使用catkin build cv_bridge时,我遇到了构建问题。 If you face a similar issue, delete all folders except src. 如果您遇到类似问题,请删除除src之外的所有文件夹。 Then run catkin_make from the terminal (make sure you are present inside catkin_workspace). 然后从终端运行catkin_make(确保你出现在catkin_workspace中)。
  • source the current workspace source devel/setup.bash 获取当前工作空间source devel/setup.bash

Now import cv2 from cv_bridge.boost.cv_bridge_boost import getCvType 现在import cv2 from cv_bridge.boost.cv_bridge_boost import getCvType

should work without an error. 应该没有错误地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM