Пример #1
0
def single_image_histograms():
    p = dtu.require_resource('frame0002.jpg')

    image_cv = dtu.image_cv_from_jpg_fn(p)

    res = go(image_cv)
    outd = dtu.get_output_dir_for_test()
    dtu.write_bgr_images_as_jpgs(res, outd)
Пример #2
0
def compare_faster2():
    variables = collections.OrderedDict()
    variables['alpha'] = dict(min=-180,
                              max=180,
                              description="angle",
                              resolution=5,
                              units='deg',
                              units_display='deg')
    variables['r'] = dict(min=3,
                          max=5,
                          description="distance",
                          resolution=0.2,
                          units='m',
                          units_display='cm')
    # this will fail if precision is float32
    gh = GridHelper(variables, precision='float64')
    val_fast = gh.create_new()
    val_fast.fill(0)
    val_slow = gh.create_new()
    val_slow.fill(0)

    od = dtu.get_output_dir_for_test()

    F = 1
    alpha0 = 177
    # r0 = 4
    r0 = 4.1
    w0 = 1
    value = dict(alpha=alpha0, r=r0)
    gh.add_vote(val_slow, value, w0, F)

    values = np.zeros((2, 1))
    values[0, 0] = alpha0
    values[1, 0] = r0
    weights = np.zeros(1)
    weights[0] = w0
    gh.add_vote_faster(val_fast, values, weights, F)

    print('sum slow: %s' % np.sum(val_slow))
    print('sum fast: %s' % np.sum(val_fast))
    d = grid_helper_plot(gh, val_slow)
    fn = os.path.join(od, 'compare_faster_slow.jpg')
    dtu.write_data_to_file(d.get_png(), fn)

    d = grid_helper_plot(gh, val_fast)
    fn = os.path.join(od, 'compare_faster_fast.jpg')
    dtu.write_data_to_file(d.get_png(), fn)

    f = lambda x: ' %5f' % x if x else '    '
    print(dtu.indent(array_as_string(val_fast, f), 'val_fast '))
    print(dtu.indent(array_as_string(val_slow, f), 'val_slow '))

    assert_almost_equal(val_fast, val_slow)
Пример #3
0
def run_one(cmd):
    v = False
    cwd = dtu.get_output_dir_for_test()
    if not os.path.exists(cwd):
        dtu.mkdirs_thread_safe(cwd)
    dtu.write_data_to_file('config echo 1', os.path.join(cwd, '.compmake.rc'))
    try:
        dtu.system_cmd_result(cwd,
                              cmd,
                              display_stdout=v,
                              display_stderr=v,
                              raise_on_error=True)
    finally:
        pass
Пример #4
0
def run(which, expect):
    v = False
    cwd = dtu.get_output_dir_for_test()
    if not os.path.exists(cwd):
        dtu.mkdirs_thread_safe(cwd)

    try:
        cmd = ['rosrun', 'easy_regression', 'run',
               '--expect', expect,
               '--test', which,
               '-o', '.',
               '-c', 'rmake']
        dtu.system_cmd_result(cwd, cmd,
              display_stdout=v,
              display_stderr=v,
              raise_on_error=True)
    finally:
        if False:
            shutil.rmtree(cwd)
Пример #5
0
def single_image1():
    p = dtu.require_resource('frame0002.jpg')
    image_cv = dtu.image_cv_from_jpg_fn(p)

    line_detector_name = 'baseline'
    image_prep_name = 'baseline'
    lane_filter_name = 'baseline'
    anti_instagram_name = 'baseline'
    robot_name = dtu.DuckietownConstants.ROBOT_NAME_FOR_TESTS
    gp = GroundProjection(robot_name)

    res, _stats = run_pipeline(image_cv, gp,
                         line_detector_name=line_detector_name,
                         image_prep_name=image_prep_name,
                         lane_filter_name=lane_filter_name,
                         anti_instagram_name=anti_instagram_name,
                         all_details=False, ground_truth=None)

    outd = dtu.get_output_dir_for_test()
    dtu.write_jpgs_to_dir(res, outd)
Пример #6
0
def anti_instagram_annotations_test():
    out_base = dtu.get_output_dir_for_test()

    #     out_base = 'anti_instagram_annotations_test'

    zipname = dtu.require_resource('ii-datasets.zip')
    dirname = os.path.dirname(zipname)
    base = os.path.join(dirname, 'ii-datasets')

    cmd = ['unzip', '-o', zipname]
    cwd = dirname
    dtu.system_cmd_result(cwd,
                          cmd,
                          display_stdout=False,
                          display_stderr=False,
                          raise_on_error=True)

    if not os.path.exists(base):
        msg = 'Could not find expected unzipped directory:\n   %s' % base
        raise Exception(msg)

    anti_instagram_annotations_test(base, out_base)
Пример #7
0
def grid_visualization():
    variables = collections.OrderedDict()
    variables['alpha'] = dict(min=-np.pi / 2,
                              max=np.pi / 2,
                              description="angle",
                              resolution=np.deg2rad(10),
                              units='rad',
                              units_display='deg')
    variables['r'] = dict(min=3,
                          max=5,
                          description="distance",
                          resolution=0.1,
                          units='m',
                          units_display='cm')
    gh = GridHelper(variables)
    val = gh.create_new()
    val.fill(0)
    val += np.random.randn(*val.shape)

    val[1, 2] = 0
    d = grid_helper_plot(gh, val)
    od = dtu.get_output_dir_for_test()
    fn = os.path.join(od, 'grid_visualization.jpg')
    dtu.write_data_to_file(d.get_png(), fn)
Пример #8
0
def compare_faster():
    variables = collections.OrderedDict()
    variables['alpha'] = dict(min=-180,
                              max=180,
                              description="angle",
                              resolution=5,
                              units='deg',
                              units_display='deg')
    variables['r'] = dict(min=3,
                          max=5,
                          description="distance",
                          resolution=0.1,
                          units='m',
                          units_display='cm')
    # this will fail if precision is float32
    gh = GridHelper(variables, precision='float64')
    val_fast = gh.create_new()
    val_fast.fill(0)
    val_slow = gh.create_new()
    val_slow.fill(0)

    od = dtu.get_output_dir_for_test()

    F = 1

    alpha0 = 7
    # r0 = 4
    r0 = 4.1
    w0 = 1.
    value = dict(alpha=alpha0, r=r0)
    gh.add_vote(val_slow, value, w0, F)

    assert_equal(np.sum(val_slow > 0), 9)

    values = np.zeros((2, 1))
    values[0, 0] = alpha0
    values[1, 0] = r0
    weights = np.zeros(1)
    weights[0] = w0
    gh.add_vote_faster(val_fast, values, weights, F)

    assert_equal(np.sum(val_fast > 0), 9)

    d = grid_helper_plot(gh, val_slow)
    fn = os.path.join(od, 'compare_faster_slow.jpg')
    dtu.write_data_to_file(d.get_png(), fn)

    d = grid_helper_plot(gh, val_fast)
    fn = os.path.join(od, 'compare_faster_fast.jpg')
    dtu.write_data_to_file(d.get_png(), fn)

    D = val_fast - val_slow
    diff = np.max(np.abs(D))
    print('diff: %r' % diff)
    if diff > 1e-8:
        print(dtu.indent(array_as_string_sign(val_fast), 'val_fast '))
        print(dtu.indent(array_as_string_sign(val_slow), 'val_slow '))
        print(dtu.indent(array_as_string_sign(D), 'Diff '))
        print('non zero val_fast: %s' % val_fast[val_fast > 0])
        print('non zero val_slow: %s' % val_slow[val_slow > 0])

    assert_almost_equal(val_fast, val_slow)
Пример #9
0
def _cwd():
    cwd = dtu.get_output_dir_for_test()
    if not os.path.exists(cwd):
        dtu.mkdirs_thread_safe(cwd)
    return cwd
Пример #10
0
def dirn(misc):
    outd = dtu.get_output_dir_for_test()
    return os.path.join(outd, misc)
Пример #11
0
def voting_kernel1():
    resolution = 10.0
    variables = collections.OrderedDict()
    variables['x'] = dict(min=100,
                          max=500,
                          description="x",
                          resolution=resolution,
                          units='cm',
                          units_display='cm')
    variables['y'] = dict(min=100,
                          max=500,
                          description="y",
                          resolution=resolution,
                          units='cm',
                          units_display='cm')
    gh = GridHelper(variables)
    votes = gh.create_new()
    votes.fill(0)

    points = []
    estimated = []
    estimated_weighted = []

    F = 1

    N = 25
    errors_x = []
    errors_x_w = []
    for i in range(N):
        dx = resolution / N
        Dy = 70
        Dx = 70
        u = (i / 5)
        v = i % 5

        x = 125 + dx * i + Dx * u
        y = 127 + Dy * v
        p = dict(x=x, y=y)
        points.append(p)
        weight = 1
        gh.add_vote(votes, p, weight=weight, F=F)

        tmp = gh.create_new()
        tmp.fill(0)

        gh.add_vote(tmp, p, weight=weight, F=F)
        assert_almost_equal(tmp.sum(), weight)
        estimate = gh.get_max(tmp)
        estimated.append(estimate)
        estimate_weigthed = gh.get_max_weighted(tmp, F=F)
        estimated_weighted.append(estimate_weigthed)

        errors_x.append(p['x'] - estimate['x'])
        errors_x_w.append(p['x'] - estimate_weigthed['x'])

    errors_x = np.array(errors_x)
    errors_x_w = np.array(errors_x_w)
    dtu.logger.debug('errors_x: %s' % errors_x)
    dtu.logger.debug('mean: %s' % np.abs(errors_x).mean())
    dtu.logger.debug('errors_x_w: %s' % errors_x_w)
    dtu.logger.debug('mean: %s' % np.abs(errors_x_w).mean())

    assert (errors_x.max() <= +resolution / 2)
    assert (errors_x.min() >= -resolution / 2)
    assert (np.abs(errors_x_w).max() <= resolution / 10)

    a = dtu.CreateImageFromPylab(dpi=1000)
    with a as pylab:
        grid_helper_plot_field(gh, votes, pylab)
        pylab.axis('equal')
        grid_helper_annotate_axes(gh, pylab)
        for p in points:
            grid_helper_mark_point(gh, pylab, p, color='blue', markersize=4)
        for e in estimated:
            grid_helper_mark_point(gh, pylab, e, color='red', markersize=3)
        for e in estimated_weighted:
            grid_helper_mark_point(gh, pylab, e, color='green', markersize=3)

    b = dtu.CreateImageFromPylab(dpi=1000)
    with b as pylab:
        x = np.array([_['x'] for _ in points])
        xe = np.array([_['x'] for _ in estimated])
        xew = np.array([_['x'] for _ in estimated_weighted])

        xe = xe - x
        xew = xew - x
        x = x * 0

        pylab.plot(x, '.', label='x')
        pylab.plot(xe, '.', label='x estimated')
        pylab.plot(xew, '.', label='x estimated weighted')
        pylab.legend()

    od = dtu.get_output_dir_for_test()
    fn = os.path.join(od, 'voting_kernel1.jpg')
    dtu.write_data_to_file(a.get_png(), fn)
    fn = os.path.join(od, 'errors.jpg')
    dtu.write_data_to_file(b.get_png(), fn)