Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Trouble converting cv2 to imgmsg

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