Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use polygon_for_parent from ocrd_utils #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ocrd_tesserocr/crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
coordinates_of_segment,
bbox_from_polygon,
bbox_from_points,
polygon_for_parent,
polygon_from_bbox,
points_from_polygon,
bbox_from_xywh,
Expand All @@ -27,7 +28,6 @@
from ocrd import Processor

from .config import TESSDATA_PREFIX, OCRD_TOOL
from .segment_region import polygon_for_parent

TOOL = 'ocrd-tesserocr-crop'
LOG = getLogger('processor.TesserocrCrop')
Expand Down
28 changes: 1 addition & 27 deletions ocrd_tesserocr/segment_region.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import

import os.path
from shapely.geometry import Polygon
from tesserocr import (
PyTessBaseAPI,
PSM, RIL, PT
Expand All @@ -12,6 +11,7 @@
make_file_id,
assert_file_grp_cardinality,
coordinates_for_segment,
polygon_for_parent,
polygon_from_points,
polygon_from_x0y0x1y1,
points_from_polygon,
Expand All @@ -23,7 +23,6 @@
MetadataItemType,
LabelsType, LabelType,
CoordsType,
PageType,
OrderedGroupType,
ReadingOrderType,
RegionRefIndexedType,
Expand Down Expand Up @@ -304,28 +303,3 @@ def _process_page(self, it, page, page_image, page_coords, page_id):
not og.get_UnorderedGroupIndexed()):
# schema forbids empty OrderedGroup
ro.set_OrderedGroup(None)

def polygon_for_parent(polygon, parent):
"""Clip polygon to parent polygon range.

(Should be moved to ocrd_utils.coordinates_for_segment.)
"""
childp = Polygon(polygon)
if isinstance(parent, PageType):
if parent.get_Border():
parentp = Polygon(polygon_from_points(parent.get_Border().get_Coords().points))
else:
parentp = Polygon([[0,0], [0,parent.get_imageHeight()],
[parent.get_imageWidth(),parent.get_imageHeight()],
[parent.get_imageWidth(),0]])
else:
parentp = Polygon(polygon_from_points(parent.get_Coords().points))
if childp.within(parentp):
return polygon
interp = childp.intersection(parentp)
if interp.is_empty:
# FIXME: we need a better strategy against this
raise Exception("intersection of would-be segment with parent is empty")
if interp.type == 'MultiPolygon':
interp = interp.convex_hull
return interp.exterior.coords[:-1] # keep open