Skip to content

Commit

Permalink
Merge pull request #70 from kba/test-points-xywh
Browse files Browse the repository at this point in the history
fix/test utility coordination translation functions
  • Loading branch information
kba authored Apr 26, 2018
2 parents a11f2a6 + 8d1dbef commit 79bae4e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
8 changes: 4 additions & 4 deletions ocrd/model/ocrd_xml_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
)
from ocrd.utils import (
xmllint_format,
xywh_from_coordinate_string,
coordinate_string_from_xywh
xywh_from_points,
points_from_xywh
)

from lxml import etree as ET
Expand All @@ -22,14 +22,14 @@ def get_coords(el):
coords = el.find('page:Coords', NAMESPACES)
if coords is not None:
points = coords.get('points')
return xywh_from_coordinate_string(points)
return xywh_from_points(points)

def set_coords(el, box):
if box is not None:
coords = el.find('page:Coords', NAMESPACES)
if coords is None:
coords = ET.SubElement(el, TAG_PAGE_COORDS)
coords.set("points", coordinate_string_from_xywh(box))
coords.set("points", points_from_xywh(box))

class OcrdXmlDocument(object):

Expand Down
33 changes: 23 additions & 10 deletions ocrd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,32 @@
def getLogger(*args, **kwargs):
return logging.getLogger(*args, **kwargs)

def coordinate_string_from_xywh(box):
def points_from_xywh(box):
"""
Constructs a polygon representation from a rectangle described as a dict with keys x, y, w, h.
"""
x, y, w, h = box['x'], box['y'], box['w'], box['h']
# tesseract uses a different region representation format
return "%i,%i %i,%i %i,%i %i,%i" % (
box['x'],
box['y'],
box['x'] + box['w'],
box['y'] + box['w'],
box['x'] + box['w'] + box['h'],
box['y'] + box['w'] + box['h'],
box['x'] + box['h'],
box['y'] + box['h']
x, y,
x + w, y,
x + w, y + h,
x, y + h
)

def xywh_from_coordinate_string(points):
def xywh_from_points(points):
"""
Constructs an dict representing a rectangle with keys x, y, w, h
"""
[tl, tr, br] = [[float(p) for p in pair.split(',')] for pair in points.split(' ')[:3]]
return {
'x': tl[0],
'y': tl[1],
'w': tr[0] - tl[0],
'h': br[1] - tr[1],
}

def polygon_from_points(points):
"""
Constructs a numpy-compatible polygon from a page representation.
"""
Expand All @@ -46,7 +55,11 @@ def xmllint_format(xml):
output, _ = proc.communicate(xml)
return output

# TODO better name
def mets_file_id(grp, n):
"""
Concatenate string and zero-padded 4 digit number
"""
return "%s_%04i" % (grp, n + 1)

def safe_filename(url):
Expand Down
29 changes: 29 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from test.base import TestCase, main
from ocrd.utils import (
points_from_xywh,
xywh_from_points,
polygon_from_points
)

class TestResolver(TestCase):

# def runTest(self):

def test_points_from_xywh(self):
self.assertEqual(
points_from_xywh({'x': 100, 'y': 100, 'w': 100, 'h': 100}),
'100,100 200,100 200,200 100,200'
)

def test_xywh_from_points(self):
self.assertEqual(
xywh_from_points('100,100 200,100 200,200 100,200'),
{'x': 100, 'y': 100, 'w': 100, 'h': 100})

def test_polygon_from_points(self):
self.assertEqual(
polygon_from_points('100,100 200,100 200,200 100,200'),
[[100, 100], [200, 100], [200, 200], [100, 200]])

if __name__ == '__main__':
main()

0 comments on commit 79bae4e

Please sign in to comment.