示例#1
0
def hello_world(i):
    templates = get_templates
    strokeNumber = i
    cs = strokes[i - 1]
    strk = Stroke(x=cs['x'], y=cs['y'], t=cs['t'])
    clean_segment_indices, segtypes = segment_stroke(strk)
    best_match = classify_stroke(strk, templates)
    return best_match
示例#2
0
def normalize_segpoints(strokes):
    """
    param stroke : a Stroke object with N x,y,t data points

    return :
        (template_x, template_y): a tuple of arrays representing the normalized X
        and Y coordinates, ordered by time, of the points to be
        used in the MHD calculation to compare against the given templates

        Coordinates are normalized so that points span between 0 and 1

        Relevant points would be the full set of stroke segment endpoints
        and the curve segment midpoints

    """
    #NORMALIZE THE STROKE POINTS

    #GET THE SET OF POINTS TO BE USED IN THE MHD CALCULATION

    template_x = []
    template_y = []
    for stroke in strokes:
        segpoints, segtypes = segment_stroke(stroke)
        assert len(segpoints) == len(segtypes) + 1
        for i in range(len(segtypes)):
            point = segpoints[i]
            template_x.append(stroke.x[point])
            template_y.append(stroke.y[point])
            if segtypes[i] == 1:
                midpoint = (segpoints[i] + segpoints[i + 1]) // 2
                template_x.append(stroke.x[midpoint])
                template_y.append(stroke.y[midpoint])
        last_point = segpoints[-1]
        template_x.append(stroke.x[last_point])
        template_y.append(stroke.y[last_point])

        num_arcs = sum([t for t in segtypes if t == 1])
    # assert len(template_x) == len(segpoints) + num_arcs

    min_x, max_x = min(template_x), max(template_x)
    min_y, max_y = min(template_y), max(template_y)
    diff_x = max_x - min_x
    diff_y = max_y - min_y
    template_x = [(x - min_x) / diff_x for x in template_x]
    template_y = [(y - min_y) / diff_y for y in template_y]

    print("\'x\':", template_x, ",")
    print("\'y\':", template_y)
    return template_x, template_y
def normalize_segpoints(stroke):
    """
    param stroke : a Stroke object with N x,y,t data points

    return :
        (template_x, template_y): a tuple of arrays representing the normalized X
        and Y coordinates, ordered by time, of the points to be
        used in the MHD calculation to compare against the given templates

        Coordinates are normalized so that points span between 0 and 1

        Relevant points would be the full set of stroke segment endpoints
        and the curve segment midpoints

    """
    #NORMALIZE THE STROKE POINTS
    segpoints, segtypes = segment_stroke(stroke)
    #print("PTS", segpoints)
    #print("TYPES", segtypes)
    x = []
    y = []
    
    for j in range(len(segpoints)-1):
        i = segpoints[j]
        x.append(stroke.x[i])
        y.append(stroke.y[i])
        if segtypes[j] == 1:
            start = i
            end = segpoints[j+1]
            mid = int((start+end)/2)
            x.append(stroke.x[mid])
            y.append(stroke.y[mid])
    last = segpoints[-1]
    x.append(stroke.x[last])
    y.append(stroke.y[last])
    max_x = max(x)
    max_y = max(y)
    min_x = min(x)
    min_y = min(y)
    x_norm = [(i-min_x)/(max_x-min_x) for i in x]
    y_norm = [(i-min_y)/(max_y-min_y) for i in y]

    #GET THE SET OF POINTS TO BE USED IN THE MHD CALCULATION

    return (x_norm,y_norm)
示例#4
0
                    9: "THREE FEATHERS",
                    10: "CHEF HAT",
                    11: "DOME",
                    12: "MUSIC NOTE"
                }

if len(sys.argv) != 2:
    raise ValueError('Error! Give stroke number after filename in command. \n e.g. python eval_stroke.py 2')

i = int(sys.argv[1])

if i <= len(strokes) and i > 0 or i == -1:
    strokeNumber = i
    cs = strokes[i-1]
    strk = Stroke(x=cs['x'], y=cs['y'], t=cs['t'])
    clean_segment_indices, segtypes = segment_stroke(strk)
    best_match = classify_stroke(strk, templates)

    print(f"{bcolors.BOLD}", "STROKE NUMBER:", strokeNumber, f"{bcolors.ENDC}")
    print(f"{bcolors.BOLD}", "STROKE CLASS:", stroke_names[strokeNumber], f"{bcolors.ENDC}")
    if best_match == stroke_names[strokeNumber]:
        print(f"{bcolors.BOLD}", "YOUR CLASSIFICATION:", f"{bcolors.OKGREEN}", best_match, f"{bcolors.ENDC}", f"{bcolors.ENDC}")
    else:
        print(f"{bcolors.BOLD}", "YOUR CLASSIFICATION:", f"{bcolors.FAIL}", best_match, f"{bcolors.ENDC}", f"{bcolors.ENDC}")

    plot_segmentation(strk, clean_segment_indices, segtypes)
    plt.figure(1).canvas.set_window_title('MIT 6.835: Stroke Segmentation')
    plt.show()
else:
    msg = 'Stroke index out of range. Give a number between 1 and ' + str(strokes.size)
    raise ValueError(msg)