def get_camera_info_for_robot(robot_name): """ Returns a CameraInfo object for the given robot. This is in a good format to pass to PinholeCameraModel: self.pcm = PinholeCameraModel() self.pcm.fromCameraInfo(self.ci) The fields are simply lists (not array or matrix). Raises: NoCameraInfoAvailable if no info available InvalidCameraInfo if the info exists but is invalid """ if robot_name == dtu.DuckietownConstants.ROBOT_NAME_FOR_TESTS: calib_data = dtu.yaml_load(default_camera_info) else: # find the file fn = get_camera_info_config_file(robot_name) # load the YAML calib_data = dtu.yaml_load_file(fn, plain_yaml=True) # convert the YAML try: camera_info = camera_info_from_yaml(calib_data) except InvalidCameraInfo as e: msg = 'Invalid data in file %s' % fn dtu.raise_wrapped(InvalidCameraInfo, e, msg) #check_camera_info_sane_for_DB17(camera_info) return camera_info
def parse_date_spec(d): from dateutil.parser import parse try: return parse(d) except ValueError as e: msg = 'Cannot parse date %s.' % d.__repr__() dtu.raise_wrapped(RTParseError, e, msg, compact=True)
def r(m): try: yield except EvaluationError as e: msg = 'Cannot evaluate binary operation: error during %s' % m msg += '\n' + str(self) dtu.raise_wrapped(EvaluationError, e, msg, compact=True)
def check(self): try: d = dtu.get_duckiefleet_root() return "Duckiefleet root is detected as %r" % d except dtu.DTConfigException as e: msg = 'Could not get the duckiefleet root in any way.' dtu.raise_wrapped(CheckFailed, e, msg)
def homography_from_yaml(data): try: h = data['homography'] res = np.array(h).reshape((3, 3)) return res except Exception as e: msg = 'Could not interpret data:' msg += '\n\n' + dtu.indent(yaml.dump(data), ' ') dtu.raise_wrapped(InvalidHomographyInfo, e, msg)
def __call__(self, a, b): try: expect_float(a) expect_float(b) val = self.f(a, b) desc = '%s %s %s' % (a, self.which, b) return ResultWithDescription(val, desc) except EvaluationError as e: msg = 'While evaluating %s(%s, %s)' % (self.f.__name__, a, b) dtu.raise_wrapped(EvaluationError, e, msg, compact=True)
def load_configuration_subscriptions(data): res = OrderedDict() for k, v in data.items(): try: check_good_name(k) res[k] = load_configuration_subscription(k, v) except DTConfigException as e: msg = 'Invalid subscription entry %r:' % k raise_wrapped(DTConfigException, e, msg, compact=True) return res
def load_configuration_publishers(data): res = OrderedDict() for k, v in data.items(): try: check_good_name(k) res[k] = load_configuration_publisher(k, v) except dtu.DTConfigException as e: msg = 'Invalid publisher entry %r:' % k dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True) return res
def check(self): try: path = dtu.get_machines_files_path() except dtu.DTConfigException as e: msg = 'Could not get path to machines.' dtu.raise_wrapped(CheckError, e, msg) if not os.path.exists(path): msg = 'Machines file does not exist.' l = 'File does not exist: %s ' % path raise CheckFailed(msg, l)
def _on_parameters_changed(self, first_time, values): try: values1 = UpdatedParameters(**values) values1.set_allowed(list(self._configuration.parameters)) self.on_parameters_changed(first_time, values1) except DTConfigException as e: msg = 'Configuration error raised by on_parameters_changed()' msg += '\n\n' + indent(yaml_dump(values), ' ', 'Configuration: ') raise_wrapped(DTConfigException, e, msg, compact=True) except Exception as e: msg = 'Configuration error raised by on_parameters_changed().' msg += '\n\n' + indent(yaml_dump(values), ' ', 'Configuration: ') raise_wrapped(DTConfigException, e, msg)
def load_configuration(realpath, contents): # TODO: load "version" string try: try: data = dtu.yaml_load(contents) except Exception as e: msg = 'Could not parse YAML file properly:' dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True) if not isinstance(data, dict): msg = 'Expected a dict, got %s.' % type(data).__name__ raise dtu.DTConfigException(msg) try: parameters = data.pop('parameters') subscriptions = data.pop('subscriptions') publishers = data.pop('publishers') contracts = data.pop('contracts') description = data.pop('description') except KeyError as e: key = e.args[0] msg = 'Invalid configuration: missing field %r.' % (key) raise dtu.DTConfigException(msg) if not isinstance(description, (str, NoneType)): msg = 'Description should be a string, not %s.' % type( description).__name__ raise dtu.DTConfigException(msg) if data: msg = 'Spurious fields found: %s' % sorted(data) raise dtu.DTConfigException(msg) parameters = load_configuration_parameters(parameters) subscriptions = load_configuration_subscriptions(subscriptions) contracts = load_configuration_contracts(contracts) publishers = load_configuration_publishers(publishers) return EasyNodeConfig(filename=realpath, parameters=parameters, contracts=contracts, subscriptions=subscriptions, publishers=publishers, package_name=None, description=description, node_type_name=None) except dtu.DTConfigException as e: msg = 'Invalid configuration at %s: ' % realpath dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True)
def go(self): extra = self.options.get_extra() if not extra: msg = 'Please specify a log.' raise dtu.DTUserError(msg) else: query = extra db_cloud = self.get_easy_logs_db() try: logs = db_cloud.query(query) except dtu.DTNoMatches as e: msg = 'Could not find the logs matching the query.' dtu.raise_wrapped(dtu.DTUserError, e, msg, compact=True) for id_log, log in logs.items(): download_if_necessary(log)
def check(self): try: path = dtu.get_machines_files_path() except dtu.DTConfigException as e: msg = 'Could not get path to machines.' dtu.raise_wrapped(CheckError, e, msg) if not os.path.exists(path): msg = 'Machines file does not exist.' l = 'File does not exist: %s ' % path raise CheckFailed(msg, l) hostname = socket.gethostname() contents = open(path).read() if not '"%s"' % hostname in contents: msg = 'This robot, "%s" is not mentioned in the machines file.' % hostname raise CheckFailed(msg)
def _parse_regression_test_check(line): line = line.strip() delim = ' ' tokens = line.split(delim) if len(tokens) != 3: msg = 'I expect exactly 3 tokens with delimiter %s.\nLine: "%s"\nTokens: %s' % ( delim, line, tokens) raise dtu.DTConfigException(msg) try: ref1 = parse_reference(tokens[0]) binary = parse_binary(tokens[1]) ref2 = parse_reference(tokens[2]) evaluable = BinaryEval(ref1, binary, ref2) except RTParseError as e: msg = 'Cannot parse string "%s".' % line dtu.raise_wrapped(RTParseError, e, msg, compact=True) return Wrapper(evaluable)
def message_class_from_string(s): if not '/' in s: msg = '' msg += 'Invalid message name "%s".\n' % s msg += 'I expected that the name of the message is in the format "PACKAGE/MSG".\n ' msg += 'E.g. "sensor_msgs/Joy" or "duckietown_msgs/BoolStamped".' raise dtu.DTConfigException(msg) # e.g. "std_msgs/Header" i = s.index('/') package = s[:i] name = s[i + 1:] symbol = '%s.msg.%s' % (package, name) try: msgclass = dtu.import_name(symbol) return msgclass except ValueError as e: msg = 'Cannot import type for message "%s" (%s).' % (s, symbol) dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True)
def camera_info_from_yaml(calib_data): try: cam_info = CameraInfo() cam_info.width = calib_data['image_width'] cam_info.height = calib_data['image_height'] # cam_info.K = np.matrix(calib_data['camera_matrix']['data']).reshape((3,3)) # cam_info.D = np.matrix(calib_data['distortion_coefficients']['data']).reshape((1,5)) # cam_info.R = np.matrix(calib_data['rectification_matrix']['data']).reshape((3,3)) # cam_info.P = np.matrix(calib_data['projection_matrix']['data']).reshape((3,4)) cam_info.K = calib_data['camera_matrix']['data'] cam_info.D = calib_data['distortion_coefficients']['data'] cam_info.R = calib_data['rectification_matrix']['data'] cam_info.P = calib_data['projection_matrix']['data'] cam_info.distortion_model = calib_data['distortion_model'] return cam_info except Exception as e: msg = 'Could not interpret data:' msg += '\n\n' + dtu.indent(yaml.dump(calib_data), ' ') dtu.raise_wrapped(InvalidCameraInfo, e, msg)
def process_log(self, bag_in, bag_out): algo_db = get_easy_algo_db() line_detector = algo_db.create_instance(FAMILY_LINE_DETECTOR, self.line_detector) image_prep = algo_db.create_instance('image_prep', self.image_prep) vehicle = dtu.which_robot(bag_in) topic = '/%s/camera_node/image/compressed' % vehicle context = FakeContext() transform = None frame = 0 for compressed_img_msg in dtu.d8n_bag_read_with_progress( bag_in, topic): with context.phase('decoding'): try: image_cv = dtu.bgr_from_jpg(compressed_img_msg.data) except ValueError as e: msg = 'Could not decode image: %s' % e dtu.raise_wrapped(ValueError, e, msg) segment_list = image_prep.process(context, image_cv, line_detector, transform) rendered = vs_fancy_display(image_prep.image_cv, segment_list) rendered = dtu.d8_image_zoom_linear(rendered, 2) log_name = 'log_name' time = 12 rendered = dtu.add_duckietown_header(rendered, log_name, time, frame) out = dtu.d8n_image_msg_from_cv_image( rendered, "bgr8", same_timestamp_as=compressed_img_msg) # Write to the bag bag_out.write('processed', out) # out = d8n_image_msg_from_cv_image(image_cv, "bgr8", same_timestamp_as=compressed_img_msg) bag_out.write('image', compressed_img_msg) frame += 1
def __init__(self, logs, processors=[], analyzers=[], checks=[], topic_videos=[], topic_images=[]): self.logs = logs self.processors = [] for p in processors: p = copy.deepcopy(p) processor = p.pop('processor') prefix_in = p.pop('prefix_in', '') prefix_out = p.pop('prefix_out', '') if p: msg = 'Extra keys: %s' % p raise ValueError(msg) p2 = ProcessorEntry(prefix_in=prefix_in, processor=processor, prefix_out=prefix_out) self.processors.append(p2) self.analyzers = analyzers self.topic_videos = topic_videos self.topic_images = topic_images check_isinstance(checks, list) try: self.cwcs = parse_list_of_checks(checks) except RTParseError as e: msg = 'Cannot parse list of checks.' msg += '\n' + dtu.indent(dtu.yaml_dump_pretty(checks), '', 'parsing: ') dtu.raise_wrapped(RTParseError, e, msg, compact=True)
def interpret_config_file(filename): """ Returns a ConfigInfo. """ try: basename = os.path.basename(filename) base = basename.replace(SUFFIX, '') # now we have something like # package-node.config_name.date # or # package-node.config_name if not '.' in base: msg = 'Invalid filename %r.' % filename raise dtu.DTConfigException(msg) tokens = base.split('.') if len(tokens) > 3: msg = 'Too many periods/tokens (tokens=%s)' % tokens raise dtu.DTConfigException(msg) if len(tokens) <= 2: # package-node.config_name package_node = tokens[0] if not '-' in package_node: msg = 'Expected a "-" in "%s".' % package_node raise dtu.DTConfigException(msg) i = package_node.index('-') package_name = package_node[:i] node_name = package_node[i + 1:] config_name = tokens[1] if len(tokens) == 3: # package-node.config_name.date date_effective = tokens[2] else: date_effective = '20170101' from dateutil.parser import parse try: date_effective = parse(date_effective) except: msg = 'Cannot interpret "%s" as a date.' % date_effective raise dtu.DTConfigException(msg) # now read file contents = open(filename).read() try: try: data = yaml.load(contents) except YAMLError as e: dtu.raise_wrapped(dtu.DTConfigException, e, 'Invalid YAML', compact=True) if not isinstance(data, dict): msg = 'Expected a dictionary inside.' raise dtu.DTConfigException(msg) for field in ['description', 'values']: if not field in data: msg = 'Missing field "%s".' % field raise dtu.DTConfigException(msg) description = data.pop('description') if not isinstance(description, str): msg = 'I expected that "description" is a string, obtained %r.' % description raise dtu.DTConfigException(msg) extends = data.pop('extends', []) if not isinstance(extends, list): msg = 'I expected that "extends" is a list, obtained %r.' % extends raise dtu.DTConfigException(msg) values = data.pop('values') if not isinstance(values, dict): msg = 'I expected that "values" is a dictionary, obtained %s.' % type( values) raise dtu.DTConfigException(msg) # Freeze the data extends = tuple(extends) values = frozendict(values) except dtu.DTConfigException as e: msg = 'Could not interpret the contents of the file\n' msg += ' %s\n' % dtu.friendly_path(filename) msg += 'Contents:\n' + dtu.indent(contents, ' > ') dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True) return ConfigInfo( filename=filename, package_name=package_name, node_name=node_name, config_name=config_name, date_effective=date_effective, extends=extends, description=description, values=values, # not decided valid=None, error_if_invalid=None) except dtu.DTConfigException as e: msg = 'Invalid file %s' % dtu.friendly_path(filename) dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True)
from .constant import Result from .geolocation import get_geolocation_data mongo_db = 'wtd01' mongo_collection = 'uploads' try: import pymongo # @UnresolvedImport @UnusedImport except ImportError as e: msg = 'pymongo not installed.' msg += '\n\nTry the following:' msg += '\n\n pip install --user pymongo' dtu.raise_wrapped(Exception, e, msg) def get_connection_string(): username = '******' password = '******' s = ("mongodb://<USERNAME>:<PASSWORD>@dt0-shard-00-00-npkyt.mongodb.net:27017," "dt0-shard-00-01-npkyt.mongodb.net:27017,dt0-shard-00-02-npkyt.mongodb.net" ":27017/test?ssl=true&replicaSet=dt0-shard-0&authSource=admin") s = s.replace("<PASSWORD>", password) s = s.replace("<USERNAME>", username) return s def get_local_keys(): username = getpass.getuser() hostname = socket.gethostname()
from duckietown_utils import on_duckiebot, on_laptop, on_circle from what_the_duck import what_the_duck_version from .constant import Result from .geolocation import get_geolocation_data mongo_db = 'wtd01' mongo_collection = 'uploads' try: import pymongo # @UnresolvedImport @UnusedImport except ImportError as e: msg = 'pymongo not installed.' msg += '\n\nTry the following:' msg += '\n\n pip install --user pymongo' raise_wrapped(Exception, e, msg) def get_connection_string(): username = '******' password = '******' s = ( "mongodb://<USERNAME>:<PASSWORD>@dt0-shard-00-00-npkyt.mongodb.net:27017," "dt0-shard-00-01-npkyt.mongodb.net:27017,dt0-shard-00-02-npkyt.mongodb.net" ":27017/test?ssl=true&replicaSet=dt0-shard-0&authSource=admin") s = s.replace("<PASSWORD>", password) s = s.replace("<USERNAME>", username) return s