cnn_bridge_main.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # BSD 3-Clause License
3 
4 # Copyright (c) 2019, Noam C. Golombek
5 # All rights reserved.
6 
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 
10 # 1. Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 
13 # 2. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 
17 # 3. Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 
33 import rospy
34 from cnn_manager import CNNManager
35 from metadata_service import MetadataService
36 from tools import str2bool
37 
38 
39 def parse_args():
40  """Parse the arguments provided when the module was initialized"""
41 
42  rospy.init_node('cnn_bridge', log_level=rospy.DEBUG)
43  params = {}
44  #'The camera or video file to get the pictures from, '
45  # 'options are: (String, Required) \n If video file, then '
46  # 'path to the video file \n If ROS camera, then the topic name
47  # '\n If CV2 device, then device ID (0, 1, 3...)'
48  if rospy.has_param('~source'):
49  if isinstance(rospy.get_param('~source'), str):
50  params['source'] = rospy.get_param('~source')
51  else:
52  raise rospy.ROSInitException(
53  'The source display needs to be of type: String')
54  else:
55  raise rospy.ROSInitException('Param source is required (String)')
56 
57  # Path to Hypes file (String, Required)
58  if rospy.has_param('~logdir'):
59  if isinstance(rospy.get_param('~logdir'), str):
60  params['logdir'] = rospy.get_param('~logdir')
61  else:
62  raise rospy.ROSInitException(
63  'The logdir display needs to be of type: String')
64  else:
65  raise rospy.ROSInitException('Param logdir is required (String)')
66 
67  # Path to the metadata file
68  if rospy.has_param('~metadata_source'):
69  if isinstance(rospy.get_param('~metadata_source'), str):
70  params['metadata_source'] = rospy.get_param('~metadata_source')
71  else:
72  raise rospy.ROSInitException(
73  'The metadata source needs to be of type: String')
74  else:
75  raise rospy.ROSInitException('Param source is required (String)')
76 
77  # Self explanatory.
78  if rospy.has_param('~input_tensor'):
79  if isinstance(rospy.get_param('~input_tensor'), str):
80  params['input_tensor'] = rospy.get_param('~input_tensor')
81  else:
82  raise rospy.ROSInitException(
83  'The input_tensor needs to be of type: String')
84  else:
85  raise rospy.ROSInitException('Param input_tensor is required (String)')
86 
87  # If segmentation, self explanatory. If detection an array of three tensors that are [boxes,scores,classes]
88  if rospy.has_param('~output_tensor'):
89  if isinstance(rospy.get_param('~output_tensor'), str):
90  params['output_tensor'] = rospy.get_param('~output_tensor')
91  else:
92  raise rospy.ROSInitException(
93  'The output_tensor needs to be of type: String')
94  else:
95  raise rospy.ROSInitException(
96  'Param output_tensor is required (String)')
97 
98  # Whether to display the output or not (Boolean, Default True)
99  if rospy.has_param('~display'):
100  if str2bool(rospy.get_param('~display')) is not None:
101  params['display'] = rospy.get_param('~display')
102  else:
103  raise rospy.ROSInitException(
104  'The param display needs to be of type: Boolean')
105  else:
106  params['display'] = True
107 
108  # File to save the video to: (String) \n Use False to disable\n '
109  # 'True saves to Camera__datetime \n If a string is provided it '
110  # 'will be the video title
111  if rospy.has_param('~video_save'):
112  params['video_save'] = rospy.get_param('~video_save')
113  else:
114  params['video_save'] = ''
115 
116  # Sets whether to use an Nvidia GPU (bool, Default False)
117  if rospy.has_param('~cpu'):
118  if str2bool(rospy.get_param('~cpu')) is not None:
119  params['cpu'] = rospy.get_param('~cpu')
120  else:
121  raise rospy.ROSInitException(
122  'The param cpu needs to be of type: Boolean')
123  else:
124  params['cpu'] = False
125 
126  # Sets what mode the program is running in (String, Either detection or segmentation)
127  if rospy.has_param('~mode'):
128  if isinstance(rospy.get_param('~metadata_source'), str):
129  if rospy.get_param('~mode') == 'detection' or rospy.get_param('~mode') == 'segmentation':
130  params['mode'] = rospy.get_param('~mode')
131  else:
132  raise rospy.ROSInitException(
133  'Unknown mode')
134  else:
135  raise rospy.ROSInitException(
136  'The param mode needs to be of type: String')
137  else:
138  raise rospy.ROSInitException('Param mode is required (String)')
139 
140  # Sets the percentage of an Nvidia GPU to use. This is used generally for running simultaneous networks.
141  if rospy.has_param('~gpu_percent'):
142  if isinstance(rospy.get_param('~gpu_percent'), float):
143  params['gpu_percent'] = rospy.get_param('~gpu_percent')
144  else:
145  raise rospy.ROSInitException(
146  'The param gpu_percent needs to be of type: Float')
147  else:
148  params['gpu_percent'] = 1
149 
150  return params
151 
152 
153 if __name__ == "__main__":
154  args = parse_args()
155  tensor_io = {}
156  tensor_io['input_tensor'] = args['input_tensor']
157  tensor_io['output_tensor'] = args['output_tensor']
158  if args['mode'] == 'segmentation':
159  tensor_io['output_tensor'] = args['output_tensor']
160  elif args['mode'] == 'detection':
161  tensor_io['output_tensor'] = args['output_tensor'].split(',')
162 
163  manager = CNNManager(args['source'], args['video_save'], args['logdir'], args['cpu'],
164  args['display'], tensor_io, args['mode'], gpu_percent=args['gpu_percent'])
165  metadata_srv = MetadataService(
166  manager.camera.original_image_size, args['metadata_source'], args['logdir'], args['mode'])
167  manager.run_loop()
def str2bool(s, return_string=False)
Definition: misc.py:56


cnn_bridge
Author(s): Noam C. Golombek , Alexander Beringolts
autogenerated on Mon Jun 10 2019 12:53:26