Add Page, Line and Letter classes.
This commit is contained in:
parent
bd86367606
commit
3d6c3de316
1 changed files with 27 additions and 7 deletions
|
|
@ -7,9 +7,11 @@ from skimage.color import rgb2gray
|
||||||
from .utils import cached_property
|
from .utils import cached_property
|
||||||
|
|
||||||
|
|
||||||
class Image(IPythonImageMixin):
|
class Image(object):
|
||||||
"""Basic image class."""
|
"""Basic image class."""
|
||||||
|
|
||||||
|
child_cls = None
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self._data = data
|
self._data = data
|
||||||
|
|
||||||
|
|
@ -77,7 +79,10 @@ class Image(IPythonImageMixin):
|
||||||
def unframe(self, width=2):
|
def unframe(self, width=2):
|
||||||
return self[width:-width,width:-width]
|
return self[width:-width,width:-width]
|
||||||
|
|
||||||
def iter_lines(self, min_space=200):
|
def _iter_children(self, min_space):
|
||||||
|
if self.child_cls is None:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
line_start = None
|
line_start = None
|
||||||
prev_line_end = 0
|
prev_line_end = 0
|
||||||
for i, row in enumerate(self.bitmap):
|
for i, row in enumerate(self.bitmap):
|
||||||
|
|
@ -85,13 +90,28 @@ class Image(IPythonImageMixin):
|
||||||
if line_start is None:
|
if line_start is None:
|
||||||
line_start = i
|
line_start = i
|
||||||
if line_start - prev_line_end >= min_space:
|
if line_start - prev_line_end >= min_space:
|
||||||
yield Image.space(line_start - prev_line_end, self.width)
|
yield self.child_cls.space(line_start - prev_line_end, self.width)
|
||||||
else:
|
else:
|
||||||
if line_start is not None:
|
if line_start is not None:
|
||||||
yield self[line_start:i, :]
|
yield self.child_cls(self._data[line_start:i, :])
|
||||||
line_start = None
|
line_start = None
|
||||||
prev_line_end = i
|
prev_line_end = i
|
||||||
|
|
||||||
def iter_letters(self, min_space=10):
|
|
||||||
for letter in self.T.iter_lines(min_space=7):
|
class Page(Image):
|
||||||
yield letter.T
|
child_cls = Line
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self._iter_children(min_space=200)
|
||||||
|
|
||||||
|
|
||||||
|
class Line(Image):
|
||||||
|
child_cls = Letter
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for rotated_letter in self.T._iter_children(min_space=10):
|
||||||
|
yield rotated_letter.T
|
||||||
|
|
||||||
|
|
||||||
|
class Letter(Image):
|
||||||
|
pass
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue