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

improve reading order and region order #44

Merged
merged 2 commits into from
May 24, 2022
Merged
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
20 changes: 17 additions & 3 deletions ocrd_browser/model/page_xml_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from PIL import ImageDraw, Image, ImageFont

from ocrd_models.ocrd_page import PcGtsType, PageType, BorderType, PrintSpaceType, RegionType, TextRegionType, TextLineType, WordType, GlyphType, GraphemeType, ChartRegionType, GraphicRegionType
from ocrd_models.ocrd_page import PcGtsType, PageType, BorderType, PrintSpaceType, RegionType, TextRegionType, TextLineType, WordType, GlyphType, GraphemeType, ChartRegionType, GraphicRegionType, SeparatorRegionType, TableRegionType
from ocrd_utils import coordinates_of_segment, getLogger, polygon_from_points, transform_coordinates

from shapely.geometry import Polygon, Point, LineString
Expand Down Expand Up @@ -516,12 +516,26 @@ def render_all(self, pc_gts: PcGtsType) -> None:
page: PageType = pc_gts.get_Page()
self.render_type(page.get_PrintSpace())
self.render_type(page.get_Border())
for region_ds in page.get_AllRegions(order='reading-order'):
def region_priority(region):
# often, regions overlap; since we don't alpha-composite,
# we should provide a useful default priority of what is
# rendered last (and thus unoccluded); document order is
# not helpful, because it depends on the workflow or geometry
# instead of visibility concerns; thus, ensure that:
# - SeparatorRegion is top-most
# - TextRegion is next
# - all others are next
if isinstance(region, SeparatorRegionType):
return 0
if isinstance(region, TextRegionType):
return -1
return -2
for region_ds in sorted(page.get_AllRegions(), key=region_priority):
self.render_type(region_ds)

if self.features & Feature.ORDER:
last_point: Optional[Point] = None
for region_ds in page.get_AllRegions(classes=['Text'], order='reading-order'):
for region_ds in page.get_AllRegions(order='reading-order-only'):
region = self.region_factory.create(region_ds)
new_point = region.poly.representative_point()
if last_point:
Expand Down