Overview¶
Reading Order Module.
Provides predictors for determining the logical reading sequence of document elements based on layout detection and spatial analysis.
Available Predictors
- RuleBasedReadingOrderPredictor: Rule-based predictor using R-tree indexing
Example
from omnidocs.tasks.reading_order import RuleBasedReadingOrderPredictor
from omnidocs.tasks.layout_extraction import DocLayoutYOLO, DocLayoutYOLOConfig
from omnidocs.tasks.ocr_extraction import EasyOCR, EasyOCRConfig
# Initialize components
layout_extractor = DocLayoutYOLO(config=DocLayoutYOLOConfig())
ocr = EasyOCR(config=EasyOCRConfig())
predictor = RuleBasedReadingOrderPredictor()
# Process document
layout = layout_extractor.extract(image)
ocr_result = ocr.extract(image)
reading_order = predictor.predict(layout, ocr_result)
# Get text in reading order
text = reading_order.get_full_text()
# Get elements by type
tables = reading_order.get_elements_by_type(ElementType.TABLE)
# Get caption associations
for elem in reading_order.ordered_elements:
if elem.element_type == ElementType.FIGURE:
captions = reading_order.get_captions_for(elem.original_id)
print(f"Figure {elem.original_id} captions: {[c.text for c in captions]}")
BaseReadingOrderPredictor
¶
Bases: ABC
Abstract base class for reading order predictors.
Reading order predictors take layout detection and OCR results and produce a properly ordered sequence of document elements.
Example
predict
abstractmethod
¶
predict(
layout: LayoutOutput,
ocr: Optional[OCROutput] = None,
page_no: int = 0,
) -> ReadingOrderOutput
Predict reading order for a single page.
| PARAMETER | DESCRIPTION |
|---|---|
layout
|
Layout detection results with bounding boxes
TYPE:
|
ocr
|
Optional OCR results. If provided, text will be matched to layout elements by bbox overlap.
TYPE:
|
page_no
|
Page number (for multi-page documents)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReadingOrderOutput
|
ReadingOrderOutput with ordered elements and associations |
Example
Source code in omnidocs/tasks/reading_order/base.py
predict_multi_page
¶
predict_multi_page(
layouts: List[LayoutOutput],
ocrs: Optional[List[OCROutput]] = None,
) -> List[ReadingOrderOutput]
Predict reading order for multiple pages.
| PARAMETER | DESCRIPTION |
|---|---|
layouts
|
List of layout results, one per page
TYPE:
|
ocrs
|
Optional list of OCR results, one per page
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[ReadingOrderOutput]
|
List of ReadingOrderOutput, one per page |
Source code in omnidocs/tasks/reading_order/base.py
BoundingBox
¶
Bases: BaseModel
Bounding box in pixel coordinates.
to_list
¶
from_list
classmethod
¶
Create from [x1, y1, x2, y2] list.
Source code in omnidocs/tasks/reading_order/models.py
to_normalized
¶
Convert to normalized coordinates (0-1024 range).
| PARAMETER | DESCRIPTION |
|---|---|
image_width
|
Original image width in pixels
TYPE:
|
image_height
|
Original image height in pixels
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BoundingBox
|
New BoundingBox with coordinates in 0-1024 range |
Source code in omnidocs/tasks/reading_order/models.py
ElementType
¶
Bases: str, Enum
Type of document element for reading order.
OrderedElement
¶
Bases: BaseModel
A document element with its reading order position.
Combines layout detection results with OCR text and assigns a reading order index.
to_dict
¶
Convert to dictionary representation.
Source code in omnidocs/tasks/reading_order/models.py
ReadingOrderOutput
¶
Bases: BaseModel
Complete reading order prediction result.
Provides: - Ordered list of document elements - Caption-to-element associations - Footnote-to-element associations - Merge suggestions for split elements
Example
get_full_text
¶
Get concatenated text in reading order.
Excludes page headers, footers, captions, and footnotes from main text flow.
Source code in omnidocs/tasks/reading_order/models.py
get_elements_by_type
¶
get_captions_for
¶
Get caption elements for a given element ID.
Source code in omnidocs/tasks/reading_order/models.py
get_footnotes_for
¶
Get footnote elements for a given element ID.
Source code in omnidocs/tasks/reading_order/models.py
to_dict
¶
Convert to dictionary representation.
Source code in omnidocs/tasks/reading_order/models.py
save_json
¶
Save to JSON file.
load_json
classmethod
¶
RuleBasedReadingOrderPredictor
¶
Bases: BaseReadingOrderPredictor
Rule-based reading order predictor using spatial analysis.
Uses R-tree spatial indexing and rule-based algorithms to determine the logical reading sequence of document elements. This is a CPU-only implementation that doesn't require GPU resources.
Features: - Multi-column layout detection - Header/footer separation - Caption-to-figure/table association - Footnote linking - Element merge suggestions
Example
from omnidocs.tasks.reading_order import RuleBasedReadingOrderPredictor
from omnidocs.tasks.layout_extraction import DocLayoutYOLO, DocLayoutYOLOConfig
from omnidocs.tasks.ocr_extraction import EasyOCR, EasyOCRConfig
# Initialize components
layout_extractor = DocLayoutYOLO(config=DocLayoutYOLOConfig())
ocr = EasyOCR(config=EasyOCRConfig())
predictor = RuleBasedReadingOrderPredictor()
# Process document
layout = layout_extractor.extract(image)
ocr_result = ocr.extract(image)
reading_order = predictor.predict(layout, ocr_result)
# Get text in reading order
text = reading_order.get_full_text()
Initialize the reading order predictor.
Source code in omnidocs/tasks/reading_order/rule_based/predictor.py
predict
¶
predict(
layout: LayoutOutput,
ocr: Optional[OCROutput] = None,
page_no: int = 0,
) -> ReadingOrderOutput
Predict reading order for a single page.
| PARAMETER | DESCRIPTION |
|---|---|
layout
|
Layout detection results with bounding boxes
TYPE:
|
ocr
|
Optional OCR results for text content
TYPE:
|
page_no
|
Page number (for multi-page documents)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReadingOrderOutput
|
ReadingOrderOutput with ordered elements and associations |
Source code in omnidocs/tasks/reading_order/rule_based/predictor.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
base
¶
Base class for reading order predictors.
Defines the abstract interface that all reading order predictors must implement.
BaseReadingOrderPredictor
¶
Bases: ABC
Abstract base class for reading order predictors.
Reading order predictors take layout detection and OCR results and produce a properly ordered sequence of document elements.
Example
predict
abstractmethod
¶
predict(
layout: LayoutOutput,
ocr: Optional[OCROutput] = None,
page_no: int = 0,
) -> ReadingOrderOutput
Predict reading order for a single page.
| PARAMETER | DESCRIPTION |
|---|---|
layout
|
Layout detection results with bounding boxes
TYPE:
|
ocr
|
Optional OCR results. If provided, text will be matched to layout elements by bbox overlap.
TYPE:
|
page_no
|
Page number (for multi-page documents)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReadingOrderOutput
|
ReadingOrderOutput with ordered elements and associations |
Example
Source code in omnidocs/tasks/reading_order/base.py
predict_multi_page
¶
predict_multi_page(
layouts: List[LayoutOutput],
ocrs: Optional[List[OCROutput]] = None,
) -> List[ReadingOrderOutput]
Predict reading order for multiple pages.
| PARAMETER | DESCRIPTION |
|---|---|
layouts
|
List of layout results, one per page
TYPE:
|
ocrs
|
Optional list of OCR results, one per page
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[ReadingOrderOutput]
|
List of ReadingOrderOutput, one per page |
Source code in omnidocs/tasks/reading_order/base.py
models
¶
Pydantic models for reading order prediction.
Takes layout detection and OCR results, produces ordered element sequence with caption and footnote associations.
Example
# Get layout and OCR
layout = layout_extractor.extract(image)
ocr = ocr_extractor.extract(image)
# Predict reading order
reading_order = predictor.predict(layout, ocr)
# Iterate in reading order
for element in reading_order.ordered_elements:
print(f"{element.index}: [{element.element_type}] {element.text[:50]}...")
# Get caption associations
for fig_id, caption_ids in reading_order.caption_map.items():
print(f"Figure {fig_id} has captions: {caption_ids}")
ElementType
¶
Bases: str, Enum
Type of document element for reading order.
BoundingBox
¶
Bases: BaseModel
Bounding box in pixel coordinates.
to_list
¶
from_list
classmethod
¶
Create from [x1, y1, x2, y2] list.
Source code in omnidocs/tasks/reading_order/models.py
to_normalized
¶
Convert to normalized coordinates (0-1024 range).
| PARAMETER | DESCRIPTION |
|---|---|
image_width
|
Original image width in pixels
TYPE:
|
image_height
|
Original image height in pixels
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BoundingBox
|
New BoundingBox with coordinates in 0-1024 range |
Source code in omnidocs/tasks/reading_order/models.py
OrderedElement
¶
Bases: BaseModel
A document element with its reading order position.
Combines layout detection results with OCR text and assigns a reading order index.
to_dict
¶
Convert to dictionary representation.
Source code in omnidocs/tasks/reading_order/models.py
ReadingOrderOutput
¶
Bases: BaseModel
Complete reading order prediction result.
Provides: - Ordered list of document elements - Caption-to-element associations - Footnote-to-element associations - Merge suggestions for split elements
Example
get_full_text
¶
Get concatenated text in reading order.
Excludes page headers, footers, captions, and footnotes from main text flow.
Source code in omnidocs/tasks/reading_order/models.py
get_elements_by_type
¶
get_captions_for
¶
Get caption elements for a given element ID.
Source code in omnidocs/tasks/reading_order/models.py
get_footnotes_for
¶
Get footnote elements for a given element ID.
Source code in omnidocs/tasks/reading_order/models.py
to_dict
¶
Convert to dictionary representation.
Source code in omnidocs/tasks/reading_order/models.py
save_json
¶
Save to JSON file.
load_json
classmethod
¶
rule_based
¶
Rule-based reading order predictor module.
Provides rule-based reading order prediction using spatial analysis.
RuleBasedReadingOrderPredictor
¶
Bases: BaseReadingOrderPredictor
Rule-based reading order predictor using spatial analysis.
Uses R-tree spatial indexing and rule-based algorithms to determine the logical reading sequence of document elements. This is a CPU-only implementation that doesn't require GPU resources.
Features: - Multi-column layout detection - Header/footer separation - Caption-to-figure/table association - Footnote linking - Element merge suggestions
Example
from omnidocs.tasks.reading_order import RuleBasedReadingOrderPredictor
from omnidocs.tasks.layout_extraction import DocLayoutYOLO, DocLayoutYOLOConfig
from omnidocs.tasks.ocr_extraction import EasyOCR, EasyOCRConfig
# Initialize components
layout_extractor = DocLayoutYOLO(config=DocLayoutYOLOConfig())
ocr = EasyOCR(config=EasyOCRConfig())
predictor = RuleBasedReadingOrderPredictor()
# Process document
layout = layout_extractor.extract(image)
ocr_result = ocr.extract(image)
reading_order = predictor.predict(layout, ocr_result)
# Get text in reading order
text = reading_order.get_full_text()
Initialize the reading order predictor.
Source code in omnidocs/tasks/reading_order/rule_based/predictor.py
predict
¶
predict(
layout: LayoutOutput,
ocr: Optional[OCROutput] = None,
page_no: int = 0,
) -> ReadingOrderOutput
Predict reading order for a single page.
| PARAMETER | DESCRIPTION |
|---|---|
layout
|
Layout detection results with bounding boxes
TYPE:
|
ocr
|
Optional OCR results for text content
TYPE:
|
page_no
|
Page number (for multi-page documents)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReadingOrderOutput
|
ReadingOrderOutput with ordered elements and associations |
Source code in omnidocs/tasks/reading_order/rule_based/predictor.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
predictor
¶
Rule-based reading order predictor.
Uses spatial analysis and R-tree indexing to determine the logical reading sequence of document elements. Self-contained implementation without external dependencies on docling-ibm-models.
Based on the algorithm from docling-ibm-models, adapted for omnidocs.
RuleBasedReadingOrderPredictor
¶
Bases: BaseReadingOrderPredictor
Rule-based reading order predictor using spatial analysis.
Uses R-tree spatial indexing and rule-based algorithms to determine the logical reading sequence of document elements. This is a CPU-only implementation that doesn't require GPU resources.
Features: - Multi-column layout detection - Header/footer separation - Caption-to-figure/table association - Footnote linking - Element merge suggestions
Example
from omnidocs.tasks.reading_order import RuleBasedReadingOrderPredictor
from omnidocs.tasks.layout_extraction import DocLayoutYOLO, DocLayoutYOLOConfig
from omnidocs.tasks.ocr_extraction import EasyOCR, EasyOCRConfig
# Initialize components
layout_extractor = DocLayoutYOLO(config=DocLayoutYOLOConfig())
ocr = EasyOCR(config=EasyOCRConfig())
predictor = RuleBasedReadingOrderPredictor()
# Process document
layout = layout_extractor.extract(image)
ocr_result = ocr.extract(image)
reading_order = predictor.predict(layout, ocr_result)
# Get text in reading order
text = reading_order.get_full_text()
Initialize the reading order predictor.
Source code in omnidocs/tasks/reading_order/rule_based/predictor.py
predict
¶
predict(
layout: LayoutOutput,
ocr: Optional[OCROutput] = None,
page_no: int = 0,
) -> ReadingOrderOutput
Predict reading order for a single page.
| PARAMETER | DESCRIPTION |
|---|---|
layout
|
Layout detection results with bounding boxes
TYPE:
|
ocr
|
Optional OCR results for text content
TYPE:
|
page_no
|
Page number (for multi-page documents)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReadingOrderOutput
|
ReadingOrderOutput with ordered elements and associations |
Source code in omnidocs/tasks/reading_order/rule_based/predictor.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |