Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 from collections import defaultdict
00037 import rospy
00038 from sensor_msgs.msg import Image, CameraInfo
00039 from naoqi_driver.naoqi_node import NaoqiNode
00040 import camera_info_manager
00041 from sensor_msgs.msg._CameraInfo import CameraInfo
00042
00043 from naoqi import ALProxy
00044
00045 from cv_bridge import CvBridge, CvBridgeError
00046 import cv2
00047 import numpy as np
00048
00049
00050 from naoqi_sensors.vision_definitions import kDepthColorSpace
00051
00052 class NaoCam (NaoqiNode):
00053 def __init__(self):
00054 NaoqiNode.__init__(self, 'nao_depth')
00055
00056 self.camProxy = self.get_proxy("ALVideoDevice")
00057 if self.camProxy is None:
00058 exit(1)
00059 self.nameId = None
00060 self.camera_infos = {}
00061 def returnNone():
00062 return None
00063
00064 self.bridge = CvBridge()
00065
00066 self.pub_img_ = rospy.Publisher('~image_raw', Image)
00067 self.pub_cimg_ = rospy.Publisher('~image_color', Image)
00068 self.pub_info_ = rospy.Publisher('~camera_info', CameraInfo)
00069
00070
00071 self.setup_cam()
00072
00073 def setup_cam( self):
00074
00075 self.camProxy.unsubscribeAllInstances("rospy_gvm")
00076
00077 kDepthCamera = 2
00078 self.frame_id = rospy.get_param("~frame_id", "/CameraDepth_frame")
00079 resolution = rospy.get_param("~resolution", 1)
00080 framerate = rospy.get_param("~fps", 15)
00081 color_space= rospy.get_param("~color_space", 17)
00082 self.nameId = self.camProxy.subscribeCamera("rospy_gvm", kDepthCamera, resolution, color_space, framerate)
00083 rospy.loginfo('Using camera: depth camera. Subscriber name is %s .' % (self.nameId))
00084
00085 return
00086
00087 def main_loop(self):
00088 img = Image()
00089 cimg = Image()
00090 r = rospy.Rate(15)
00091 while not rospy.is_shutdown():
00092 if self.pub_img_.get_num_connections() == 0:
00093 r.sleep()
00094 continue
00095
00096 image = self.camProxy.getImageRemote(self.nameId)
00097 if image is None:
00098 continue
00099
00100 '''
00101 #Images received from NAO have
00102 if self.config['use_ros_time']:
00103 img.header.stamp = rospy.Time.now()
00104 else:
00105 img.header.stamp = rospy.Time(image[4] + image[5]*1e-6)
00106 '''
00107 img.header.stamp = rospy.Time.now()
00108 img.header.frame_id = self.frame_id
00109 img.height = image[1]
00110 img.width = image[0]
00111 nbLayers = image[2]
00112 if image[3] == kDepthColorSpace:
00113 encoding = "mono16"
00114 else:
00115 rospy.logerr("Received unknown encoding: {0}".format(image[3]))
00116 img.encoding = encoding
00117 img.step = img.width * nbLayers
00118 img.data = image[6]
00119
00120 self.pub_img_.publish(img)
00121
00122
00123 infomsg = CameraInfo()
00124 infomsg.header = img.header
00125
00126 ratio_x = float(640)/float(img.width)
00127 ratio_y = float(480)/float(img.height)
00128 infomsg.width = img.width
00129 infomsg.height = img.height
00130
00131 infomsg.K = [ 525, 0, 3.1950000000000000e+02,
00132 0, 525, 2.3950000000000000e+02,
00133 0, 0, 1 ]
00134 infomsg.P = [ 525, 0, 3.1950000000000000e+02, 0,
00135 0, 525, 2.3950000000000000e+02, 0,
00136 0, 0, 1, 0 ]
00137
00138 for i in range(3):
00139 infomsg.K[i] = infomsg.K[i] / ratio_x
00140 infomsg.K[3+i] = infomsg.K[3+i] / ratio_y
00141 infomsg.P[i] = infomsg.P[i] / ratio_x
00142 infomsg.P[4+i] = infomsg.P[4+i] / ratio_y
00143
00144 infomsg.D = []
00145 infomsg.binning_x = 0
00146 infomsg.binning_y = 0
00147 infomsg.distortion_model = ""
00148 self.pub_info_.publish(infomsg)
00149
00150
00151
00152
00153 colorimg = np.zeros((img.height,img.width,3), np.uint8)
00154 try:
00155 cimg = self.bridge.cv2_to_imgmsg(colorimg, "bgr8")
00156 cimg.header.stamp = img.header.stamp
00157 cimg.header.frame_id = img.header.frame_id
00158 self.pub_cimg_.publish(cimg)
00159 except CvBridgeError, e:
00160 print e
00161
00162 r.sleep()
00163
00164
00165 self.camProxy.unsubscribe(self.nameId)
00166
00167 if __name__ == "__main__":
00168 try:
00169 naocam = NaoCam()
00170 naocam.main_loop()
00171 except RuntimeError as e:
00172 rospy.logerr('Something went wrong: %s' % str(e) )
00173 rospy.loginfo('Camera stopped')