Use x, y, top, bottom instead of x1, y1, x2, y2. Add aliases: left=x, top=y.
This commit is contained in:
parent
360ed844c2
commit
4ff37720d9
3 changed files with 45 additions and 29 deletions
|
|
@ -62,14 +62,14 @@ class PageScene(QGraphicsScene):
|
||||||
|
|
||||||
letterPen = QPen(QColor(50, 50, 50, 100))
|
letterPen = QPen(QColor(50, 50, 50, 100))
|
||||||
letterBrush = QBrush(QColor(255, 255, 0, 80))
|
letterBrush = QBrush(QColor(255, 255, 0, 80))
|
||||||
linePen = QPen(QColor(255, 150, 150, 100))
|
linePen = QPen(QColor(255, 50, 50, 100))
|
||||||
for line in page:
|
for line in page:
|
||||||
for word in line:
|
for word in line:
|
||||||
for letter in word:
|
for letter in word:
|
||||||
if not letter.image.isspace:
|
if not letter.image.isspace:
|
||||||
self.addRect(letter.x1, letter.y1, letter.width - 1, letter.height - 1, letterPen, letterBrush)
|
self.addRect(letter.x - 1, letter.y - 1, letter.width + 1, letter.height + 1, letterPen, letterBrush)
|
||||||
self.addLine(line.x1, line.baseline - 1, line.x2, line.baseline - 1, linePen)
|
self.addLine(line.left, line.baseline, line.right, line.baseline, linePen)
|
||||||
# self.addRect(line.x1, line.y1, line.width, line.height, Qt.red)
|
# self.addRect(line.x, line.y, line.width, line.height, Qt.red)
|
||||||
|
|
||||||
def addPage(self, page):
|
def addPage(self, page):
|
||||||
qimage = ndimage2qimage(page.image.data)
|
qimage = ndimage2qimage(page.image.data)
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,10 @@ def _is_nonblank(bitmap):
|
||||||
class Image(object):
|
class Image(object):
|
||||||
"""Basic image class."""
|
"""Basic image class."""
|
||||||
|
|
||||||
def __init__(self, data, y1=0, x1=0):
|
def __init__(self, data, x=0, y=0):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.y1 = y1
|
self.x = x
|
||||||
self.x1 = x1
|
self.y = y
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
"""Return an Image for the specified region."""
|
"""Return an Image for the specified region."""
|
||||||
|
|
@ -59,12 +59,12 @@ class Image(object):
|
||||||
else:
|
else:
|
||||||
yslice, xslice = key
|
yslice, xslice = key
|
||||||
|
|
||||||
ystart, yend = indices(yslice, self.height)
|
|
||||||
xstart, xend = indices(xslice, self.width)
|
xstart, xend = indices(xslice, self.width)
|
||||||
|
ystart, yend = indices(yslice, self.height)
|
||||||
|
|
||||||
y1 = self.y1 + ystart
|
x = self.x + xstart
|
||||||
x1 = self.x1 + xstart
|
y = self.y + ystart
|
||||||
return Image(self.data[key], y1, x1)
|
return Image(self.data[key], x, y)
|
||||||
|
|
||||||
def _repr_png_(self):
|
def _repr_png_(self):
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
|
|
@ -76,12 +76,20 @@ class Image(object):
|
||||||
return cls(imread(filename))
|
return cls(imread(filename))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def y2(self):
|
def left(self):
|
||||||
return self.y1 + self.height
|
return self.x
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def x2(self):
|
def right(self):
|
||||||
return self.x1 + self.width
|
return self.x + self.width
|
||||||
|
|
||||||
|
@property
|
||||||
|
def top(self):
|
||||||
|
return self.y
|
||||||
|
|
||||||
|
@property
|
||||||
|
def bottom(self):
|
||||||
|
return self.y + self.height
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def shape(self):
|
def shape(self):
|
||||||
|
|
@ -97,7 +105,7 @@ class Image(object):
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def T(self):
|
def T(self):
|
||||||
return type(self)(self.data.swapaxes(0, 1), y1=self.x1, x1=self.y1)
|
return type(self)(self.data.swapaxes(0, 1), x=self.y, y=self.x)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def bitmap(self):
|
def bitmap(self):
|
||||||
|
|
@ -175,8 +183,8 @@ class Image(object):
|
||||||
if prev_line is None:
|
if prev_line is None:
|
||||||
prev_line = line
|
prev_line = line
|
||||||
else:
|
else:
|
||||||
if line.y1 - prev_line.y2 < min_space:
|
if line.top - prev_line.bottom < min_space:
|
||||||
prev_line = self[prev_line.y1:line.y2]
|
prev_line = self[prev_line.top:line.bottom]
|
||||||
else:
|
else:
|
||||||
yield prev_line
|
yield prev_line
|
||||||
prev_line = line
|
prev_line = line
|
||||||
|
|
|
||||||
|
|
@ -45,20 +45,28 @@ class PageObject(object):
|
||||||
return self.image.width
|
return self.image.width
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def x1(self):
|
def x(self):
|
||||||
return self.image.x1
|
return self.image.x
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def x2(self):
|
def y(self):
|
||||||
return self.image.x2
|
return self.image.y
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def y1(self):
|
def left(self):
|
||||||
return self.image.y1
|
return self.image.left
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def y2(self):
|
def right(self):
|
||||||
return self.image.y2
|
return self.image.right
|
||||||
|
|
||||||
|
@property
|
||||||
|
def top(self):
|
||||||
|
return self.image.top
|
||||||
|
|
||||||
|
@property
|
||||||
|
def bottom(self):
|
||||||
|
return self.image.bottom
|
||||||
|
|
||||||
|
|
||||||
class Page(PageObject):
|
class Page(PageObject):
|
||||||
|
|
@ -95,7 +103,7 @@ class Line(PageObject):
|
||||||
gradient = filters.correlate1d(histogram, [-1, 1], axis=0)
|
gradient = filters.correlate1d(histogram, [-1, 1], axis=0)
|
||||||
# top = gradient.argmax()
|
# top = gradient.argmax()
|
||||||
bottom = gradient.argmin()
|
bottom = gradient.argmin()
|
||||||
return self.y1 + bottom
|
return self.y + bottom
|
||||||
|
|
||||||
|
|
||||||
class Word(PageObject):
|
class Word(PageObject):
|
||||||
|
|
@ -113,10 +121,10 @@ class Word(PageObject):
|
||||||
obj_indices = ndimage.find_objects(labels, max_label)
|
obj_indices = ndimage.find_objects(labels, max_label)
|
||||||
letter_images = (self.image[obj_index] for obj_index in obj_indices)
|
letter_images = (self.image[obj_index] for obj_index in obj_indices)
|
||||||
letters = (
|
letters = (
|
||||||
Letter(image, self.baseline - image.y2)
|
Letter(image, self.baseline - image.bottom)
|
||||||
for image in letter_images
|
for image in letter_images
|
||||||
)
|
)
|
||||||
sorted_letters = sorted(letters, key=lambda letter: (letter.x1, -letter.y1))
|
sorted_letters = sorted(letters, key=lambda letter: (letter.left, -letter.bottom))
|
||||||
return iter(sorted_letters)
|
return iter(sorted_letters)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue