当前位置:首页 > 编程笔记 > 正文
已解决

ros 接收相机数据数据并发布

来自网友在路上 158858提问 提问时间:2023-09-27 06:53:18阅读次数: 58

最佳答案 问答题库588位专家为你答疑解惑

完整这个任务不需要用到python3

效果如下所示

环境 

ROS1  python2.7

  1. 原始环境 无conda  或者conda deactivate 无conda 状态

  2. pip install rospkg

  3. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python==4.2.0.3

  4. 代码 放在工程目录中

#!/usr/bin/env python
#!coding=utf-8#write by leo at 2018.04.26
#function: 
#1, Get live_video from the webcam.
#2, With ROS publish Image_info (to yolo and so on).
#3, Convert directly, don't need to save pic at local.import rospy
from sensor_msgs.msg import Image
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
import sysdef webcamImagePub():# init ros_noderospy.init_node('webcam_puber', anonymous=True)# queue_size should be small in order to make it 'real_time'# or the node will pub the past_frameimg_pub = rospy.Publisher('webcam/image_raw', Image, queue_size=2)rate = rospy.Rate(25) # 5hz# make a video_object and init the video objectcap = cv2.VideoCapture(0)# define picture to_down' coefficient of ratioscaling_factor = 0.5# the 'CVBridge' is a python_class, must have a instance.# That means "cv2_to_imgmsg() must be called with CvBridge instance"bridge = CvBridge()if not cap.isOpened():sys.stdout.write("Webcam is not available !")return -1count = 0# loop until press 'esc' or 'q'while not rospy.is_shutdown():ret, frame = cap.read()# resize the frameif ret:count = count + 1else:rospy.loginfo("Capturing image failed.")if count == 2:count = 0frame = cv2.resize(frame,None,fx=scaling_factor,fy=scaling_factor,interpolation=cv2.INTER_AREA)msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")img_pub.publish(msg)print '** publishing webcam_frame ***'	rate.sleep()if __name__ == '__main__':try:webcamImagePub()except rospy.ROSInterruptException:pass

运行代码 

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"ros 接收相机数据数据并发布":http://eshow365.cn/6-14484-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!