def get_images(): found = locate_files(get_duckiefleet_root(), '*.jpg') basename2filename = dict((os.path.basename(_), _) for _ in found) if not nopic in basename2filename: msg = 'I expect a file %r to represent missing pictures.' % nopic raise DTConfigException(msg) return basename2filename
def define_jobs_context(self, context): dirname = self.options.dirname filenames = dtu.locate_files(dirname, '*.jpg') if len(filenames) == 0: msg = 'Could not find any file' raise Exception(msg) options = { 'L1': dict(phi=make_smaller, distance=L1), 'L2': dict(phi=make_smaller, distance=L2), } filenames = [f for f in filenames if '-0' in f] filenames = sorted(filenames) print "\n".join(filenames) for id_option, params in options.items(): images = [dtu.image_cv_from_jpg_fn(f) for f in filenames] c = context.child(id_option) out = os.path.join(dirname, 'similarity', id_option, 'similarity') A = c.comp(get_similarity_matrix, images, out=out, **params) c.comp(write_similarity_matrix, A, out + '_final', more=True, images=images)
def load_configuration_for_nodes_in_package(package_name): """ returns dict node_name -> config """ suffix = '.easy_node.yaml' package_dir = dtu.get_ros_package_path(package_name) configs = dtu.locate_files(package_dir, '*' + suffix) res = {} for c in configs: node_name = os.path.basename(c).replace(suffix, '') res[node_name] = load_configuration_package_node( package_name, node_name) return res
def search_all_configuration_files(): sources = [] # We look in $DUCKIETOWN_ROOT/catkin_ws/src sources.append(dtu.get_catkin_ws_src()) # then we look in $DUCKIETOWN_FLEET sources.append(dtu.get_duckiefleet_root()) configs = [] pattern = '*' + SUFFIX dtu.logger.info('Looking for %s files.' % pattern) for s in sources: fs = dtu.locate_files(s, pattern) dtu.logger.info('%4d files in %s' % (len(fs), s)) configs.extend(fs) # logger.debug('I found:\n' + "\n".join(configs)) return configs
def load_configuration_package_node(package_name, node_type_name): path = dtu.get_ros_package_path(package_name) look_for = '%s.easy_node.yaml' % node_type_name found = dtu.locate_files(path, look_for) if not found: msg = 'Could not find EasyNode configuration %r.' % look_for raise dtu.DTConfigException(msg) # XXX fn = found[0] contents = open(fn).read() c = load_configuration(fn, contents) c = c._replace(package_name=package_name) c = c._replace(node_type_name=node_type_name) # Add the common parameters if node_type_name != 'easy_node': c0 = load_configuration_baseline() c = merge_configuration(c0, c) return c
def anti_instagram_annotations_test(dirname, out_base): base = dtu.expand_all(dirname) if not os.path.exists(base): msg = 'Could not find directory %s' % base raise Exception(msg) dirs = dtu.locate_files(base, '*.iids1', alsodirs=True) directory_results = {} overall_results = [] if not dirs: raise ValueError('No IIDS1 directories found') for d in dirs: out = os.path.join(out_base, os.path.basename(d) + '.v') if not os.path.exists(out): os.makedirs(out) results = examine_dataset(d, out) overall_results = merge_comparison_results(results, overall_results) directory_results[d] = results db = shelve.open('tests_results', flag='n') db['directory_results'] = directory_results db['overall_results'] = overall_results db.close() logger.info( "overall average error: %f" % (overall_results['total_error'] / overall_results['total_pixels'])) logger.info("overall regions checked: %f" % (overall_results['total_regions'])) for t in overall_results['v_vals'].keys(): logger.info("region %f: RGB %f,%f,%f, HSV %f,%f,%f" % (t, np.mean(overall_results['r_vals'][t]), np.mean(overall_results['g_vals'][t]), np.mean(overall_results['b_vals'][t]), np.mean(overall_results['h_vals'][t]), np.mean(overall_results['s_vals'][t]), np.mean(overall_results['v_vals'][t])))
def get_all_python_files(self): python_files = locate_files(self.dirname, '*.py') return python_files
def examine_dataset(dirname, out): logger.info(dirname) dirname = dtu.expand_all(dirname) jpgs = dtu.locate_files(dirname, '*.jpg') mats = dtu.locate_files(dirname, '*.mat') logger.debug('I found %d JPGs and %d .mat.' % (len(jpgs), len(mats))) if len(jpgs) == 0: msg = 'Not JPGs found in %r.' % dirname raise ValueError(msg) # if len(mats) == 0: # msg = 'Not enough mats.' # raise ValueError(msg) first_jpg = sorted(jpgs)[0] logger.debug('Using jpg %r to learn transformation.' % first_jpg) first_jpg_image = dtu.image_cv_from_jpg_fn(first_jpg) success, health, parameters = calculate_transform(first_jpg_image) s = "" s += 'success: %s\n' % str(success) s += 'health: %s\n' % str(health) s += 'parameters: %s\n' % str(parameters) w = os.path.join(out, 'learned_transform.txt') with open(w, 'w') as f: f.write(s) logger.info(s) transform = ScaleAndShift(**parameters) duckietown_package_dir = dtu.get_ros_package_path('duckietown') config_dir = os.path.join( duckietown_package_dir, 'config/baseline/line_detector/line_detector_node') if not os.path.exists(config_dir): msg = 'Could not find configuration dir %s' % config_dir raise Exception(msg) config_dir = dtu.expand_all(config_dir) configurations = dtu.locate_files(config_dir, '*.yaml') if not configurations: msg = 'Could not find any configuration file in %s.' % config_dir raise Exception(msg) #logger.info('configurations: %r' % configurations) for j in jpgs: summaries = [] shape = (200, 160) interpolation = cv2.INTER_NEAREST bn = os.path.splitext(os.path.basename(j))[0] fn = os.path.join(out, '%s.all.png' % (bn)) if os.path.exists(fn): logger.debug('Skipping because file exists: %r' % fn) else: for c in configurations: logger.info('Trying %r' % c) name = os.path.splitext(os.path.basename(c))[0] if name in ['oreo', 'myrtle', 'bad_lighting', '226-night']: continue with open(c) as f: stuff = yaml.load(f) if not 'detector' in stuff: msg = 'Cannot find "detector" section in %r' % c raise ValueError(msg) detector = stuff['detector'] logger.info(detector) if not isinstance(detector, list) and len(detector) == 2: raise ValueError(detector) def LineDetectorClass(): return dtu.instantiate(detector[0], detector[1]) s = run_detection(transform, j, out, shape=shape, interpolation=interpolation, name=name, LineDetectorClass=LineDetectorClass) summaries.append(s) together = dtu.make_images_grid(summaries, cols=1, pad=10) cv2.imwrite(fn, dtu.zoom_image(together, 4)) overall_results = [] comparison_results = {} for m in mats: logger.debug(m) jpg = os.path.splitext(m)[0] + '.jpg' if not os.path.exists(jpg): msg = 'JPG %r for mat %r does not exist' % (jpg, m) logger.error(msg) else: frame_results = test_pair(transform, jpg, m, out) comparison_results[m] = frame_results overall_results = merge_comparison_results(comparison_results[m], overall_results) print "comparison_results[m]=frame_results" print "finished mats: " + dirname return overall_results