Exemplo n.º 1
0
def test_parse_graph():
    questions = geoserver_interface.download_questions(973).values()
    for question in questions:
        image_segment_parse = parse_image_segments(open_image(question.diagram_path))
        primitive_parse = parse_primitives(image_segment_parse)
        selected_primitive_parse = select_primitives(primitive_parse)
        core_parse = parse_core(selected_primitive_parse)
        graph_parse = parse_graph(core_parse)

        print("Confident information in the diagram:")
        for variable_node in parse_confident_atoms(graph_parse):
            print variable_node

        core_parse.display_points()
        lines = get_all_instances(graph_parse, 'line')
        circles = get_all_instances(graph_parse, 'circle')
        arcs = get_all_instances(graph_parse, 'arc')
        angles = get_all_instances(graph_parse, 'angle')
        print("Displaying lines...")
        for key, line in lines.iteritems():
            graph_parse.display_instances([line])
        print("Displaying circles...")
        for key, circle in circles.iteritems():
            graph_parse.display_instances([circle])
        print("Displaying arcs...")
        for key, arc in arcs.iteritems():
            graph_parse.display_instances([arc])
        print("Displaying angles...")
        for key, angle in angles.iteritems():
            graph_parse.display_instances([angle])
Exemplo n.º 2
0
def test_parse_graph():
    questions = geoserver_interface.download_questions(973).values()
    for question in questions:
        image_segment_parse = parse_image_segments(
            open_image(question.diagram_path))
        primitive_parse = parse_primitives(image_segment_parse)
        selected_primitive_parse = select_primitives(primitive_parse)
        core_parse = parse_core(selected_primitive_parse)
        graph_parse = parse_graph(core_parse)

        print("Confident information in the diagram:")
        for variable_node in parse_confident_atoms(graph_parse):
            print variable_node

        core_parse.display_points()
        lines = get_all_instances(graph_parse, 'line')
        circles = get_all_instances(graph_parse, 'circle')
        arcs = get_all_instances(graph_parse, 'arc')
        angles = get_all_instances(graph_parse, 'angle')
        print("Displaying lines...")
        for key, line in lines.iteritems():
            graph_parse.display_instances([line])
        print("Displaying circles...")
        for key, circle in circles.iteritems():
            graph_parse.display_instances([circle])
        print("Displaying arcs...")
        for key, arc in arcs.iteritems():
            graph_parse.display_instances([arc])
        print("Displaying angles...")
        for key, angle in angles.iteritems():
            graph_parse.display_instances([angle])
Exemplo n.º 3
0
def extract(image, i):
    # print('----a')
    image_segment_parse, detected_char = ge.parse_image_segments(image)
    primitive_parse = parse_primitives(image_segment_parse)
    selected = select_primitives(primitive_parse)
    core_parse = parse_core(selected)
    # print('----p')
    graph_parse = parse_graph(core_parse)
    # print('----g')
    pairs = []
    points = get_all_instances(graph_parse, 'point')
    # print('----1')
    lines = get_all_instances(graph_parse, 'line')
    # print('----2')
    circles = get_all_instances(graph_parse, 'circle')
    # print('----3')
    angles = get_all_instances(graph_parse, 'angle')
    # print('----4')
    polygon = get_all_instances(graph_parse, 'polygon')
    # pr0int('----5')
    for point in points.keys():
        points[point] = (points[point].x, points[point].y)

    image = core_parse.get_image_points()
    for line in lines.values():
        image = draw_line2(
            image,
            line,
            offset=image_segment_parse.diagram_image_segment.offset)
    # cv2.imwrite('tmp/results/im_lines.png', image)
    # image = cv2.imread(image_path)
    for circle in circles.values():
        image = draw_circle2(
            image,
            circle,
            offset=image_segment_parse.diagram_image_segment.offset)
    cv2.imwrite('tmp/results/{}entity.png'.format(i), image)
    return {
        'id': i,
        'points': points,
        'lines': lines.keys(),
        'circles': circles.keys(),
        'angles': angles.keys(),
        'polygon': polygon.keys(),
        'chars': detected_char
    }
