Skip to content

Refactor dataclasses - #222

Draft
bertsky wants to merge 23 commits into
qurator-spk:mainfrom
bertsky:refactor-dataclasses
Draft

Refactor dataclasses#222
bertsky wants to merge 23 commits into
qurator-spk:mainfrom
bertsky:refactor-dataclasses

Conversation

@bertsky

@bertsky bertsky commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Based on #219, but going further (likely v0.10) …

Robert Sachunsky 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
bertsky force-pushed the refactor-dataclasses branch from b8b427c to e71923c Compare July 27, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant