-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
post processing and text_line extraction
- Loading branch information
Showing
11 changed files
with
222 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def subtract(text_document, total_document): | ||
non_text_document = total_document - text_document | ||
return non_text_document |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from postProcessing.classes.line import Line | ||
from postProcessing.classes.line_collector import LineCollector | ||
|
||
|
||
def is_near(comp_left, comp_right): | ||
distance_height = abs(comp_right.xmin - comp_left.xmax) <= 1.2 * max(comp_left.bb_height, comp_right.bb_height) | ||
if distance_height: | ||
return True | ||
return False | ||
|
||
|
||
def create_line(comp_list): | ||
x_min = min(c.xmin for c in comp_list) | ||
x_max = max(c.xmax for c in comp_list) | ||
y_min = min(c.ymin for c in comp_list) | ||
y_max = max(c.ymax for c in comp_list) | ||
|
||
return Line(x_min, y_min, x_max, y_max) | ||
|
||
|
||
def find_lines(text_component): | ||
lines = [] | ||
for c in text_component.as_list(): | ||
if not (any(c in l for l in lines)): | ||
line = [] | ||
same_row = c.same_row.as_list() | ||
line.extend(comp for comp in same_row if comp.type == 'text') | ||
if len(line) != 0: | ||
if c not in line: | ||
line.append(c) | ||
sorted_l = sorted(line, key=lambda comp: comp.xmin) | ||
lines.append(sorted_l) | ||
return lines | ||
|
||
|
||
def text_segmentation(text_tree): | ||
l_collector = LineCollector() | ||
component_collector = text_tree.included.text_component() | ||
component_lines = find_lines(component_collector) | ||
chains = [] | ||
for line in component_lines: | ||
chain_same_line = [] | ||
chain = [line[0]] | ||
for c_id in range(len(line) - 1): | ||
if is_near(line[c_id], line[c_id + 1]): | ||
chain.append(line[c_id + 1]) | ||
else: | ||
chain_same_line.append(create_line(chain)) | ||
l_collector.add_line(create_line(chain)) | ||
chain = [line[c_id + 1]] | ||
if c_id + 1 == len(line) - 1: | ||
chain_same_line.append(create_line(chain)) | ||
l_collector.add_line(create_line(chain)) | ||
chains.append(chain_same_line) | ||
|
||
return l_collector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
class Line: | ||
def __init__(self, x_min, y_min, x_max, y_max): | ||
self._xmin = x_min | ||
self._ymin = y_min | ||
self._xmax = x_max | ||
self._ymax = y_max | ||
|
||
@property | ||
def xmin(self): | ||
return self._xmin | ||
|
||
@property | ||
def ymin(self): | ||
return self._ymin | ||
|
||
@property | ||
def xmax(self): | ||
return self._xmax | ||
|
||
@property | ||
def ymax(self): | ||
return self._ymax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import numpy as np | ||
from postProcessing.classes.line import Line | ||
|
||
class LineCollector: | ||
|
||
def __init__(self): | ||
self._line_list = [] | ||
self._cached_matrix = None | ||
|
||
def add_line(self, line): | ||
if len(self._line_list) > 0: | ||
overlapped = self.overlap(line) | ||
if len(overlapped) > 0: | ||
line = self.unify(overlapped, line) | ||
self._line_list.append(line) | ||
self._cached_matrix = None | ||
|
||
def as_matrix(self): | ||
if self._cached_matrix is not None: | ||
return self._cached_matrix | ||
|
||
self._cached_matrix = np.array([[v.xmin, v.ymin, v.xmax, v.ymax] for v in self._line_list]) | ||
return self._cached_matrix | ||
|
||
def as_list(self): | ||
return self._line_list | ||
|
||
def overlap(self, line): | ||
xmin_less_linemin = self.as_matrix()[:, 0] <= line.xmin | ||
linemin_less_xmax = line.xmin <= self.as_matrix()[:, 2] | ||
and_1_2 = np.bitwise_and(xmin_less_linemin, linemin_less_xmax) | ||
|
||
linemin_less_xmin = line.xmin <= self.as_matrix()[:, 0] | ||
xmin_less_linemax = self.as_matrix()[:, 0] <= line.xmax | ||
and_3_4 = np.bitwise_and(linemin_less_xmin, xmin_less_linemax) | ||
|
||
same_row = (np.maximum(self.as_matrix()[:, 1], line.ymin) - np.minimum(self.as_matrix()[:, 3], line.ymax)) < 0 | ||
return np.where(np.bitwise_and(np.bitwise_or(and_1_2, and_3_4), same_row))[0].tolist() | ||
|
||
def unify(self, overlapped, line): | ||
x_min = min(line.xmin, min(self.as_list()[i].xmin for i in overlapped)) | ||
x_max = max(line.xmax, max(self.as_list()[i].xmax for i in overlapped)) | ||
y_min = min(line.ymin, min(self.as_list()[i].ymin for i in overlapped)) | ||
y_max = max(line.ymax, max(self.as_list()[i].ymax for i in overlapped)) | ||
for index in sorted(overlapped, reverse=True): | ||
del self._line_list[index] | ||
return Line(x_min, y_min, x_max, y_max) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters