예제 #1
0
    def create_line(self):
        if len(self.clicker.points) > 2:
            if self.lines:
                heights = np.median(np.array(
                    [[line.heights[0], line.heights[1]]
                     for line in self.lines]),
                                    axis=0)
            else:
                heights = [5, 5]

            new_line = layout.TextLine(id=None,
                                       baseline=np.array(
                                           self.clicker.points[:-1],
                                           dtype=np.float),
                                       heights=heights)
            new_line.polygon = linepp.baseline_to_textline(
                new_line.baseline, new_line.heights)
            dummy_region = layout.RegionLayout(id='dummy',
                                               polygon=[[-1.0, -1.0],
                                                        [-1.0, -1.0],
                                                        [-1.0, -1.0]])
            dummy_region.lines.append(new_line)
            self.page_layout.regions.append(dummy_region)
            self.lines.append(new_line)
            self.clear_interaction()
            self.render()
예제 #2
0
 def create_region(self):
     if len(self.clicker.points) > 3:
         new_region = layout.RegionLayout(
             id='r{}'.format(len(self.page_layout.regions) + 1),
             polygon=np.array(self.clicker.points[:-1], dtype=np.float))
         self.page_layout.regions.append(new_region)
         self.clear_interaction()
         self.render()
예제 #3
0
    def create_region_from_lines(self):
        if self.selected_lines:
            lines = []
            for l_num in self.selected_lines:
                lines.append(self.lines[l_num].polygon)
                poly = linepp.region_from_textlines(lines)

            new_region = layout.RegionLayout(
                id='r{}'.format(len(self.page_layout.regions) + 1),
                polygon=np.array(poly.simplify(5).exterior.coords,
                                 dtype=np.float))
            self.page_layout.regions.append(new_region)
            self.clear_interaction()
            self.render()
예제 #4
0
    def create_region_from_lines(self):
        if self.selected_lines:
            poly_points = []
            max_alpha = 0
            for l_num in self.selected_lines:
                poly_points += self.lines[l_num].polygon.tolist()
                # heuristic to estimate appropriate alpha for region polygon
                x_dist = np.amax(np.diff(self.lines[l_num].baseline[:, 0]))
                height = self.lines[l_num].heights[0] + self.lines[
                    l_num].heights[1]
                max_alpha = np.amax([max_alpha, x_dist, height])

            poly = region_engine.alpha_shape(
                np.stack(poly_points, axis=1).T, 1.5 * max_alpha)
            new_region = layout.RegionLayout(
                id='r{}'.format(len(self.page_layout.regions) + 1),
                polygon=np.array(poly.simplify(5).exterior.coords,
                                 dtype=np.float))
            self.page_layout.regions.append(new_region)
            self.clear_interaction()
            self.render()
예제 #5
0
                    file=sys.stderr)
            height = np.median([h[0] + h[1] for h in heights_list])
            print(
                f"OPTIMAL DOWNAMPLING {img.shape[0]//self.downsample}:{img.shape[1]//self.downsample}",
                self.downsample, height, height / self.downsample)

        self.downsample = temp_downsample

        return baselines_list, heights_list, textlines_list


if __name__ == '__main__':
    from pero_ocr.document_ocr import layout

    test_layout = layout.PageLayout(id='test')
    test_p = layout.RegionLayout('r', np.zeros((4, 2)))

    engine_instance = EngineLineDetectorCNN(
        '/mnt/matylda1/hradis/PERO/layout_engines/baselines_prod/parsenet_multi_alldata_fix_exported'
    )
    image = cv2.imread(
        '/mnt/matylda1/ikodym/junk/refactor_test/8e41ecc2-57ed-412a-aa4f-d945efa7c624.jpg'
    )
    baselines, heights, textlines = engine_instance.detect_lines(image)

    for baseline, height, textline in zip(baselines, heights, textlines):
        new_textline = layout.TextLine(baseline=baseline,
                                       heights=height,
                                       polygon=textline)
        test_p.lines.append(new_textline)