Ask Your Question
0

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.

Code:

#!/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 = video_capture.read()
    ros_image = CvBridge().cv2_to_imgmsg(rgb_frame, "bgr8")
    video_publisher.publish(ros_image)
    cv2.waitKey(10)
  video_capture.release()   

if __name__ == '__main__':

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

Error:

Traceback (most recent call last):
  File "./image_publisher_node.py", line 21, in <module>
    read_video()
  File "./image_publisher_node.py", 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/core.py", 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

Comments

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
1

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

Likely the issue is that your rgb_frame is None because the video_capture.read() 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 https://answers.opencv.org/questions/ would be a better place to ask follow-up questions.

edit flag offensive delete link more

Comments

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

Stats

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

Seen: 323 times

Last updated: May 22 '20