camera_depth.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # ROS node to provide access to the camera by wrapping NaoQI access (may not
5 # be the most efficient way...)
6 #
7 # Copyright 2012 Daniel Maier, University of Freiburg
8 # Copyright 2014 Aldebaran Robotics
9 # http://www.ros.org/wiki/nao
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are met:
13 #
14 # # Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # # Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 # # Neither the name of the University of Freiburg nor the names of its
20 # contributors may be used to endorse or promote products derived from
21 # this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 #
35 
36 from collections import defaultdict
37 import rospy
38 from sensor_msgs.msg import Image, CameraInfo
39 from naoqi_driver.naoqi_node import NaoqiNode
40 import camera_info_manager
41 from sensor_msgs.msg._CameraInfo import CameraInfo
42 
43 from naoqi import ALProxy
44 
45 from cv_bridge import CvBridge, CvBridgeError
46 import cv2
47 import numpy as np
48 
49 # import color spaces
50 from naoqi_sensors.vision_definitions import kDepthColorSpace
51 
52 class NaoCam (NaoqiNode):
53  def __init__(self):
54  NaoqiNode.__init__(self, 'nao_depth')
55 
56  self.camProxy = self.get_proxy("ALVideoDevice")
57  if self.camProxy is None:
58  exit(1)
59  self.nameId = None
60  self.camera_infos = {}
61  def returnNone():
62  return None
63 
64  self.bridge = CvBridge()
65  # ROS publishers
66  self.pub_img_ = rospy.Publisher('~image_raw', Image)
67  self.pub_cimg_ = rospy.Publisher('~image_color', Image)
68  self.pub_info_ = rospy.Publisher('~camera_info', CameraInfo)
69 
70  # initialize the parameter of the depth camera
71  self.setup_cam()
72 
73  def setup_cam( self):
74  # unsubscribe for all zombie subscribers
75  self.camProxy.unsubscribeAllInstances("rospy_gvm")
76  # subscribe
77  kDepthCamera = 2
78  self.frame_id = rospy.get_param("~frame_id", "/CameraDepth_frame")
79  resolution = rospy.get_param("~resolution", 1) #320x240
80  framerate = rospy.get_param("~fps", 15)
81  color_space= rospy.get_param("~color_space", 17) # mono16
82  self.nameId = self.camProxy.subscribeCamera("rospy_gvm", kDepthCamera, resolution, color_space, framerate)
83  rospy.loginfo('Using camera: depth camera. Subscriber name is %s .' % (self.nameId))
84 
85  return
86 
87  def main_loop(self):
88  img = Image()
89  cimg = Image()
90  r = rospy.Rate(15)
91  while not rospy.is_shutdown():
92  if self.pub_img_.get_num_connections() == 0:
93  r.sleep()
94  continue
95 
96  image = self.camProxy.getImageRemote(self.nameId)
97  if image is None:
98  continue
99  # Deal with the image
100  '''
101  #Images received from NAO have
102  if self.config['use_ros_time']:
103  img.header.stamp = rospy.Time.now()
104  else:
105  img.header.stamp = rospy.Time(image[4] + image[5]*1e-6)
106  '''
107  img.header.stamp = rospy.Time.now()
108  img.header.frame_id = self.frame_id
109  img.height = image[1]
110  img.width = image[0]
111  nbLayers = image[2]
112  if image[3] == kDepthColorSpace:
113  encoding = "mono16"
114  else:
115  rospy.logerr("Received unknown encoding: {0}".format(image[3]))
116  img.encoding = encoding
117  img.step = img.width * nbLayers
118  img.data = image[6]
119 
120  self.pub_img_.publish(img)
121 
122  # deal with the camera info
123  infomsg = CameraInfo()
124  infomsg.header = img.header
125  # yes, this is only for an XTion / Kinect but that's the only thing supported by NAO
126  ratio_x = float(640)/float(img.width)
127  ratio_y = float(480)/float(img.height)
128  infomsg.width = img.width
129  infomsg.height = img.height
130  # [ 525., 0., 3.1950000000000000e+02, 0., 525., 2.3950000000000000e+02, 0., 0., 1. ]
131  infomsg.K = [ 525, 0, 3.1950000000000000e+02,
132  0, 525, 2.3950000000000000e+02,
133  0, 0, 1 ]
134  infomsg.P = [ 525, 0, 3.1950000000000000e+02, 0,
135  0, 525, 2.3950000000000000e+02, 0,
136  0, 0, 1, 0 ]
137 
138  for i in range(3):
139  infomsg.K[i] = infomsg.K[i] / ratio_x
140  infomsg.K[3+i] = infomsg.K[3+i] / ratio_y
141  infomsg.P[i] = infomsg.P[i] / ratio_x
142  infomsg.P[4+i] = infomsg.P[4+i] / ratio_y
143 
144  infomsg.D = []
145  infomsg.binning_x = 0
146  infomsg.binning_y = 0
147  infomsg.distortion_model = ""
148  self.pub_info_.publish(infomsg)
149 
150  #Currently we only get depth image from the 3D camera of NAO, so we make up a fake color image (a black image)
151  #and publish it under image_color topic.
152  #This should be update when the color image from 3D camera becomes available.
153  colorimg = np.zeros((img.height,img.width,3), np.uint8)
154  try:
155  cimg = self.bridge.cv2_to_imgmsg(colorimg, "bgr8")
156  cimg.header.stamp = img.header.stamp
157  cimg.header.frame_id = img.header.frame_id
158  self.pub_cimg_.publish(cimg)
159  except CvBridgeError, e:
160  print e
161 
162  r.sleep()
163 
164 
165  self.camProxy.unsubscribe(self.nameId)
166 
167 if __name__ == "__main__":
168  try:
169  naocam = NaoCam()
170  naocam.main_loop()
171  except RuntimeError as e:
172  rospy.logerr('Something went wrong: %s' % str(e) )
173  rospy.loginfo('Camera stopped')
def get_proxy(self, name, warn=True)


romeo_sensors_py
Author(s): Ha Dang
autogenerated on Mon Jun 10 2019 14:45:54