# chirag4862/doclayout-yolo architecture

> Architecture diagram of the GitHub repository chirag4862/doclayout-yolo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chirag4862/doclayout-yolo
- Repository: https://github.com/chirag4862/doclayout-yolo

## Overview

DocLayout-YOLO turns an uploaded or CLI-supplied document image into detected layout regions, optional OCR text, and structured results; it can also render annotated images. The Streamlit and CLI entry points both initialize an ONNX session and invoke detection. Detection preprocesses the image, runs inference, and postprocesses boxes; output handling groups detections and optionally extracts text. The README also documents a Python API and configurable class mapping. Edges are limited to relationships shown in the excerpts or README; the model internals and unsampled implementation details are not inferred.

Main entry points: [`app.py`](https://github.com/chirag4862/doclayout-yolo/blob/main/app.py) (Interactive demo), [`scripts/run_detection.py`](https://github.com/chirag4862/doclayout-yolo/blob/main/scripts/run_detection.py) (CLI runner), [`layout_detector/session.py`](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/session.py) (ONNX session setup), [`layout_detector/detector.py`](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/detector.py) (Layout detector)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User entry points"]
  node_streamlit_app["Interactive demo<br/>[app.py]"]
  node_cli["CLI runner<br/>[run_detection.py]"]
end

subgraph group_inference["Layout inference"]
  node_detector["Layout detector<br/>[detector.py]"]
  node_onnx_runtime["ONNX Runtime"]
end

subgraph group_results["Output processing"]
  node_ocr_builder["OCR and JSON builder<br/>[utils.py]"]
  node_paddleocr["PaddleOCR"]
  node_annotator["Detection rendering<br/>[detector.py]"]
  node_json_writer["JSON file writer<br/>[utils.py]"]
  node_json_result["Structured JSON"]
  node_annotated_result["Annotated image"]
end

subgraph group_resources["Model resources"]
  node_session_factory["ONNX session setup<br/>[session.py]"]
  node_model["YOLO weights<br/>[best.onnx]"]
  node_class_map["Class mapping<br/>[metadata.yaml]"]
end

node_user(("Document user"))
node_cli_user(("CLI user"))

node_user -->|"uploads image"| node_streamlit_app
node_cli_user -->|"provides image"| node_cli
node_streamlit_app -->|"creates session"| node_session_factory
node_cli -->|"creates session"| node_session_factory
node_session_factory -->|"loads weights"| node_model
node_session_factory -->|"reads classes"| node_class_map
node_streamlit_app -->|"runs detection"| node_detector
node_cli -->|"runs detection"| node_detector
node_detector -->|"runs inference"| node_onnx_runtime
node_streamlit_app -->|"builds OCR output"| node_ocr_builder
node_cli -->|"builds OCR output"| node_ocr_builder
node_ocr_builder -->|"extracts text"| node_paddleocr
node_streamlit_app -.->|"initializes OCR"| node_paddleocr
node_cli -.->|"initializes OCR"| node_paddleocr
node_streamlit_app -->|"draws detections"| node_annotator
node_cli -->|"draws detections"| node_annotator
node_cli -->|"saves results"| node_json_writer
node_json_writer -->|"writes JSON"| node_json_result
node_streamlit_app -->|"offers download"| node_json_result
node_streamlit_app -->|"offers download"| node_annotated_result
node_cli -->|"saves image"| node_annotated_result

click node_streamlit_app "https://github.com/chirag4862/doclayout-yolo/blob/main/app.py"
click node_cli "https://github.com/chirag4862/doclayout-yolo/blob/main/scripts/run_detection.py"
click node_session_factory "https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/session.py"
click node_model "https://github.com/chirag4862/doclayout-yolo/blob/main/best.onnx"
click node_class_map "https://github.com/chirag4862/doclayout-yolo/blob/main/config/metadata.yaml"
click node_detector "https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/detector.py"
click node_ocr_builder "https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/utils.py"
click node_annotator "https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/detector.py"
click node_json_writer "https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/utils.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_streamlit_app,node_cli,node_user,node_cli_user toneBlue
class node_detector,node_onnx_runtime toneAmber
class node_ocr_builder,node_paddleocr,node_annotator,node_json_writer,node_json_result,node_annotated_result toneMint
class node_session_factory,node_model,node_class_map toneRose
```

## Components

### User entry points

- [Interactive demo](https://github.com/chirag4862/doclayout-yolo/blob/main/app.py): `app.py`
- [CLI runner](https://github.com/chirag4862/doclayout-yolo/blob/main/scripts/run_detection.py): `scripts/run_detection.py`

### Layout inference

- [Layout detector](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/detector.py): `layout_detector/detector.py`
- **ONNX Runtime**

### Output processing

- [OCR and JSON builder](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/utils.py): `layout_detector/utils.py`
- **PaddleOCR**
- [Detection rendering](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/detector.py): `layout_detector/detector.py`
- [JSON file writer](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/utils.py): `layout_detector/utils.py`
- **Structured JSON**
- **Annotated image**

### Model resources

- [ONNX session setup](https://github.com/chirag4862/doclayout-yolo/blob/main/layout_detector/session.py): `layout_detector/session.py`
- [YOLO weights](https://github.com/chirag4862/doclayout-yolo/blob/main/best.onnx): `best.onnx`
- [Class mapping](https://github.com/chirag4862/doclayout-yolo/blob/main/config/metadata.yaml): `config/metadata.yaml`

### Other components

- **Document user**
- **CLI user**

## Connections

- Document user → Interactive demo: uploads image (evidence: `app.py`)
- CLI user → CLI runner: provides image (evidence: `scripts/run_detection.py`)
- Interactive demo → ONNX session setup: creates session (evidence: `app.py`)
- CLI runner → ONNX session setup: creates session (evidence: `scripts/run_detection.py`)
- ONNX session setup → YOLO weights: loads weights (evidence: `layout_detector/session.py`)
- ONNX session setup → Class mapping: reads classes (evidence: `layout_detector/session.py`)
- Interactive demo → Layout detector: runs detection (evidence: `app.py`)
- CLI runner → Layout detector: runs detection (evidence: `scripts/run_detection.py`)
- Layout detector → ONNX Runtime: runs inference (evidence: `layout_detector/detector.py`)
- Interactive demo → OCR and JSON builder: builds OCR output (evidence: `app.py`)
- CLI runner → OCR and JSON builder: builds OCR output (evidence: `scripts/run_detection.py`)
- OCR and JSON builder → PaddleOCR: extracts text (evidence: `layout_detector/utils.py`)
- Interactive demo → PaddleOCR: initializes OCR (evidence: `app.py`)
- CLI runner → PaddleOCR: initializes OCR (evidence: `scripts/run_detection.py`)
- Interactive demo → Detection rendering: draws detections (evidence: `app.py`)
- CLI runner → Detection rendering: draws detections (evidence: `scripts/run_detection.py`)
- CLI runner → JSON file writer: saves results (evidence: `scripts/run_detection.py`)
- JSON file writer → Structured JSON: writes JSON (evidence: `layout_detector/utils.py`)
- Interactive demo → Structured JSON: offers download (evidence: `app.py`)
- Interactive demo → Annotated image: offers download (evidence: `app.py`)
- CLI runner → Annotated image: saves image (evidence: `scripts/run_detection.py`)
