ROS 发布者节点

ROS Publisher Node

我正在编写代码以将帧从视频发布到 rostopic。声明 pub 对象时出现错误。

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

class Video_Publisher:
    def __init__(self):
        self.pub = rospy.publisher('tennis_ball_image', Image , queue_size = 1)
        self.bridge= CvBridge()
    def framepublish(self):
        video = cv2.VideoCapture('video/tennis-ball-video.mp4')
        while not rospy.is_shutdown():
            ret, frame = video.read()
            #cv2.imshow("TennisVideo",frame)
            try:    
                ros_image = bridge.immsg_to_cv2(frame,"bgr8")
            except CvBridgeError as e:
                 print(e)
                 continue
            self.pub.publish(ros_image)
            self.rate.sleep

if __name__ == '__main__':
    rospy.init_node('tennis_ball_publisher',anonymous=True)
    frames = Video_Publisher()
    frames.framepublish('video/tennis-ball-video.mp4')

它给我以下错误:

Traceback (most recent call last):
  File "tennis_ball_publisher.py", line 33, in <module>
    frames = Video_Publisher()
  File "tennis_ball_publisher.py", line 11, in __init__
    self.pub = rospy.publisher('tennis_ball_image', Image , queue_size = 1)
AttributeError: 'module' object has no attribute 'publisher'

Python 区分大小写:出版商 --> 出版商 和 cv2_to_imgmsg 而不是 imgmsg_to_cv2 解决了问题。