Exemplo n.º 4
0
def _get_all(match_parse, basic_ontology, type_):
    assert isinstance(match_parse, MatchParse)
    general_graph_parse = match_parse.general_graph_parse
    instances = get_all_instances(general_graph_parse, type_.name)
    assert isinstance(instances, dict)
    packs = []
    for key, d in instances.iteritems():
        instance = d['instance']
        constant = Constant(instance, type_)
        formula = Formula(basic_ontology, constant, [])
        variables = {}
        cost = 0
        packs.append(FormulaPack(formula, variables, cost))
    return packs
Exemplo n.º 5
0
def _get_all(match_parse, basic_ontology, type_):
    assert isinstance(match_parse, MatchParse)
    general_graph_parse = match_parse.general_graph_parse
    instances = get_all_instances(general_graph_parse, type_.name)
    assert isinstance(instances, dict)
    packs = []
    for key, d in instances.iteritems():
        instance = d['instance']
        constant = Constant(instance, type_)
        formula = Formula(basic_ontology, constant, [])
        variables = {}
        cost = 0
        packs.append(FormulaPack(formula, variables, cost))
    return packs
Exemplo n.º 6
0
    def display_instances(self, inst_type="line", ids_of_insts=[]):
        all_instances = get_all_instances(self.current_graph_parse, inst_type)
        assert all_instances

        # self.current_graph_parse.display_instances(all_instances)
        # matching chars
        ids_of_insts_matched = []
        for ids_of_inst in ids_of_insts:
            ids_of_inst_matched = itemgetter(*ids_of_inst)(self.current_match)
            if ids_of_inst_matched not in all_instances.keys():
                if inst_type is "line" or inst_type is "angle":
                    ids_of_inst_matched = list(ids_of_inst_matched)
                    ids_of_inst_matched.reverse(
                    )  # line: BA -> AB, angle: ABC -> CBA
                    import ipdb
                    ipdb.set_trace()
                    assert tuple(ids_of_inst_matched) in all_instances.keys()
                    ids_of_insts_matched.append(tuple(ids_of_inst_matched))
                elif inst_type is "polygon":
                    for p in permutations(ids_of_inst_matched):
                        if tuple(p) in all_instances.keys():
                            ids_of_insts_matched.append(tuple(p))
                            break
            else:
                ids_of_insts_matched.append(ids_of_inst_matched)

        try:
            instances_filter = list(
                itemgetter(*ids_of_insts_matched)(
                    all_instances))  # indices_filter: a list of tuple
        except KeyError as e:
            print(e)
        # finally:
        #     print(ids_of_insts_matched)

        self.current_graph_parse.display_instances(
            instances_filter,
            display_label=True,
            match_position=self.current_match_position)
