Ask Your Question

Image publisher gives numpy error

asked 2020-05-22 06:15:11 -0500

DustyNinja gravatar image

Hi, I am running the code from the directory of the file. After initiating both the publisher and subscriber the code works perfectly for 15sec and then give the error shown bellow.


#!/usr/bin/env python 
import cv2
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

def read_video():
  video_publisher = rospy.Publisher("tennis_ball_image", Image, queue_size=100)
  video_capture = cv2.VideoCapture("video/tennis-ball-video.mp4")
  while not rospy.is_shutdown():
    _,rgb_frame =
    ros_image = CvBridge().cv2_to_imgmsg(rgb_frame, "bgr8")

if __name__ == '__main__':

     rospy.init_node("tennis_ball_publisher", anonymous=True)
 except rospy.ROSInterruptException:
     rospy.loginfo("node terminated.")


Traceback (most recent call last):
  File "./", line 21, in <module>
  File "./", line 12, in read_video
    ros_image = CvBridge().cv2_to_imgmsg(rgb_frame, "bgr8")
  File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/", line 246, in cv2_to_imgmsg
    raise TypeError('Your input type is not a numpy array')
TypeError: Your input type is not a numpy array
edit retag flag offensive close merge delete


Do you have an example of a duplicate question? If this isn't actually a duplicate question, then it shouldn't be closed. If the answer I provided was correct, then the answer should be marked as such. If you figured out something else, it would be great if you answered your own question. We hope to get questions with correct answers to help others facing similar issues rather than closing questions. Thanks!

jarvisschultz gravatar image jarvisschultz  ( 2020-05-22 17:10:10 -0500 )edit

It got closed by mistake i am unable to reopen it

DustyNinja gravatar image DustyNinja  ( 2020-05-22 17:14:31 -0500 )edit

Got it... just re-opened. Thanks for getting back to me

jarvisschultz gravatar image jarvisschultz  ( 2020-05-26 12:03:31 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-05-22 10:43:32 -0500

Likely the issue is that your rgb_frame is None because the call didn't properly return an image. You should be checking the return value of that call instead of just dumping it as _. Is it possible that after ~15 seconds you've just reached the end of the input video? Also, note that it's probably good practice to be wrapping the cv2_to_imgmsg with a try-except anyway (as in the tutorials, although perhaps you'd also want to catch a TypeError).

PS. This really isn't a ROS problem, it's an OpenCV problem. I realize the traceback points to something from cv_bridge, but it's not the problem here. The problem is the data you are feeding into cv_bridge. Perhaps the would be a better place to ask follow-up questions.

edit flag offensive delete link more


Cool! Thank you

DustyNinja gravatar image DustyNinja  ( 2020-05-22 13:39:32 -0500 )edit

And you were correct i did not realise that due to the speed of the video the 40sec video got over in just 15sec. So when the video stops it gives the numpy error as the node is still running. So to prevent that i have looped the video by resetting the frames

DustyNinja gravatar image DustyNinja  ( 2020-05-22 17:17:49 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2020-05-22 06:15:11 -0500

Seen: 323 times

Last updated: May 22 '20