pythonopencvrosrospy

ROS Publisher Node


I am writing a code to publish the frames from a video to a rostopic. While declaring the pub object it is giving me an error.

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')

It is giving me the following error:

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'

Solution

  • Python being case sensitive: publisher --> Publisher and cv2_to_imgmsg instead of imgmsg_to_cv2 solves the problem.