Models¶
Pydantic models for layout extraction outputs.
Defines standardized output types and label enums for layout detection.
Coordinate Systems
- Absolute (default): Coordinates in pixels relative to original image size
- Normalized (0-1024): Coordinates scaled to 0-1024 range (virtual 1024x1024 canvas)
Use bbox.to_normalized(width, height) or output.get_normalized_bboxes()
to convert to normalized coordinates.
Example
LayoutLabel
¶
Bases: str, Enum
Standardized layout labels used across all layout extractors.
These provide a consistent vocabulary regardless of which model is used.
CustomLabel
¶
Bases: BaseModel
Type-safe custom layout label definition for VLM-based models.
VLM models like Qwen3-VL support flexible custom labels beyond the standard LayoutLabel enum. Use this class to define custom labels with validation.
Example
from omnidocs.tasks.layout_extraction import CustomLabel
# Simple custom label
code_block = CustomLabel(name="code_block")
# With metadata
sidebar = CustomLabel(
name="sidebar",
description="Secondary content panel",
color="#9B59B6",
)
# Use with QwenLayoutDetector
result = detector.extract(image, custom_labels=[code_block, sidebar])
LabelMapping
¶
Base class for model-specific label mappings.
Each model maps its native labels to standardized LayoutLabel values.
Initialize label mapping.
| PARAMETER | DESCRIPTION |
|---|---|
mapping
|
Dict mapping model-specific labels to LayoutLabel enum values
TYPE:
|
Source code in omnidocs/tasks/layout_extraction/models.py
BoundingBox
¶
Bases: BaseModel
Bounding box coordinates in pixel space.
Coordinates follow the convention: (x1, y1) is top-left, (x2, y2) is bottom-right.
to_list
¶
to_xyxy
¶
to_xywh
¶
from_list
classmethod
¶
Create from [x1, y1, x2, y2] list.
Source code in omnidocs/tasks/layout_extraction/models.py
to_normalized
¶
Convert to normalized coordinates (0-1024 range).
Scales coordinates from absolute pixel values to a virtual 1024x1024 canvas. This provides consistent coordinates regardless of original image size.
| 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 |
Example
Source code in omnidocs/tasks/layout_extraction/models.py
to_absolute
¶
Convert from normalized (0-1024) to absolute pixel coordinates.
| PARAMETER | DESCRIPTION |
|---|---|
image_width
|
Target image width in pixels
TYPE:
|
image_height
|
Target image height in pixels
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BoundingBox
|
New BoundingBox with absolute pixel coordinates |
Source code in omnidocs/tasks/layout_extraction/models.py
LayoutBox
¶
Bases: BaseModel
Single detected layout element with label, bounding box, and confidence.
to_dict
¶
Convert to dictionary representation.
Source code in omnidocs/tasks/layout_extraction/models.py
get_normalized_bbox
¶
Get bounding box in normalized (0-1024) coordinates.
| PARAMETER | DESCRIPTION |
|---|---|
image_width
|
Original image width
TYPE:
|
image_height
|
Original image height
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BoundingBox
|
BoundingBox with normalized coordinates |
Source code in omnidocs/tasks/layout_extraction/models.py
LayoutOutput
¶
Bases: BaseModel
Complete layout extraction results for a single image.
filter_by_label
¶
filter_by_confidence
¶
to_dict
¶
Convert to dictionary representation.
Source code in omnidocs/tasks/layout_extraction/models.py
sort_by_position
¶
Return a new LayoutOutput with boxes sorted by position.
| PARAMETER | DESCRIPTION |
|---|---|
top_to_bottom
|
If True, sort by y-coordinate (reading order)
TYPE:
|
Source code in omnidocs/tasks/layout_extraction/models.py
get_normalized_bboxes
¶
Get all bounding boxes in normalized (0-1024) coordinates.
| RETURNS | DESCRIPTION |
|---|---|
List[Dict]
|
List of dicts with normalized bbox coordinates and metadata. |
Example
Source code in omnidocs/tasks/layout_extraction/models.py
visualize
¶
visualize(
image: Image,
output_path: Optional[Union[str, Path]] = None,
show_labels: bool = True,
show_confidence: bool = True,
line_width: int = 3,
font_size: int = 12,
) -> Image.Image
Visualize layout detection results on the image.
Draws bounding boxes with labels and confidence scores on the image. Each layout category has a distinct color for easy identification.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
PIL Image to draw on (will be copied, not modified)
TYPE:
|
output_path
|
Optional path to save the visualization
TYPE:
|
show_labels
|
Whether to show label text
TYPE:
|
show_confidence
|
Whether to show confidence scores
TYPE:
|
line_width
|
Width of bounding box lines
TYPE:
|
font_size
|
Size of label text (note: uses default font)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Image
|
PIL Image with visualizations drawn |
Example
Source code in omnidocs/tasks/layout_extraction/models.py
load_json
classmethod
¶
Load a LayoutOutput instance from a JSON file.
Reads a JSON file and deserializes its contents into a LayoutOutput object. Uses Pydantic's model_validate_json for proper handling of nested objects.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to JSON file containing serialized LayoutOutput data. Can be string or pathlib.Path object.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LayoutOutput
|
Deserialized layout output instance from file.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
UnicodeDecodeError
|
If file cannot be decoded as UTF-8. |
ValueError
|
If file contents are not valid JSON. |
ValidationError
|
If JSON data doesn't match LayoutOutput schema. |
Example
Found 5 elementsSource code in omnidocs/tasks/layout_extraction/models.py
save_json
¶
Save LayoutOutput instance to a JSON file.
Serializes the LayoutOutput object to JSON and writes it to a file. Automatically creates parent directories if they don't exist. Uses UTF-8 encoding for compatibility and proper handling of special characters.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path where JSON file should be saved. Can be string or pathlib.Path object. Parent directories will be created if they don't exist.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If file cannot be written due to permission or disk errors. |
TypeError
|
If file_path is not a string or Path object. |