Skip to content

Commit

Permalink
Remove requirement for image files in RO training
Browse files Browse the repository at this point in the history
Extracts image dimensions from XML instead of image files
  • Loading branch information
mittagessen committed Dec 4, 2023
1 parent baff962 commit 5b441ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions kraken/lib/dataset/ro.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(self, files: Sequence[Union[PathLike, str]] = None,
else:
raise ValueError(f'Invalid RO type {level}')
# traverse RO and substitute features.
w, h = Image.open(doc.imagename).size
w, h = doc.image_size
sorted_lines = []
for line in order:
line_coords = np.array(line.baseline) / (w, h)
Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(self, files: Sequence[Union[PathLike, str]] = None,
else:
raise ValueError(f'Invalid RO type {level}')
# traverse RO and substitute features.
w, h = Image.open(doc.imagename).size
w, h = doc.image_size
sorted_lines = []
for line in order:
line_coords = np.array(line.baseline) / (w, h)
Expand Down
6 changes: 6 additions & 0 deletions kraken/lib/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class XMLPage(object):
type: Literal['baselines', 'bbox'] = 'baselines'
base_dir: Optional[Literal['L', 'R']] = None
imagename: PathLike = None
image_size: Tuple[int, int] = None
_orders: Dict[str, Dict[str, Any]] = None
has_tags: bool = False
_tag_set: Optional[Dict] = None
Expand Down Expand Up @@ -113,7 +114,10 @@ def _parse_alto(self):
image = doc.find('.//{*}fileName')
if image is None or not image.text:
raise ValueError('No valid image filename found in ALTO file {self.filename}')

self.imagename = base_directory.joinpath(image.text)
page = doc.find('.//{*}Page')
self.image_size = int(page.get('WIDTH')), int(page.get('HEIGHT'))

# find all image regions in order
regions = []
Expand Down Expand Up @@ -292,6 +296,8 @@ def _parse_page(self):
except KeyError:
logger.warning(f'Invalid value {image.get("readingDirection")} encountered in page-level reading direction.')
self.imagename = base_directory.joinpath(image.get('imageFilename'))
self.image_size = int(image.get('imageWidth')), int(image.get('imageHeight'))

# find all image regions
regions = [reg for reg in image.iterfind('./{*}*')]
# parse region type and coords
Expand Down

0 comments on commit 5b441ad

Please sign in to comment.