0. Preface
现在很多新的图片处理model都是基于python3的,而ROS还是2.7的,要结合起来用不可避免有很多问题,以下以接收ROS image为例子
以下方法需要用的anaconda,安装方法有很多blog分享
1. Preparation
以下是python3接收image topic和发送image topic的一个节点代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from cv_bridge import CvBridge
### ROS
import rospy
from sensor_msgs.msg import Image
img_flg = False
image = None
def imageCallback(msg):
global image
global img_flg
bridge = CvBridge()
image = bridge.imgmsg_to_cv2(msg, desired_encoding="passthrough")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img_flg = True
# print(image.shape)
rospy.init_node('img_node')
rospy.Subscriber('/top_camera', Image, imageCallback)
imgpub = rospy.Publisher('/pro_img', Image, queue_size=10)
rate = rospy.Rate(10) # 10hz
def inter():
global img_flg
while not rospy.is_shutdown():
if img_flg: # only consider human label
### IMAGE PROCESSING ###
#### ... ####
########################
bridge = CvBridge()
masked_image = masked_image.astype(np.uint8)
processed_image_msg = bridge.cv2_to_imgmsg(masked_image, encoding="passthrough")
imgpub.publish(processed_image_msg)
print("Saved!\n")
bbox_flg = False
img_flg = False
else:
print("Waiting...\n")
rate.sleep()
rospy.spin()
if __name__ == '__main__':
try:
inter()
except rospy.ROSInterruptException:
pass
但是现在其实运行不起来的,会直接报错,如下:
[ERROR] [1727968719.817374]: bad callback: <function imageCallback at 0x7fee6d8c8b80>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/will/project/SAM/segment-anything/sam_withros.py", line 45, in imageCallback
image = bridge.imgmsg_to_cv2(msg, "bgr8")
File "/opt/ros/melodic/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/melodic/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/melodic/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)
2. Solution
1)先建一个python3.9的环境
2)安装一些依赖项
pip install rospkg
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge
注意上述的ROS版本,这里是melodic,根据实际情况调整
3)建一个新的workspace,编译cv_bridge的包
mkdir -p ~/python3_ws/src
cd ~/python3_ws/src
catkin_init_workspace
cd ..
catkin_make
catkin_make install
4) 从Github下载cv_bridge的包
git clone -b melodic https://github.com/ros-perception/vision_opencv.git
5)然后进行编译以及指定python版本
catkin_make -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
然后source一下
source ~/python3_ws/devel/setup.bash
6)编译成功后,测试一下,进入python,然后运行from cv_bridge.boost.cv_bridge_boost import getCvType
如果没有报错就成功了
(det) will@will-Dell-G15-5511:~/project$ python
Python 3.9.12 (main, Jun 1 2022, 11:38:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from cv_bridge.boost.cv_bridge_boost import getCvType
>>>
7)最后,记得先source python3_ws,再进入虚拟环境,配置一下缺的包,然后跑一下上面一开始的代码,就会发现,这时就成功了。