Exemplo n.º 7
0
def _ground_variable(match_parse, variable, references={}):
    assert isinstance(variable, FormulaNode)
    assert isinstance(match_parse, MatchParse)
    return_type = variable.return_type
    graph_parse = match_parse.graph_parse
    core_parse = graph_parse.core_parse
    variable_signature = variable.signature

    if variable_signature.id in signatures:
        # pass What, Which, etc.
        return variable
    elif variable_signature.id in match_parse.graph_parse.core_parse.variable_assignment.keys(
    ):
        # pass point_0, point_1, etc.
        return variable
    elif isinstance(variable_signature,
                    VariableSignature) and variable_signature.is_ref():
        # @v_1, etc.
        return references[variable_signature.name]
    elif return_type == 'number':
        if is_number(variable_signature.name):
            return variable
        elif len(variable_signature.name) == 1:
            # x, y, z, etc. Need to redefine id (id shouldn't be tuple).
            return FormulaNode(
                VariableSignature(variable_signature.name, return_type), [])
        elif len(variable_signature.name
                 ) == 2 and variable_signature.name.isupper():
            new_leaf = FormulaNode(
                VariableSignature(variable.signature.id,
                                  "line",
                                  name=variable.signature.name), [])
            return FormulaNode(signatures['LengthOf'],
                               [_ground_variable(match_parse, new_leaf)])
    elif return_type == 'point':
        if len(variable_signature.name) == 1:
            return match_parse.match_dict[variable_signature.name][0]
        else:
            points = get_all_instances(graph_parse, 'point', True)
            return SetNode(points.values())
    elif return_type == 'line':
        if len(variable_signature.name
               ) == 1 and variable_signature.name in match_parse.match_dict:
            line = match_parse.match_dict[variable_signature.name][0]
            return line
        elif len(variable_signature.name
                 ) == 2 and variable_signature.name.isupper():
            label_a, label_b = variable_signature.name
            point_a = match_parse.match_dict[label_a][0]
            point_b = match_parse.match_dict[label_b][0]
            return FormulaNode(signatures['Line'], [point_a, point_b])
            """
        elif variable_signature.name == 'hypotenuse':
            def func(x):
                l, t = x
                formula = FormulaNode(signatures['IsHypotenuseOf'], (l,t))
                tv = core_parse.evaluate(formula)
                return tv.norm
            lines = get_all_instances(graph_parse, 'line', True).values()
            triangles = get_all_instances(graph_parse, 'triangle', True).values()
            line, triangle = min(itertools.product(lines, triangles), key=func)
            return line
            """

        else:
            lines = get_all_instances(graph_parse, 'line', True)
            return SetNode(lines.values())
    elif return_type == 'circle':
        if len(variable_signature.name) == 1:
            center_label = variable_signature.name
            center = match_parse.match_dict[center_label][0]
            center_idx = int(center.signature.name.split("_")[1])
            return graph_parse.circle_dict[center_idx][0]['variable']
            # radius = match_parse.graph_parse.core_parse.radius_variables[center_idx][0]
        elif variable_signature.name == 'circle':
            circles = get_all_instances(graph_parse, 'circle', True)
            return SetNode(circles.values())
        else:
            raise Exception()
    elif return_type == 'angle':
        # TODO :
        if len(variable_signature.name
               ) == 3 and variable_signature.name.isupper():
            label_a, label_b, label_c = variable_signature.name
            point_a = match_parse.match_dict[label_a][0]
            point_b = match_parse.match_dict[label_b][0]
            point_c = match_parse.match_dict[label_c][0]
            out = FormulaNode(signatures['Angle'], [point_a, point_b, point_c])
            measure = evaluate(FormulaNode(signatures['MeasureOf'], [out]),
                               core_parse.variable_assignment)
            if measure > np.pi:
                out = FormulaNode(signatures['Angle'],
                                  [point_c, point_b, point_a])
            return out
        elif len(variable_signature.name
                 ) == 1 and variable_signature.name.isupper():
            angles = get_all_instances(graph_parse, 'angle', True)
            p = match_parse.match_dict[variable_signature.name][0]
            for formula in angles.values():
                if formula.children[1].signature == p.signature:
                    measure = evaluate(
                        FormulaNode(signatures['MeasureOf'], [formula]),
                        core_parse.variable_assignment)
                    if measure > np.pi:
                        continue
                    return formula

        elif len(variable_signature.name
                 ) == 1 and variable_signature.name.islower():
            return match_parse.match_dict[variable_signature.name][0]
    elif return_type == 'arc':
        if len(variable_signature.name
               ) == 2 and variable_signature.name.isupper():
            point_keys = [
                match_parse.point_key_dict[label]
                for label in variable_signature.name
            ]
            test_arc = get_instances(graph_parse, 'arc', False,
                                     *point_keys).values()[0]
            if MeasureOf(test_arc) > np.pi:
                point_keys = [point_keys[1], point_keys[0]]
            arc = get_instances(graph_parse, 'arc', True,
                                *point_keys).values()[0]
            return arc
        else:
            arcs = get_all_instances(graph_parse, 'arc', True)
            return SetNode(arcs.values())

    elif return_type == 'triangle':
        if variable_signature.name.isupper() and len(
                variable_signature.name) == 3:
            point_keys = [
                match_parse.point_key_dict[label]
                for label in variable_signature.name
            ]
            triangles = get_instances(graph_parse, 'triangle', True,
                                      *point_keys)
            return triangles.values()[0]
        else:
            triangles = get_all_instances(graph_parse, 'triangle', True)
            return SetNode(triangles.values())
    elif return_type == 'quad':
        if variable_signature.name.isupper() and len(
                variable_signature.name) == 4:
            point_keys = [
                match_parse.point_key_dict[label]
                for label in variable_signature.name
            ]
            quads = get_instances(graph_parse, 'quad', True, *point_keys)
            return quads.values()[0]
        else:
            quads = get_all_instances(graph_parse, 'quad', True)
            return SetNode(quads.values())
    elif return_type == 'hexagon':
        if variable_signature.name.isupper() and len(
                variable_signature.name) == 6:
            point_keys = [
                match_parse.point_key_dict[label]
                for label in variable_signature.name
            ]
            hexagons = get_instances(graph_parse, 'hexagon', True, *point_keys)
            return hexagons.values()[0]
        else:
            quads = get_all_instances(graph_parse, 'hexagon', True)
            return SetNode(quads.values())
    elif return_type == 'polygon':
        if variable_signature.name.isupper():
            point_keys = [
                match_parse.point_key_dict[label]
                for label in variable_signature.name
            ]
            polygons = get_instances(graph_parse, 'polygon', True, *point_keys)
            return polygons.values()[0]
        else:
            polygons = get_all_instances(graph_parse, 'polygon', True)
            return SetNode(polygons.values())
    elif return_type == 'twod':
        circles = get_all_instances(graph_parse, 'circle', True)
        polygons = get_all_instances(graph_parse, 'polygon', True)
        return SetNode(polygons.values() + circles.values())
    elif return_type == 'oned':
        lines = get_all_instances(graph_parse, 'line', True)
        arcs = get_all_instances(graph_parse, 'arc', True)
        return SetNode(lines.values() + arcs.values())

    #logging.warning("failed to ground variable: %r" % variable)
    raise Exception()
