# elango-p/ocr-text-extract-lib architecture

> Architecture diagram of the GitHub repository elango-p/ocr-text-extract-lib, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/elango-p/ocr-text-extract-lib
- Repository: https://github.com/elango-p/ocr-text-extract-lib

## Overview

ocr-text-extract is a browser-only OCR library that accepts an image, prepares alternate image passes, recognizes them with Tesseract.js, selects the strongest result, reconstructs readable text, and returns text or JSON. Its documented capabilities include modal focus, dark-image and single-line fallbacks, confidence-based cleaning, and multi-column layout reconstruction. The workflow is supported by the README and excerpts; unsampled implementation details are not inferred beyond those documented capabilities.

Main entry points: [`src/recognize.ts`](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/recognize.ts) (OCR orchestrator), [`src/scoring.ts`](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/scoring.ts) (Pass scoring), [`src/text-format.ts`](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts) (Confidence filtering), [`src/index.ts`](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/index.ts) (Public API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Public API and input"]
  node_api["Public API<br/>[index.ts]"]
  node_source["Image source resolver<br/>[image-source.ts]"]
end

subgraph group_image["Image preparation"]
  node_canvas["Scale and inspect<br/>[preprocess.ts]"]
  node_modal["Modal focus detection<br/>[preprocess.ts]"]
  node_passes["Image pass preparation<br/>[preprocess.ts]"]
end

subgraph group_recognition["Recognition and selection"]
  node_extract["OCR orchestrator<br/>[recognize.ts]"]
  node_score["Pass scoring<br/>[scoring.ts]"]
  node_metrics["OCR metrics<br/>[constants.ts]"]
end

subgraph group_results["Text and results"]
  node_clean["Confidence filtering<br/>[text-format.ts]"]
  node_layout["Layout reconstruction<br/>[text-format.ts]"]
  node_normalize["Text normalization<br/>[text-format.ts]"]
  node_format["Result formatting<br/>[output-format.ts]"]
end

node_user(("Library caller"))
node_browser["Browser image APIs"]
node_tesseract["Tesseract.js"]

node_user -->|"calls"| node_api
node_api -->|"exports"| node_extract
node_extract -->|"resolves input"| node_source
node_source -->|"reads files"| node_browser
node_extract -->|"scales image"| node_canvas
node_canvas -->|"uses canvas"| node_browser
node_extract -->|"detects focus"| node_modal
node_extract -->|"builds passes"| node_passes
node_extract -->|"runs recognition"| node_tesseract
node_extract -->|"scores passes"| node_score
node_score -->|"returns metrics"| node_metrics
node_score -->|"measures coherence"| node_normalize
node_extract -->|"formats OCR data"| node_clean
node_clean -->|"reconstructs layout"| node_layout
node_clean -->|"normalizes text"| node_normalize
node_extract -->|"builds result"| node_format
node_format -->|"returns output"| node_user

click node_api "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/index.ts"
click node_extract "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/recognize.ts"
click node_source "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/image-source.ts"
click node_canvas "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/preprocess.ts"
click node_modal "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/preprocess.ts"
click node_passes "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/preprocess.ts"
click node_score "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/scoring.ts"
click node_metrics "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/constants.ts"
click node_clean "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts"
click node_layout "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts"
click node_normalize "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts"
click node_format "https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/output-format.ts"

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_api,node_source,node_browser toneBlue
class node_canvas,node_modal,node_passes toneAmber
class node_extract,node_score,node_metrics toneMint
class node_clean,node_layout,node_normalize,node_format toneRose
class node_user,node_tesseract toneIndigo
```

## Components

### Public API and input

- [Public API](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/index.ts): `src/index.ts`
- [Image source resolver](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/image-source.ts): `src/image-source.ts`

### Image preparation

- [Scale and inspect](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/preprocess.ts): `src/preprocess.ts`
- [Modal focus detection](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/preprocess.ts): `src/preprocess.ts`
- [Image pass preparation](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/preprocess.ts): `src/preprocess.ts`

### Recognition and selection

- [OCR orchestrator](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/recognize.ts): `src/recognize.ts`
- [Pass scoring](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/scoring.ts): `src/scoring.ts`
- [OCR metrics](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/constants.ts): `src/constants.ts`

### Text and results

- [Confidence filtering](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts): `src/text-format.ts`
- [Layout reconstruction](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts): `src/text-format.ts`
- [Text normalization](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/text-format.ts): `src/text-format.ts`
- [Result formatting](https://github.com/elango-p/ocr-text-extract-lib/blob/main/src/output-format.ts): `src/output-format.ts`

### Other components

- **Library caller**
- **Browser image APIs**
- **Tesseract.js**

## Connections

- Library caller → Public API: calls (evidence: `README.md`)
- Public API → OCR orchestrator: exports (evidence: `src/index.ts`)
- OCR orchestrator → Image source resolver: resolves input (evidence: `src/recognize.ts`)
- Image source resolver → Browser image APIs: reads files (evidence: `src/image-source.ts`)
- OCR orchestrator → Scale and inspect: scales image (evidence: `src/recognize.ts`)
- Scale and inspect → Browser image APIs: uses canvas (evidence: `src/preprocess.ts`)
- OCR orchestrator → Modal focus detection: detects focus (evidence: `src/recognize.ts`)
- OCR orchestrator → Image pass preparation: builds passes (evidence: `src/recognize.ts`)
- OCR orchestrator → Tesseract.js: runs recognition (evidence: `src/recognize.ts`)
- OCR orchestrator → Pass scoring: scores passes (evidence: `src/recognize.ts`)
- Pass scoring → OCR metrics: returns metrics (evidence: `src/scoring.ts`)
- Pass scoring → Text normalization: measures coherence (evidence: `src/scoring.ts`)
- OCR orchestrator → Confidence filtering: formats OCR data (evidence: `src/recognize.ts`)
- Confidence filtering → Layout reconstruction: reconstructs layout (evidence: `src/text-format.ts`)
- Confidence filtering → Text normalization: normalizes text (evidence: `src/text-format.ts`)
- OCR orchestrator → Result formatting: builds result (evidence: `src/recognize.ts`)
- Result formatting → Library caller: returns output (evidence: `README.md`)
