ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Trouble converting cv2 to imgmsg

asked 2016-02-16 11:36:46 -0500

ebaumert gravatar image

Hi everyone,

I am trying to write a python node that publishes single frames the attached webcam records. However, when I try to run it I get the following error message.

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Traceback (most recent call last):
  File "/home/x/catkin_ws/src/webcam_pub/webcam_pub/src/webcam_pub.py", line 34, in <module>
    webcam_pub()
  File "/home/x/catkin_ws/src/webcam_pub/webcam_pub/src/webcam_pub.py", line 23, in webcam_pub
    msg = cv2_to_imgmsg(frame, encoding="bgr8")
NameError: global name 'cv2_to_imgmsg' is not defined

I do not understand why I cannot call the cv2_to_imgmsg function as defined here.

Help would be appreciated - my node looks as follows.

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError

def webcam_pub():
 pub = rospy.Publisher('webcam/image_raw', Image, queue_size=1)
 rospy.init_node('webcam_pub', anonymous=True)
 rate = rospy.Rate(60) # 60hz

 cam = cv2.VideoCapture(0)

 if not cam.isOpened():
  sys.stdout.write("Webcam is not available")
  return -1

 while not rospy.is_shutdown():
  ret, frame = cam.read()
  msg = cv2_to_imgmsg(frame, encoding="bgr8")

  if ret:
   rospy.loginfo("Capturing image failed.")

  pub.publish(msg)
  rate.sleep()


if __name__ == '__main__':
 try:
  webcam_pub()
 except rospy.ROSInterruptException:
  pass
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-02-16 11:48:45 -0500

Check out the example Python script at the bottom of the tutorial you linked. Notice that both imgmsg_to_cv2 and cv2_to_imgmsg are methods that belong to the cv_bridge.CvBridge class. Your code needs to mimic this structure. First instantiate an instance of cv_bridge.CvBridge, and then call the cv2_to_imgmsg method. Something like:

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError

def webcam_pub():
    pub = rospy.Publisher('webcam/image_raw', Image, queue_size=1)
    rospy.init_node('webcam_pub', anonymous=True)
    rate = rospy.Rate(60) # 60hz

    cam = cv2.VideoCapture(0)
    bridge = CvBridge()

    if not cam.isOpened():
         sys.stdout.write("Webcam is not available")
         return -1

    while not rospy.is_shutdown():
        ret, frame = cam.read()
        msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")

        if ret:
            rospy.loginfo("Capturing image failed.")

        pub.publish(msg)
        rate.sleep()


if __name__ == '__main__':
    try:
        webcam_pub()
except rospy.ROSInterruptException:
    pass
edit flag offensive delete link more

Comments

Thank you very much, I must have overlooked the instantiation!

ebaumert gravatar image ebaumert  ( 2016-02-17 07:17:15 -0500 )edit

Thanks for answer. I too had the same doubt.

kk2105 gravatar image kk2105  ( 2019-04-04 22:47:11 -0500 )edit

I run the program (but opening a file instead of camera) and eventhough the camera is open , if ret shows that the image capture failed. Shoouldnt it be if not ret??

Kansai gravatar image Kansai  ( 2021-03-23 02:20:03 -0500 )edit

@Kansai at a quick glance I'd say you are probably correct. I was just copying OPs code. My goal was to answer the original question not debug other issues in the code.

jarvisschultz gravatar image jarvisschultz  ( 2021-03-23 09:14:41 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-16 11:36:46 -0500

Seen: 8,174 times

Last updated: Feb 16 '16