Exemplo n.º 8
0
def _ground_variable(match_parse, variable, references={}):
    assert isinstance(variable, FormulaNode)
    assert isinstance(match_parse, MatchParse)
    return_type = variable.return_type
    graph_parse = match_parse.graph_parse
    core_parse = graph_parse.core_parse
    variable_signature = variable.signature

    if variable_signature.id in signatures:
        # pass What, Which, etc.
        return variable
    elif variable_signature.id in match_parse.graph_parse.core_parse.variable_assignment.keys():
        # pass point_0, point_1, etc.
        return variable
    elif isinstance(variable_signature, VariableSignature) and variable_signature.is_ref():
        # @v_1, etc.
        return references[variable_signature.name]
    elif return_type == "number":
        if is_number(variable_signature.name):
            return variable
        elif len(variable_signature.name) == 1:
            # x, y, z, etc. Need to redefine id (id shouldn't be tuple).
            return FormulaNode(VariableSignature(variable_signature.name, return_type), [])
        elif len(variable_signature.name) == 2 and variable_signature.name.isupper():
            new_leaf = FormulaNode(VariableSignature(variable.signature.id, "line", name=variable.signature.name), [])
            return FormulaNode(signatures["LengthOf"], [_ground_variable(match_parse, new_leaf)])
        else:
            # ABC: number -> just variable
            return variable
    elif return_type == "point":
        if len(variable_signature.name) == 1:
            return match_parse.match_dict[variable_signature.name][0]
        else:
            points = get_all_instances(graph_parse, "point", True)
            return SetNode(points.values())
    elif return_type == "line":
        if len(variable_signature.name) == 1 and variable_signature.name in match_parse.match_dict:
            line = match_parse.match_dict[variable_signature.name][0]
            return line
        elif len(variable_signature.name) == 2 and variable_signature.name.isupper():
            label_a, label_b = variable_signature.name
            point_a = match_parse.match_dict[label_a][0]
            point_b = match_parse.match_dict[label_b][0]
            return FormulaNode(signatures["Line"], [point_a, point_b])
        else:
            lines = get_all_instances(graph_parse, "line", True)
            return SetNode(lines.values())
    elif return_type == "circle":
        if len(variable_signature.name) == 1:
            center_label = variable_signature.name
            center = match_parse.match_dict[center_label][0]
            center_idx = int(center.signature.name.split("_")[1])
            return graph_parse.circle_dict[center_idx][0]["variable"]
            # radius = match_parse.graph_parse.core_parse.radius_variables[center_idx][0]
        elif variable_signature.name == "circle":
            circles = get_all_instances(graph_parse, "circle", True)
            return SetNode(circles.values())
        else:
            raise Exception()
    elif return_type == "angle":
        # TODO :
        if len(variable_signature.name) == 3 and variable_signature.name.isupper():
            label_a, label_b, label_c = variable_signature.name
            point_a = match_parse.match_dict[label_a][0]
            point_b = match_parse.match_dict[label_b][0]
            point_c = match_parse.match_dict[label_c][0]
            out = FormulaNode(signatures["Angle"], [point_a, point_b, point_c])
            measure = evaluate(FormulaNode(signatures["MeasureOf"], [out]), core_parse.variable_assignment)
            if measure > np.pi:
                out = FormulaNode(signatures["Angle"], [point_c, point_b, point_a])
            return out
        elif len(variable_signature.name) == 1 and variable_signature.name.isupper():
            angles = get_all_instances(graph_parse, "angle", True)
            p = match_parse.match_dict[variable_signature.name][0]
            for formula in angles.values():
                if formula.children[1].signature == p.signature:
                    measure = evaluate(FormulaNode(signatures["MeasureOf"], [formula]), core_parse.variable_assignment)
                    if measure > np.pi:
                        continue
                    return formula
        elif (
            len(variable_signature.name) == 1
            and variable_signature.name.islower()
            and variable_signature.name in match_parse.match_dict
        ):
            return match_parse.match_dict[variable_signature.name][0]
        else:
            angles = get_all_instances(graph_parse, "angle", True)
            return SetNode(angles.values())
    elif return_type == "arc":
        if len(variable_signature.name) == 2 and variable_signature.name.isupper():
            point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name]
            test_arc = get_instances(graph_parse, "arc", False, *point_keys).values()[0]
            if MeasureOf(test_arc) > np.pi:
                point_keys = [point_keys[1], point_keys[0]]
            arc = get_instances(graph_parse, "arc", True, *point_keys).values()[0]
            return arc
        else:
            arcs = get_all_instances(graph_parse, "arc", True)
            return SetNode(arcs.values())

    elif return_type == "triangle":
        if variable_signature.name.isupper() and len(variable_signature.name) == 3:
            point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name]
            triangles = get_instances(graph_parse, "triangle", True, *point_keys)
            return triangles.values()[0]
        else:
            triangles = get_all_instances(graph_parse, "triangle", True)
            return SetNode(triangles.values())
    elif return_type == "quad":
        if variable_signature.name.isupper() and len(variable_signature.name) == 4:
            point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name]
            quads = get_instances(graph_parse, "quad", True, *point_keys)
            return quads.values()[0]
        else:
            quads = get_all_instances(graph_parse, "quad", True)
            return SetNode(quads.values())
    elif return_type == "hexagon":
        if variable_signature.name.isupper() and len(variable_signature.name) == 6:
            point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name]
            hexagons = get_instances(graph_parse, "hexagon", True, *point_keys)
            return hexagons.values()[0]
        else:
            quads = get_all_instances(graph_parse, "hexagon", True)
            return SetNode(quads.values())
    elif return_type == "polygon":
        if variable_signature.name.isupper():
            point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name]
            polygons = get_instances(graph_parse, "polygon", True, *point_keys)
            return polygons.values()[0]
        else:
            polygons = get_all_instances(graph_parse, "polygon", True)
            return SetNode(polygons.values())
    elif return_type == "twod":
        circles = get_all_instances(graph_parse, "circle", True)
        polygons = get_all_instances(graph_parse, "polygon", True)
        return SetNode(polygons.values() + circles.values())
    elif return_type == "oned":
        lines = get_all_instances(graph_parse, "line", True)
        arcs = get_all_instances(graph_parse, "arc", True)
        return SetNode(lines.values() + arcs.values())

    logging.error("failed to ground variable: %r" % variable)
    return variable
