Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 import roslib
00013 roslib.load_manifest('hrl_camera')
00014 import rospy
00015 import cv
00016 import time
00017 from std_msgs.msg import String
00018 from sensor_msgs.msg import Image
00019 from cv_bridge.cv_bridge import CvBridge, CvBridgeError
00020 import hrl_camera.hrl_camera as hc
00021 import hrl_camera.camera as cam
00022 from camera_config import camera_parameters as ccp
00023
00024 from sensor_msgs.msg import CameraInfo
00025
00026
00027 if __name__ == '__main__':
00028 import optparse
00029 p = optparse.OptionParser()
00030
00031 p.add_option('--cam_list', action='store', type='string', dest='cam_list',
00032 default=None, help='colon (:) separated list of camera names')
00033
00034 opt, args = p.parse_args()
00035
00036 if opt.cam_list == None:
00037 import camera_uuid as cu
00038 rospy.logout('This utility is used for publishing ')
00039 rospy.logout('named and calibrated OpenCV accessible')
00040 rospy.logout('camera images over ROS.')
00041 rospy.logout('')
00042 rospy.logout('You did not specify a camera name.')
00043 rospy.logout('Available cameras:' + str(cu.camera_names()))
00044 exit()
00045
00046 cam_name_list = opt.cam_list.split(':')
00047
00048 bridge = CvBridge()
00049 from socket import gethostname
00050 rospy.init_node('camera_publisher_'+gethostname(), anonymous=False)
00051
00052 cameras = []
00053 image_pub_l = []
00054 caminfo_pub_l = []
00055
00056 for c in cam_name_list:
00057 camera = hc.find_camera(c)
00058 rospy.logout('====================================================')
00059 if 'frame_rate' in ccp[c]:
00060 fps = ccp[c]['frame_rate']
00061 rospy.logout('Setting '+c+' frame rate to %.2f'%(fps))
00062 camera.set_frame_rate(fps)
00063
00064 if 'ros_topic' in ccp[c]:
00065 topic_name = ccp[c]['ros_topic']
00066 else:
00067 topic_name = 'cvcamera_' + c
00068
00069 image_pub = rospy.Publisher(topic_name, Image)
00070 config_pub = rospy.Publisher(topic_name+'_info', CameraInfo)
00071
00072
00073
00074 rospy.logout('Opening OpenCV camera with ID ' + c)
00075 rospy.logout('Publishing on topic ' + topic_name)
00076 rospy.logout('Camera operating at rate %.2f'%(camera.get_frame_rate()))
00077
00078 cameras.append(camera)
00079 image_pub_l.append(image_pub)
00080 caminfo_pub_l.append(config_pub)
00081
00082 n_cameras = len(cameras)
00083
00084 while not rospy.is_shutdown():
00085 for i in range(n_cameras):
00086 camera, image_pub, config_pub = cameras[i], image_pub_l[i], caminfo_pub_l[i]
00087 try:
00088 cv_image = cv.CloneImage(camera.get_frame())
00089
00090 rosimage = bridge.cv_to_imgmsg(cv_image, "bgr8")
00091 image_pub.publish(rosimage)
00092 m = camera.intrinsic_cvmat
00093 intrinsic_list = [m[0,0], m[0,1], m[0,2], 0.0, m[1,0], m[1,1], m[1,2], 0.0, m[2,0], m[2,1], m[2,2], 0.0]
00094 config_pub.publish(CameraInfo(P=intrinsic_list))
00095 except rospy.exceptions.ROSSerializationException, e:
00096 rospy.logerr('serialization exception')
00097 except CvBridgeError, e:
00098 print e
00099 break
00100 except KeyboardInterrupt:
00101 rospy.logout("Shutting down.")
00102 break
00103 time.sleep(1/100.0)
00104
00105 cv.DestroyAllWindows()
00106
00107