[reading order] Improve reading order and add exporters#2116
[reading order] Improve reading order and add exporters#2116felixdittrich92 wants to merge 13 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2116 +/- ##
==========================================
- Coverage 96.61% 96.08% -0.53%
==========================================
Files 168 169 +1
Lines 9094 9533 +439
==========================================
+ Hits 8786 9160 +374
- Misses 308 373 +65
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR improves DocTR’s reading-order handling (including memory optimizations), introduces a dedicated doctr.io.exporters module with new Markdown/AsciiDoc/HTML exporters, and updates the builder/predictor paths and docs/tests to support these new export surfaces.
Changes:
- Refactors export logic into
doctr/io/exporters.py, adds Markdown/AsciiDoc/HTML exporters, and wires export mixins into core element classes. - Updates reading-order computation (DAG traversal + overlap ratio memory behavior) and adds extensive regression tests for reading-order and exporters.
- Improves table-region cropping by adding padding and adjusts table word ordering / geometry handling in the builder.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/common/test_models_reading_order.py | Adds regression tests and helpers for reading-order edge cases (columns, key/value rows). |
| tests/common/test_models_builder.py | Extends builder repr expectations and adds tests for keep_reading_order. |
| tests/common/test_io_exporters.py | New test suite covering page/document export to Markdown/AsciiDoc/HTML/XML and reading-order behavior. |
| doctr/models/reading_order/base.py | Optimizes overlap computations and refines topological reading-order traversal (incl. multi-column heuristic). |
| doctr/models/predictor/pytorch.py | Adds padding to table-region crops to improve downstream table recognition. |
| doctr/models/layout/lw_detr/pytorch.py | Updates layout model class list (removes checkbox classes). |
| doctr/models/builder.py | Adds keep_reading_order, improves rotated-page handling in line resolution, and refines table cell word ordering. |
| doctr/io/exporters.py | New module implementing exporters, reading-order caching, and export mixins. |
| doctr/io/elements.py | Moves export/render methods to mixins (delegating to doctr.io.exporters). |
| doctr/io/init.py | Re-exports new exporters in the doctr.io package namespace. |
| docs/source/using_doctr/using_models.rst | Documents new export APIs and keep_reading_order. |
| docs/source/modules/io.rst | Adds API docs for new export methods and exporter classes. |
Comments suppressed due to low confidence (1)
doctr/io/exporters.py:930
- Same issue as
export_as_markdown: document-level calls can reasonably passinclude_furniture, which currently breaks on KIE pages. Accept/ignore it here as well to avoidTypeErrorwhen exporting mixed document types with shared helpers.
def export_as_asciidoc(self, direction: str = "auto", escape: bool = True) -> str:
"""Export the KIE page as AsciiDoc, with the predictions of each class sorted in reading order.
Args:
direction: reading direction, one of 'auto', 'ltr', 'rtl', 'ttb-rtl' or 'ttb-ltr'
escape: whether the characters and line markers carrying a structural meaning in AsciiDoc should
be escaped
Returns:
an AsciiDoc string with one section per detection class
"""
return AsciiDocExporter().export_kie_page(cast("KIEPage", self), direction=direction, escape=escape)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def export_as_markdown(self, direction: str = "auto", escape: bool = True) -> str: | ||
| """Export the KIE page as Markdown, with the predictions of each class sorted in reading order. | ||
|
|
||
| Args: | ||
| direction: reading direction, one of 'auto', 'ltr', 'rtl', 'ttb-rtl' or 'ttb-ltr' | ||
| escape: whether the characters carrying a structural meaning in Markdown should be escaped | ||
|
|
||
| Returns: | ||
| a Markdown string with one section per detection class | ||
| """ | ||
| return MarkdownExporter().export_kie_page(cast("KIEPage", self), direction=direction, escape=escape) | ||
|
|
| def export_as_html(self, direction: str = "auto") -> str: | ||
| """Export the KIE page as semantic HTML, with the predictions of each class sorted in reading order""" | ||
| return HTMLExporter().export_kie_page(cast("KIEPage", self), direction=direction) | ||
|
|
| To export the output in reading order as Markdown, AsciiDoc, HTML or XML, you can use the `export_as_markdown`, | ||
| `export_as_asciidoc`, `export_as_html` and `export_as_xml` methods (also available on a single :class:`Page`). | ||
| Reading order is always applied by these exporters: the content is linearized column by column, the reading direction is inferred from the | ||
| recognized text (e.g. right-to-left for Arabic or Hebrew documents), and - when the predictor is run with | ||
| layout detection (``detect_layout=True``) - the layout regions are used to render headings, list items and | ||
| recognized tables, and to place the page furniture (headers, footers, footnotes): |
| The reading-order-aware export of a :class:`Document` / :class:`Page` to Markdown, AsciiDoc is available | ||
| through the ``export_as_markdown`` / ``export_as_asciidoc`` / ``export_as_html`` / ``export_as_xml`` / ``export_as`` / ``export`` / ``render`` methods documented above, which | ||
| delegate to the exporters of :mod:`doctr.io.exporters`. |
This PR:
Closes: #1586 #2085