def parse_match_from_known_labels(graph_parse, known_labels):
    assert isinstance(graph_parse, GraphParse)
    match_dict = {}
    offset = graph_parse.image_segment_parse.diagram_image_segment.offset
    for idx, d in enumerate(known_labels):
        label = d['label']
        x = d['x'] - offset[0]
        y = d['y'] - offset[1]
        label_point = instantiators['point'](x, y)
        type_ = d['type']
        arr = type_.split(' ')
        if len(arr) > 1:
            type_ = arr[-1]

        # Find closest type_ instance's key in graph_parse
        instances = get_all_instances(graph_parse, type_)
        if len(arr) > 1 and type_ == 'line' and arr[0] == 'length':
            distances = [(key, label_distance_to_line(label_point, instance, True)) for key, instance in instances.iteritems()]
        elif type_ == 'line':
            distances = [(key, label_distance_to_line(label_point, instance, False)) for key, instance in instances.iteritems()]
        elif type_ == 'point':
            distances = [(key, label_distance_to_point(label_point, instance)) for key, instance in instances.iteritems()]
        elif type_ == 'arc':
            distances = [(key, label_distance_to_arc(label_point, instance)) for key, instance in instances.iteritems()]
        elif type_ == 'angle':
            distances = [(key, label_distance_to_angle(label_point, instance)) for key, instance in instances.iteritems()]

        # Then use the key to get corresponding variable in general graph
        # Wrap the general instance in function nod3. If there are extra prefixes, add these as well the formula
        argmin_key = min(distances, key=lambda pair: pair[1])[0]
        if type_ == 'line':
            a_key, b_key = argmin_key
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            formula = FunctionNode(function_signatures['Line'], [a_point, b_point])
            if len(arr) > 1 and arr[0] == 'length':
                formula = FunctionNode(function_signatures['LengthOf'], [formula])
        elif type_ == 'point':
            formula = graph_parse.point_variables[argmin_key]
        elif type_ == 'angle':
            assert len(arr) > 1 and arr[0] == 'angle'
            a_key, b_key, c_key = argmin_key
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            c_point = graph_parse.point_variables[c_key]
            formula = FunctionNode(function_signatures['Angle'], [a_point, b_point, c_point])
            formula = FunctionNode(function_signatures['MeasureOf'], [formula])
        elif type_ == 'arc':
            (center_key, radius_key), a_key, b_key = argmin_key
            center_point = graph_parse.point_variables[center_key]
            radius = graph_parse.radius_variables[center_key][radius_key]
            circle = FunctionNode(function_signatures['Circle'], [center_point, radius])
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            formula = FunctionNode(function_signatures['Arc'], [circle, a_point, b_point])
        if label not in match_dict:
            match_dict[label] = []
        elif issubtype(formula.return_type, 'entity'):
            raise Exception()
        match_dict[label].append(formula)

    match_parse = MatchParse(graph_parse, match_dict)
    return match_parse
