Esempio 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])
Esempio n. 2
0
def diagram_to_graph_parse(diagram):
    image_segment_parse = parse_image_segments(diagram)
    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)
    return graph_parse
Esempio n. 3
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])
Esempio n. 4
0
def question_to_graph_parse(question):
    diagram = open_image(question.diagram_path)
    image_segment_parse = parse_image_segments(diagram)
    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)
    return graph_parse
def question_to_graph_parse(question):
    diagram = open_image(question.diagram_path)
    image_segment_parse = parse_image_segments(diagram)
    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)
    return graph_parse
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
    }
Esempio n. 7
0
    def parse_image(
            self,
            image_path="C:/Users/DM/Desktop/example_1.JPG",
            use_haris_centroids=True):  # image_path = 'tmp/images2/0.png'
        ge = Geometry_Extractor()
        img = cv2.imread(image_path, 0)

        ## parse image
        image_segment_parse, detected_char = ge.parse_image_segments(img)
        # cv2.imwrite('tmp/results/source1.png', image_segment_parse.diagram_image_segment.segmented_image)
        primitive_parse = parse_primitives(image_segment_parse,
                                           parse_circles=False)
        # cv2.imwrite('tmp/results/primitive1.png', primitive_parse.get_image_primitives())
        selected = select_primitives(primitive_parse)
        # cv2.imwrite('tmp/results/selected1.png', selected.get_image_primitives())
        print("Displaying primitives...")
        core_parse = parse_core(selected)
        print("Displaying points...")
        core_parse.display_points(display_label=False)
        # cv2.imwrite('tmp/results/core1.png', core_parse.get_image_points())
        # print(core_parse.intersection_points)
        print(detected_char)
        graph_parse = parse_graph(core_parse)
        self.current_graph_parse = graph_parse

        # image = cv2.imread(image_path)
        # 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/im_circles.png', image)

        match = {}
        candidate_pairs = []
        for char in detected_char.keys():
            top_left, bottom_right = detected_char[char]
            mid_point = ((top_left[0] + bottom_right[0]) / 2,
                         (top_left[1] + bottom_right[1]) / 2)
            for pointid, loc in core_parse.intersection_points.items():
                distance = (mid_point[0] - loc.x)**2 + (mid_point[1] -
                                                        loc.y)**2
                candidate_pairs.append((distance, char, pointid))
        candidate_pairs.sort(key=lambda x: x[0])
        for pair in candidate_pairs:
            if pair[0] < 500 and pair[1] not in match.keys(
            ) and pair[2] not in match.values():
                match[pair[1]] = pair[2]
        self.current_match = deepcopy(match)
        print(match)
        for char, idx in match.items():
            match[char] = core_parse.intersection_points[idx]
        self.current_match_position = match
        print(match)

        ## harris centroids detection, refer: https://docs.opencv.org/3.4/dc/d0d/tutorial_py_features_harris.html
        if use_haris_centroids:
            import ipdb
            ipdb.set_trace()
            # find Harris corners
            origin_img = core_parse.image_segment_parse.get_colored_original_image(
            )
            gray_img = cv2.cvtColor(origin_img, cv2.COLOR_BGR2GRAY)
            gray_img = np.float32(gray_img)
            # cv2.imwrite("corner.png", gray_img)
            # Harris_detector = cv2.cornerHarris(gray_img, 7, 3, 0.08)
            Harris_detector = cv2.cornerHarris(gray_img, 5, 3, 0.04)
            cv2.imwrite("corner1.png", Harris_detector)
            dst = cv2.dilate(Harris_detector, None)
            cv2.imwrite("corner2.png", dst)
            img_2 = cv2.imread(image_path)
            # img_2[dst>0.01*dst.max()] = [255, 0, 0]
            # cv2.imwrite('corner3.png', img_2)

            # find centroids
            ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
            dst = np.uint8(dst)
            _, _, _, centroids = cv2.connectedComponentsWithStats(dst)

            # Now draw centroids
            for xy in centroids:
                xy_int = xy.copy().astype(int)
                cv2.circle(img_2, tuple(xy_int), 1, (255, 0, 0), thickness=-1)
                cv2.putText(img_2,
                            str(xy_int),
                            tuple(xy_int),
                            cv2.FONT_HERSHEY_PLAIN,
                            1.0, (0, 0, 0),
                            thickness=1)
            cv2.imwrite("image.png", img_2)

            new_match = {}
            new_candidate_pairs = []
            for char, position in match.items():
                for centroid in centroids:
                    distance = (centroid[0] - position.x)**2 + (centroid[1] -
                                                                position.y)**2
                    new_candidate_pairs.append(
                        (distance, char, centroid.tolist()))
            new_candidate_pairs.sort(key=lambda x: x[0])
            for pair in new_candidate_pairs:
                if pair[1] not in new_match.keys(
                ) and pair[2] not in new_match.values():
                    new_match[pair[1]] = pair[2]
            self.current_match_position = new_match
            print("new_match:", new_match)

        # HACK: restore format and rerank by letter-order
        coordinates = []
        for char, coo in self.current_match_position.items():
            if use_haris_centroids:
                coordinates.append("{0}({1}+7,{2}-24)".format(
                    char, coo[0], coo[1]))
            else:
                coordinates.append("{0}({1}+7,{2}-24)".format(
                    char, coo.x, coo.y))
        coordinates.sort(key=lambda x: x[0])

        return " ".join(coordinates)
        print(i)
    match_char = {}
    try:
        image = cv2.imread(image_path.format(i), cv2.IMREAD_GRAYSCALE)
        # if(image.sum()) == 0:
        #     image = cv2.imread(image_path + image_path.format(i), -1)
        #     image = image[:, :, 3]
        #     maxd = image.max()
        #     mind = image.min()
        #     image = (1 - (image - mind) / (maxd - mind)) * 255
        #     image = image.astype(np.uint8)
        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)
        graph_parse = parse_graph(core_parse)
        pairs = []

        for char in detected_char.keys():
            top_left, bottom_right = detected_char[char]
            mid_point = ((top_left[0] + bottom_right[0]) / 2,
                         (top_left[1] + bottom_right[1]) / 2)
            for pointid, loc in core_parse.intersection_points.items():
                distance = (mid_point[0] - loc.x)**2 + (mid_point[1] -
                                                        loc.y)**2
                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():