在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()
posted @ 2023-05-08 11:06  bitterteaer  阅读(151)  评论(0编辑  收藏  举报