def parse_match_from_known_labels(graph_parse, known_labels):
    assert isinstance(graph_parse, GraphParse)
    match_dict = {}
    point_key_dict = {}
    offset = graph_parse.image_segment_parse.diagram_image_segment.offset
    for idx, d in enumerate(known_labels):
        label = d['label']
        x = d['x'] - offset[0]
        y = d['y'] - offset[1]
        label_point = instantiators['point'](x, y)
        type_ = d['type']
        arr = type_.split(' ')
        if len(arr) > 1:
            type_ = arr[-1]

        # Find closest type_ instance's key in graph_parse
        instances = get_all_instances(graph_parse, type_)
        if len(instances) == 0:
            logging.error("no instance found of type %s" % type_)
            continue

        if len(arr) > 1 and type_ == 'line' and arr[0] == 'length':
            distances = [(key,
                          label_distance_to_line(label_point, instance, True))
                         for key, instance in instances.iteritems()]
        elif type_ == 'line':
            distances = [(key,
                          label_distance_to_line(label_point, instance, False))
                         for key, instance in instances.iteritems()]
        elif type_ == 'point':
            distances = [(key, label_distance_to_point(label_point, instance))
                         for key, instance in instances.iteritems()]
        elif type_ == 'arc':
            distances = [(key, label_distance_to_arc(label_point, instance))
                         for key, instance in instances.iteritems()]
        elif type_ == 'angle':
            # filter subangles
            # instances = {key: value for key, value in instances.iteritems() if all(x == value or not is_subangle(x, value) for x in instances.values())}
            distances = [(key, label_distance_to_angle(label_point, instance))
                         for key, instance in instances.iteritems()]

        # Then use the key to get corresponding variable in general graph
        # Wrap the general instance in function nod3. If there are extra prefixes, add these as well the formula
        argmin_key = min(distances, key=lambda pair: pair[1])[0]
        if type_ == 'line':
            a_key, b_key = argmin_key
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            formula = FormulaNode(signatures['Line'], [a_point, b_point])
            if len(arr) > 1 and arr[0] == 'length':
                formula = FormulaNode(signatures['LengthOf'], [formula])
        elif type_ == 'point':
            formula = graph_parse.point_variables[argmin_key]
            point_key_dict[label] = argmin_key
        elif type_ == 'angle':
            a_key, b_key, c_key = argmin_key
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            c_point = graph_parse.point_variables[c_key]
            formula = FormulaNode(signatures['Angle'],
                                  [a_point, b_point, c_point])
            if len(arr) > 1 and arr[0] == 'angle':
                formula = FormulaNode(signatures['MeasureOf'], [formula])
                formula = FormulaNode(
                    signatures['Div'],
                    [formula, FormulaNode(signatures['Degree'], [])])
        elif type_ == 'arc':
            (center_key, radius_key), a_key, b_key = argmin_key
            center_point = graph_parse.point_variables[center_key]
            radius = graph_parse.radius_variables[center_key][radius_key]
            circle = FormulaNode(signatures['Circle'], [center_point, radius])
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            formula = FormulaNode(signatures['Arc'],
                                  [circle, a_point, b_point])
        if label not in match_dict:
            match_dict[label] = []
        elif issubtype(formula.return_type, 'entity'):
            raise Exception()
        match_dict[label].append(formula)

    match_parse = MatchParse(graph_parse, match_dict, point_key_dict)
    return match_parse
