def correctImages(self, ai, imageset, gtimageset, error_threshold): error = [] for i, image in enumerate(imageset): #print(image.shape) ai.calculateTransform(image,True) logger.info('health: %s' % ai.health) transimage = ai.applyTransform(image) testimgf = "testimage%d.jpg" % i cv2.imwrite(testimgf,transimage) testimg = cv2.imread(testimgf) # uncorrected is > 500 e = L2_image_distance(testimg, gtimageset[i]) if e > error_threshold: logger.error("Correction seemed to fail for image %s" % i) error.append(e) return error
def examine_dataset(dirname, out): logger.info(dirname) dirname = expand_environment(dirname) jpgs = locate_files(dirname, "*.jpg") mats = locate_files(dirname, "*.mat") logger.debug("I found %d jpgs and %d mats" % (len(jpgs), len(mats))) if len(jpgs) == 0: msg = "Not enough jpgs." 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 = 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) config_dir = "${DUCKIETOWN_ROOT}/catkin_ws/src/duckietown/config/baseline/line_detector/line_detector_node/" config_dir = expand_environment(config_dir) configurations = locate_files(config_dir, "*.yaml") # 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) from duckietown_utils.instantiate_utils import instantiate def LineDetectorClass(): return instantiate(detector[0], detector[1]) s = run_detection( transform, j, out, shape=shape, interpolation=interpolation, name=name, LineDetectorClass=LineDetectorClass, ) summaries.append(s) together = make_images_grid(summaries, cols=1, pad=10, bgcolor=[0.5, 0.5, 0.5]) cv2.imwrite(fn, zoom_image(together, 4)) # ipython_if_guy() 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" # ipython_if_guy() print "finished mats: " + dirname return overall_results
def examine_dataset(dirname, out): logger.info(dirname) dirname = expand_all(dirname) jpgs = locate_files(dirname, '*.jpg') mats = 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 = 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 = 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 = expand_all(config_dir) configurations = 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 instantiate(detector[0], detector[1]) s = run_detection(transform, j, out, shape=shape, interpolation=interpolation, name=name, LineDetectorClass=LineDetectorClass) summaries.append(s) together = make_images_grid(summaries, cols=1, pad=10, bgcolor=[.5, .5, .5]) cv2.imwrite(fn, 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