check_camera_info_matrix.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 @file check_camera_info_matrix.py
4 """
5 import os
6 import sys
7 import subprocess
8 import signal
9 import unittest
10 import re
11 import time
12 import rospy
13 import rostest
14 from rs_general.rs_general import parse_camera_type
15 
16 PKG = 'realsense_camera'
17 NAME = 'matrix_check'
18 PROJECTION = 'projection_matrix'
19 PROJECTION_LENGHT = 12
20 DISTORTION = 'distortion_coefficients'
21 DISTORTION_LENGHT = 5
22 ROTATION = 'rotation_identity'
23 ROTATION_LENGHT = 9
24 DEPTH = 'depth'
25 COLOR = 'color'
26 IR = 'ir'
27 IR2 = 'ir2'
28 FISHEYE = 'fisheye'
29 CAMERAS = ['R200', 'F200', 'SR300', 'ZR300']
30 
31 
32 class CheckCameraInfoMatrix(unittest.TestCase):
33  """
34  @class CheckCameraInfoMatrix
35  """
36 
37  def setUp(self):
38  """
39  @fn setUp
40  @param self
41  @return
42  """
43  self.assertIn(camera_type, CAMERAS)
44 
45  def get_matrix(self, string, matrix_type):
46  """search values of matrix type from string,
47  and organize into matrix array
48  @fn get_matrix
49  @param self
50  @param string
51  @param matrix_type
52  @return matrix_array: matrix values
53  """
54  if (matrix_type == PROJECTION and not string.find('P: ') == -1) \
55  or (matrix_type == DISTORTION and
56  not string.find('D: ') == -1) \
57  or (matrix_type == ROTATION and
58  not string.find('R: ') == -1):
59  values = re.findall(r'([0-9]+\.[0-9]+)', string)
60  matrix_array = map(float, values)
61  return matrix_array
62  else:
63  return ''
64 
65  def is_zero(self, value):
66  """determine whether the value equal to 0
67  @fn is_zero
68  @param self
69  @param value
70  @return True: equal to 0; False: not equal to 0
71  """
72  if (value > -0.00000001) and (value < 0.00000001):
73  return True
74  else:
75  return False
76 
77  def get_camera_info(self, stream_type, matrix_type):
78  """run rostopic to print camera info, get matrix values from stdout
79  @fn get_camera_info
80  @param self
81  @param stream_type
82  @param matrix_type
83  @return matrix_array
84  """
85  cmd = 'rostopic echo /camera/' + stream_type + '/camera_info'
86  print "cmd = ", cmd
87  process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
88  stderr=subprocess.PIPE, shell=True)
89  while True:
90  buff = process.stdout.readline()
91  if buff == '' and process.poll is not None:
92  break
93  array = self.get_matrix(buff, matrix_type)
94  if array != '':
95  os.kill(process.pid, signal.SIGKILL)
96  return array
97  return ''
98 
99  def verify_projection_matrix(self, matrix, stream_type):
100  """verify projection matrix values, adapt each stream type
101  @fn verify_projection_matrix
102  @param self
103  @param matrix
104  @param stream_type
105  return
106  """
107  if matrix == '' or len(matrix) != PROJECTION_LENGHT \
108  or stream_type == '':
109  self.assertTrue(False,
110  'Not get correct projection matrix or stream type.')
111 
112  if stream_type == DEPTH:
113  if (self.is_zero(matrix[3]) or
114  self.is_zero(matrix[7]) or
115  self.is_zero(matrix[11])):
116  self.assertTrue(False, 'Projection matrix value is incorrect.')
117  elif (stream_type == COLOR or
118  stream_type == IR or
119  stream_type == IR2 or
120  stream_type == FISHEYE):
121  if (not self.is_zero(matrix[3]) or
122  not self.is_zero(matrix[7]) or
123  not self.is_zero(matrix[11])):
124  self.assertTrue(False, 'Projection matrix value is incorrect.')
125  else:
126  self.assertTrue(False,
127  'Stream type "' + stream_type + '" is invalid.')
128 
129  def verify_rotation_matrix(self, matrix):
130  """verify rotation matrix values, adapt each stream type
131  @fn verify_rotation_matrix
132  @param self
133  @param matrix
134  return
135  """
136  if matrix == '' or len(matrix) != ROTATION_LENGHT:
137  self.assertTrue(False, 'Not get correct rotation matrix.')
138 
139  for i in range(0, len(matrix)):
140  if i == 0 or i == 4 or i == 8:
141  if matrix[i] < 0.99999999 or matrix[i] > 1.00000001:
142  self.assertTrue(False, 'Rotation matrix value is incorrect.')
143  else:
144  if not self.is_zero(matrix[i]):
145  self.assertTrue(False, 'Rotation matrix value is incorrect.')
146 
147  def verify_distortion_matrix(self, matrix, stream_type):
148  """verify distortion matrix values, adapt each stream type
149  @fn verify_distortion_matrix
150  @param self
151  @param matrix
152  @param stream_type
153  return
154  """
155  if camera_type == '' or stream_type == '' or matrix == '' \
156  or len(matrix) != DISTORTION_LENGHT:
157  self.assertTrue(False,
158  'Not get correct distortion matrix or stream type.')
159 
160  if camera_type == 'R200' or camera_type == 'ZR300':
161  if stream_type == COLOR:
162  for i in range(0, len(matrix)):
163  if i == 4:
164  self.assertTrue(self.is_zero(matrix[i]), 'Distortion \
165  matrix value of color is incorrect.')
166  else:
167  self.assertFalse(self.is_zero(matrix[i]), 'Distortion \
168  matrix value of color is incorrect.')
169  elif (stream_type == DEPTH or
170  stream_type == IR or
171  stream_type == IR2):
172  for i in range(0, len(matrix)):
173  self.assertTrue(self.is_zero(matrix[i]), 'Distortion \
174  matrix value of (depth/IR/IR2) is incorrect.')
175  elif stream_type == FISHEYE:
176  for i in range(0, len(matrix)):
177  if i == 0:
178  self.assertFalse(self.is_zero(matrix[i]), 'Distortion \
179  matrix value of fisheye is incorrect.')
180  else:
181  self.assertTrue(self.is_zero(matrix[i]), 'Distortion \
182  matrix value of fisheye is incorrect.')
183  else:
184  self.assertTrue(False,
185  'Stream type "' + stream_type + '" is invalid.')
186  elif camera_type == 'F200' or camera_type == 'SR300':
187  if stream_type == COLOR:
188  for i in range(0, len(matrix)):
189  self.assertTrue(self.is_zero(matrix[i]), 'Distortion \
190  matrix value of color is incorrect.')
191  elif stream_type == DEPTH or stream_type == IR:
192  for i in range(0, len(matrix)):
193  self.assertFalse(self.is_zero(matrix[i]), 'Distortion \
194  matrix value of (depth/IR) is incorrect.')
195  else:
196  self.assertTrue(False,
197  'Stream type "' + stream_type + '" is invalid.')
198 
200  """check projection matrix values from camera info
201  @fn test_check_projection_matrix
202  @param self
203  @return
204  """
205  rospy.init_node(NAME, anonymous=True, log_level=rospy.INFO)
206 
207  # wait for nodelet to be started
208  time.sleep(10)
209 
211  self.get_camera_info(DEPTH, PROJECTION), DEPTH)
212  time.sleep(0.5)
214  self.get_camera_info(COLOR, PROJECTION), COLOR)
215  time.sleep(0.5)
217  self.get_camera_info(IR, PROJECTION), IR)
218  time.sleep(0.5)
219  if camera_type == 'R200' or camera_type == 'ZR300':
221  self.get_camera_info(IR2, PROJECTION), IR2)
222  time.sleep(0.5)
223  if camera_type == 'ZR300':
225  self.get_camera_info(FISHEYE, PROJECTION), FISHEYE)
226  time.sleep(0.5)
227 
229  """check rotation identity matrix values from camera info
230  @fn test_check_rotation_identity_matrix
231  @param self
232  @return
233  """
234  rospy.init_node(NAME, anonymous=True, log_level=rospy.INFO)
235 
236  # wait for nodelet to be started
237  time.sleep(10)
238 
239  self.verify_rotation_matrix(self.get_camera_info(DEPTH, ROTATION))
240  time.sleep(0.5)
241  self.verify_rotation_matrix(self.get_camera_info(COLOR, ROTATION))
242  time.sleep(0.5)
243  self.verify_rotation_matrix(self.get_camera_info(IR, ROTATION))
244  time.sleep(0.5)
245  if camera_type == 'R200' or camera_type == 'ZR300':
246  self.verify_rotation_matrix(self.get_camera_info(IR2, ROTATION))
247  time.sleep(0.5)
248  if camera_type == 'ZR300':
249  self.verify_rotation_matrix(self.get_camera_info(FISHEYE, ROTATION))
250  time.sleep(0.5)
251 
253  """check distortion coefficients matrix values from camera info
254  @fn test_check_distortion_coefficients
255  @param self
256  @return
257  """
258  rospy.init_node(NAME, anonymous=True, log_level=rospy.INFO)
259 
260  # wait for nodelet to be started
261  time.sleep(10)
262 
264  self.get_camera_info(DEPTH, DISTORTION), DEPTH)
265  time.sleep(0.5)
267  self.get_camera_info(COLOR, DISTORTION), COLOR)
268  time.sleep(0.5)
270  self.get_camera_info(IR, DISTORTION), IR)
271  time.sleep(0.5)
272  if camera_type == 'R200' or camera_type == 'ZR300':
274  self.get_camera_info(IR2, DISTORTION), IR2)
275  time.sleep(0.5)
276  if camera_type == 'ZR300':
278  self.get_camera_info(FISHEYE, DISTORTION), FISHEYE)
279  time.sleep(0.5)
280 
281 if __name__ == '__main__':
282  camera_type = parse_camera_type(sys.argv)
283  rostest.rosrun(PKG, NAME, CheckCameraInfoMatrix, sys.argv)
def get_camera_info(self, stream_type, matrix_type)
def parse_camera_type(args)
Definition: rs_general.py:23


realsense_camera
Author(s): Rajvi Jingar , Reagan Lopez , Matt Hansen , Mark Horn
autogenerated on Mon Jun 10 2019 14:40:37