def parse_match_from_known_labels(graph_parse, known_labels):
    assert isinstance(graph_parse, GraphParse)
    match_dict = {}
    point_key_dict = {}
    offset = graph_parse.image_segment_parse.diagram_image_segment.offset
    for idx, d in enumerate(known_labels):
        label = d["label"]
        x = d["x"] - offset[0]
        y = d["y"] - offset[1]
        label_point = instantiators["point"](x, y)
        type_ = d["type"]
        arr = type_.split(" ")
        if len(arr) > 1:
            type_ = arr[-1]

        # Find closest type_ instance's key in graph_parse
        instances = get_all_instances(graph_parse, type_)
        if len(instances) == 0:
            logging.error("no instance found of type %s" % type_)
            continue

        if len(arr) > 1 and type_ == "line" and arr[0] == "length":
            distances = [
                (key, label_distance_to_line(label_point, instance, True)) for key, instance in instances.iteritems()
            ]
        elif type_ == "line":
            distances = [
                (key, label_distance_to_line(label_point, instance, False)) for key, instance in instances.iteritems()
            ]
        elif type_ == "point":
            distances = [
                (key, label_distance_to_point(label_point, instance)) for key, instance in instances.iteritems()
            ]
        elif type_ == "arc":
            distances = [(key, label_distance_to_arc(label_point, instance)) for key, instance in instances.iteritems()]
        elif type_ == "angle":
            # filter subangles
            # instances = {key: value for key, value in instances.iteritems() if all(x == value or not is_subangle(x, value) for x in instances.values())}
            distances = [
                (key, label_distance_to_angle(label_point, instance)) for key, instance in instances.iteritems()
            ]

        # Then use the key to get corresponding variable in general graph
        # Wrap the general instance in function nod3. If there are extra prefixes, add these as well the formula
        argmin_key = min(distances, key=lambda pair: pair[1])[0]
        if type_ == "line":
            a_key, b_key = argmin_key
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            formula = FormulaNode(signatures["Line"], [a_point, b_point])
            if len(arr) > 1 and arr[0] == "length":
                formula = FormulaNode(signatures["LengthOf"], [formula])
        elif type_ == "point":
            formula = graph_parse.point_variables[argmin_key]
            point_key_dict[label] = argmin_key
        elif type_ == "angle":
            a_key, b_key, c_key = argmin_key
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            c_point = graph_parse.point_variables[c_key]
            formula = FormulaNode(signatures["Angle"], [a_point, b_point, c_point])
            if len(arr) > 1 and arr[0] == "angle":
                formula = FormulaNode(signatures["MeasureOf"], [formula])
                formula = FormulaNode(signatures["Div"], [formula, FormulaNode(signatures["Degree"], [])])
        elif type_ == "arc":
            (center_key, radius_key), a_key, b_key = argmin_key
            center_point = graph_parse.point_variables[center_key]
            radius = graph_parse.radius_variables[center_key][radius_key]
            circle = FormulaNode(signatures["Circle"], [center_point, radius])
            a_point = graph_parse.point_variables[a_key]
            b_point = graph_parse.point_variables[b_key]
            formula = FormulaNode(signatures["Arc"], [circle, a_point, b_point])
        if label not in match_dict:
            match_dict[label] = []
        elif issubtype(formula.return_type, "entity"):
            raise Exception()
        match_dict[label].append(formula)

    match_parse = MatchParse(graph_parse, match_dict, point_key_dict)
    return match_parse
