Refactor dataclasses - #222
Draft
bertsky wants to merge 23 commits into
Draft
Conversation
added 16 commits
July 27, 2026 13:31
- `utils`:
- add dataclasses `Region` (including contour, skew, conf,
centers and area – the latter computed during init, but only once
instead of over and over) and `TextRegion` (ditto, but with a list
of lines) to encapsulate intermediate results more succinctly
- drop `check_any_text_region_in_model_one_is_main_or_header`
- `run_single()`:
- instantiate `Region` and `TextRegion` lists for separator lines,
drop-caps, marginals, paragraphs, headings, images, tables instead
of multiple sets of identifiers for these segment types, name them
more consistently
- apply contour "dilation" (really Shapely polygon instantiation,
repair/join and buffering) only once in the writer after rescaling
to original/final image size
- `filter_contours_inside_a_bigger_one()`: separate and rename
- → `filter_small_regions()`: taking lists of `Region` (original and
deskewed) for paragraphs and marginals, filtering small regions
that are contained in large regions or marginals in-place
- → `filter_small_textlines()`: taking a list of `TextRegion` and
filtering lines that are contained in larger lines (of possibly
other regions) in-place
- `filter_contours_without_textlines_inside()`: simplify and rename
→ `filter_textregions_without_textlines()`
- rename `separate_marginals_to_left_and_right_and_order_from_top_to_down`
→ `separate_marginals_and_order()` and simplify
- `get_slopes_and_deskew*`: work on list of `Region` in-place
(modifying their `.skew` and `.lines`), extract line confidence, too
- writer:
- expect `Region` and `TextRegion` instances for various segment types
instead of wild list of identifiers
- simplify (a lot)
- merge `build_pagexml_no_full_layout` and build_pagexml_full_layout`
→ `build_pagexml()` (w/ empty lists as default)
- also annotate textline confidence
- enlarge/dilate/buffer coordinates differently for various segment types:
6px for text and tables, 2px for images and seplines
- add more type hints
- fix model loading for predictor - add `extract_images` model to ModelZoo and Makefile, too - rename `get_regions_light_v_extract_only_images` → `get_early_layout` (with similar refactoring as done in superclass previously) - separate `run_single()` from `run()` (as in superclass) - adapt to new `Region` dataclasses, too
- `read_xml()`: bring sanity here…
- use identifiers for class labels instead of numeric literals
- use functions from `ocrd_utils` for PAGE coordinates
- use PrintSpace/Border coordinates directly instead of plotting
and contour detection
- simplify ad-hoc PAGE XML parser and writer (a lot)
- avoid XML invalidity when `AlternativeImage` exists
- separate `run_single()` from `run()` (as in superclass)
- re-use superclass' `do_order_of_regions_with_model()`
- include drop capitals, too (as in superclass)
- CLI: add `--overwrite`, too
use `Region` for page (Border) coordinates and skew angle (as for the other segment types), pass as mandatory arg to writer
- layout/extract-images: when running with `input_binary`, then pass the binarized image of the full page to the writer - writer: if there is a binarized image, then add an `AlternativeImage` to the top-level of the output PAGE (with the array instead of a file name) - writer.write_xml (standalone CLIs): save the image in the same directory as the output file, but with `.bin.png` isntead of `.xml` as suffix - processor: convert the binarized image array to PIL and pass over as OcrdPageResultImage for the final path name
- calls `run_boxes_order()` and `do_order_of_regions()` - gets called w/ original or deskewed contours/masks - heading contours as empty list instead of `None`
- CLI: rename `machine-based-reading-order` → `reorder`
- CLI: default to heuristic, add `-mb` / `--model_based`
for old behaviour
- CLI: add `-dim` / `--dir_imgs` for path resolution of
image files (needed for col-classifier phase of heuristic
RO)
- in heuristic mode:
- `setup_models()`: load `page` and `col_classifier` models
instead of `reading_order`
- `read_xml()`: also extract skew angle and `img_filename`,
as well as table region labels
- `run_single()`: resolve image file name by trying PAGE
`@imageFilename` with prefixes and trying PAGE XML
basename with typical image suffixes
- `run_single()`: read images and run column classifier
(which may crop andresize the image and); adapt extracted
label map and contours to new size if resized
- derive non-separator mask (`regions_without_separators`)
by setting separators and images to zero in region map
- deskew if necessary (masks and contours)
- then call `do_order_of_regions_heuristic()`
- also log output file
bertsky
force-pushed
the
refactor-dataclasses
branch
from
July 27, 2026 11:32
b8b427c to
e71923c
Compare
added 6 commits
July 28, 2026 15:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on #219, but going further (likely v0.10) …