Not able to publish image to std_msg/Image
I am trying to run a publisher code that publishes an Image to a topic. But, whenever I run the code, I get an error. The code I have written is below -
#!/usr/bin/env python
import rospy
import numpy as np
import cv2
from sensor_msgs.msg import Image
video_capture = cv2.VideoCapture(0)
def main():
rospy.init_node('Tennis Ball Publisher', anonymous = True)
pub = rospy.Publisher("tennis_ball_image", Image, queue_size = 10)
rate = rospy.Rate(1)
while (True):
ret, frame = video_capture.read()
pub.publish(frame)
rate.sleep()
if __name__=='__main__':
main()
This is the error that I get on running the code -
Traceback (most recent call last):
File "tennis_ball_publisher.py", line 26, in <module>
main()
File "tennis_ball_publisher.py", line 20, in main
pub.publish(frame)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 879, in publish
data = args_kwds_to_message(self.data_class, args, kwds)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/msg.py", line 122, in args_kwds_to_message
return data_class(*args)
File "/opt/ros/melodic/lib/python2.7/dist-packages/sensor_msgs/msg/_Image.py", line 78, in __init__
super(Image, self).__init__(*args, **kwds)
File "/opt/ros/melodic/lib/python2.7/dist-packages/genpy/message.py", line 294, in __init__
raise TypeError("Invalid number of arguments, args should be %s"%str(self.__slots__)+" args are"+str(args))
TypeError: Invalid number of arguments, args should be ['header', 'height', 'width', 'encoding', 'is_bigendian', 'step', 'data'] args are(None,)
I suspect that the error is in the way I am publishing the frame but I am not able to rectify it. Any help is appreciated.