Exemplo n.º 12
0
             pairs.append((distance, char, pointid))
     for pair in pairs:
         if pair[0] < 500 and pair[1] not in match_char.keys(
         ) and pair[2] not in match_char.values():
             match_char[pair[1]] = pair[2]
     for char, idx in match_char.items():
         match_char[char] = (core_parse.intersection_points[idx].x,
                             core_parse.intersection_points[idx].y)
     # print(i, match_char)
     # cimage = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
     # for char, point in match_char.items():
     #     cv2.circle(cimage, (int(point[0]), int(point[1])), 2, (0, 0, 255), 2)
     # cv2.imwrite('tmp/all_results/core{}.png'.format(i), cimage)
     extract_list.append({
         'points':
         get_all_instances(graph_parse, 'point').keys(),
         'lines':
         get_all_instances(graph_parse, 'line').keys(),
         'circles':
         get_all_instances(graph_parse, 'circle').keys(),
         'angles':
         get_all_instances(graph_parse, 'angle').keys(),
         'polygon':
         get_all_instances(graph_parse, 'polygon').keys(),
         'chars':
         detected_char,
         'match_char':
         match_char
     })
 except:
     print('fail"', i)
Exemplo n.º 13
0
# selected.display_primitives()
cv2.imwrite('tmp/results/selected1.png', selected.get_image_primitives())

# point clustering
core_parse = parse_core(selected)
# core_parse.display_points()
cv2.imwrite('tmp/results/core1.png', core_parse.get_image_points())

# print(core_parse.intersection_points.values())
# for point in core_parse.intersection_points:
#     print(point)

# elements fine-tuning

graph_parse = parse_graph(core_parse)
lines = get_all_instances(graph_parse, 'line')
circles = get_all_instances(graph_parse, 'circle')
arcs = get_all_instances(graph_parse, 'arc')
angles = get_all_instances(graph_parse, 'angle')
print(lines)
print(circles)
# image = core_parse.get_image_points()
image = cv2.imread(image_path)
# image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
for line in lines.values():
    image = draw_line2(image, line, offset=image_segment_parse.diagram_image_segment.offset)
cv2.imwrite('tmp/results/im_lines.png', image)

# image = cv2.imread(image_path)
for circle in circles.values():
    image = draw_circle2(image, circle, offset=image_segment_parse.diagram_image_segment.offset)