在python中将opencv读取到的视频数据发布为ros话题,并在ros中能够通过rqt显示出来

import cv2
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

# Initialize ROS node
rospy.init_node('video_publisher', anonymous=True)

# Create a publisher for the video stream
video_pub = rospy.Publisher('video_stream', Image, queue_size=10)

# Initialize OpenCV video capture
cap = cv2.VideoCapture('rtsp://admin:qwer1234!.@192.168.1.67:554')

# Initialize CvBridge for image conversion
bridge = CvBridge()

# Loop to read and publish video frames
while not rospy.is_shutdown():
    # Read a frame from the video
    ret, frame = cap.read()

    # Convert the frame to a ROS image message
    try:
        ros_image = bridge.cv2_to_imgmsg(frame, "bgr8")
    except CvBridgeError as e:
        rospy.logerr(e)

    # Publish the ROS image message
    video_pub.publish(ros_image)

# Release the OpenCV video capture and shutdown ROS node
cap.release()
rospy.shutdown()

本文作者:bitterteaer

本文链接:https://www.cnblogs.com/bitterteaer/p/17381146.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   bitterteaer  阅读(166)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起