Skip to content

Commit

Permalink
constraint created lines to parent region polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnth committed Sep 21, 2021
1 parent bca4a4c commit f92298d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 10 additions & 7 deletions ocr4all_helper_scripts/helpers/pagelineseg_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

from lxml import etree
from PIL import Image
from shapely.geometry import Polygon
from shapely import affinity


# Add printing for every thread
from threading import Lock
Expand Down Expand Up @@ -329,12 +332,12 @@ def pagelineseg(xmlfile: str,
pageutils.remove_existing_textlines(root)

for coord_idx, coord in enumerate(sorted(coordmap)):
coords = coordmap[coord]['coords']
region_coords = coordmap[coord]['coords']

if len(coords) < 3:
if len(region_coords) < 3:
continue

cropped, [min_x, min_y, max_x, max_y] = imgmanipulate.cutout(im, coords)
cropped, [min_x, min_y, max_x, max_y] = imgmanipulate.cutout(im, region_coords)

if coordmap[coord].get("orientation"):
orientation = coordmap[coord]['orientation']
Expand Down Expand Up @@ -373,7 +376,7 @@ def pagelineseg(xmlfile: str,

# Interpret whole region as TextLine if no TextLines are found
if not lines or len(lines) == 0:
coord_str = " ".join([f"{x},{y}" for x, y in coords])
coord_str = " ".join([f"{x},{y}" for x, y in region_coords])
textregion = root.find(f'.//{{*}}TextRegion[@id="{coord}"]')
if orientation:
textregion.set('orientation', str(orientation))
Expand All @@ -385,9 +388,9 @@ def pagelineseg(xmlfile: str,
if coordmap[coord]["type"] == "drop-capital":
coord_str = coordmap[coord]["coordstring"]
else:
coords = ((x + min_x, y + min_y) for x, y in poly)
capped_coords = [(min(width, max(0, x)), min(height, max(0, y))) for x, y in coords]
coord_str = " ".join([f"{int(x)},{int(y)}" for x, y in capped_coords])
line_coords = Polygon([(x + min_x, y + min_y) for x, y in poly])
sanitized_coords = pageutils.sanitize(line_coords, Polygon(region_coords), width, height)
coord_str = " ".join([f"{int(x)},{int(y)}" for x, y in sanitized_coords])

textregion = root.find(f'.//{{*}}TextRegion[@id="{coord}"]')
if orientation:
Expand Down
15 changes: 15 additions & 0 deletions ocr4all_helper_scripts/utils/pageutils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
from lxml import etree
from shapely.geometry import Polygon, GeometryCollection


def sanitize(polygon: Polygon,
parent: Polygon,
page_width: int,
page_height: int):
sanitized_polygon = parent.intersection(polygon)
# If intersection leads to more than one element just use the element with the largest area as all others are
# most likely nice. TODO: check if this can't be done more elegantely
if isinstance(sanitized_polygon, GeometryCollection):
sanitized_polygon = max(sanitized_polygon, key=lambda a: a.area)
sanitized_polygon = [(min(page_width, max(0, x)),
min(page_height, max(0, y))) for x, y in sanitized_polygon.exterior.coords]
return sanitized_polygon


def get_root(xmlfile: str) -> etree.Element:
Expand Down

0 comments on commit f92298d

Please sign in to comment.