亲测可行,如有疑惑,直接先跳到最后查看效果图~所使用的python为3.6.9。
环境
-
Jetson nano -
Ubuntu18.04 -
python3.6.9 -
ROS Melodic
Jetson系列基础环境配置:Jetson系列——Ubuntu18.04版本基础配置(换源、ROS、远程桌面、开机自连WIFi、SD卡备份) 树莓派环境配置:树莓派4B——Ubuntu 18.04.05安装和基础配置教程(包括WIFI和远程桌面配置、ROS和主从机控制) 虚拟机环境配置:Ubuntu——双系统Ubuntu18.04系统安装和基础配置并安装ROS
1.安装环境
sudo apt-get install python3-catkin-pkg-modules
sudo apt-get install python3-rospkg-modules
pip3 install rosdep rosinstall catkin_pkg
2.基于python3环境编译cv_bridge
mkdir py3_ros_ws && cd py3_ros_ws
mkdir src && cd src
git clone https://github.com/ros-perception/vision_opencv.git
git checkout melodic
source /opt/ros/melodic/setup.bash
catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
报错解决
解决方法:(如果你的是x86,就在/usr/lib/x86-64-linux-gnu 这个文件夹)
cd /usr/lib/aarch64-linux-gnu/
软连接:
sudo ln -s libboost_python-py36.so libboost_python37.so
sudo ln -s libboost_python-py36.a libboost_python37.a
编译成功
3.创建python3图像功能包测试
建立python3功能包
初始化工作空间
mkdir py3_test_ws && cd py3_test_ws
mkdir src && cd src
catkin_init_workspace
创建功能包
catkin_create_pkg py3_demo rospy rosmsg roscpp
编写节点
cd py3_demo && mkdir scripts
cd scripts && touch camera.py
摄像头发布节点
camera.py
import cv2
import numpy as np
import rospy
from std_msgs.msg import Header
from sensor_msgs.msg import Image
from cv_bridge import CvBridge , CvBridgeError
import time
if __name__=="__main__":
import sys
print(sys.version)
capture = cv2.VideoCapture(0)
rospy.init_node('camera_node', anonymous=True)
image_pub=rospy.Publisher('/image_view/image_raw', Image, queue_size = 1)
while not rospy.is_shutdown():
start = time.time()
ret, frame = capture.read()
if ret:
frame = cv2.flip(frame,1)
ros_frame = Image()
header = Header(stamp = rospy.Time.now())
header.frame_id = "Camera"
ros_frame.header=header
ros_frame.width = 640
ros_frame.height = 480
ros_frame.encoding = "bgr8"
ros_frame.data = np.array(frame).tostring()
image_pub.publish(ros_frame)
end = time.time()
print("cost time:", end-start )
rate = rospy.Rate(25)
capture.release()
cv2.destroyAllWindows()
print("quit successfully!")
摄像头接收处理节点
import rospy
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
def callback(data):
global bridge
cv_img = bridge.imgmsg_to_cv2(data, "bgr8")
cv2.imshow("frame" , cv_img)
cv2.waitKey(1)
if __name__ == '__main__':
import sys
print(sys.version)
rospy.init_node('img_process_node', anonymous=True)
bridge = CvBridge()
rospy.Subscriber('/image_view/image_raw', Image, callback)
rospy.spin()
运行效果图
可以看到,打印出的python版本为3.6.9 :
参考文章:
|