我正在编写代码以将视频中的帧发布到 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'