Ask Your Question
1

Trouble converting cv2 to imgmsg

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

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 -0600

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 imageebaumert ( 2016-02-17 07:17:15 -0600 )edit

Thanks for answer. I too had the same doubt.

kk2105 gravatar imagekk2105 ( 2019-04-04 22:47:11 -0600 )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

1 follower

Stats

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

Seen: 4,006 times

Last updated: Feb 16 '16