Skip to main content
Glama
README.md
# vlm-ocr

An MCP server that OCRs scanned PDF pages (or image files) with the cheapest engine that can do the job: Tesseract first, a vision model only when needed.

- **Free orientation fix.** Tesseract OSD (`--psm 0`) straightens rotated pages. It is used only when its confidence is at least 5; otherwise the page goes to the vision model unrotated and the model orients it.
- **Engine per call:** `auto | tesseract | local | cloud`. `auto` runs Tesseract and escalates to the cloud model when mean word confidence is below 80 or the page looks like a dense numeric table (at least 45% numeric tokens).
- **Manual hooks:** force any engine, and force rotation with `rotate = auto | 0 | 90 | 180 | 270` (degrees clockwise).
- **Anti-hallucination:** vision models fail silently with clean, plausible, wrong numbers. `ocr_reconcile` reads each page twice and reports only the disagreements. Unreadable cells are marked `[ILLEGIBLE]`, never guessed.

Tools: `ocr_pdf_pages`, `ocr_reconcile`, `estimate_cost`, `list_vision_models`.

## Install

```
sudo dnf install tesseract            # or: apt install tesseract-ocr
pip install -r requirements.txt
```

OSD needs `osd.traineddata`, which some distro packages omit. Download it from the tessdata project and point `TESSDATA_PREFIX` at the folder holding it (it must also hold `eng.traineddata`). Without it, rotation falls back to the vision model.

Register the server in your MCP client; see `examples/mcp-config.json`.

## Configuration (environment variables)

| variable | default | meaning |
|---|---|---|
| `TESSDATA_PREFIX` | Tesseract default | folder with `osd.traineddata` and the language files |
| `TESSERACT_CMD` | `tesseract` | Tesseract binary |
| `VLM_OCR_TESS_LANG` | `eng` | Tesseract language(s), e.g. `eng+urd` |
| `VLM_OCR_OSD_MIN_CONF` | `5` | trust OSD only at or above this confidence |
| `VLM_OCR_CONF_THRESHOLD` | `80` | `auto` escalates below this mean word confidence |
| `VLM_OCR_NUMERIC_RATIO` | `0.45` | numeric-token share that marks a dense numeric table |
| `CLOUD_API_URL` | `https://openrouter.ai/api/v1` | any OpenAI-compatible base URL |
| `CLOUD_API_KEY` | | key for that endpoint |
| `CLOUD_API_KEY_FILE` | | alternative: a file holding the key (a local proxy token works too) |
| `VLM_OCR_MODEL` | `google/gemini-2.5-flash-lite` | cloud model id |
| `LOCAL_VLM_URL` | off | base URL of your own server, e.g. `http://localhost:8080/v1` |
| `LOCAL_VLM_MODEL` | | model name that server expects |
| `LOCAL_VLM_API_KEY` | `none` | key, if your server wants one |

## Engines

- **tesseract**: free, under 2.5 s per page on a laptop CPU, no network. Best on clean print.
- **cloud**: any OpenAI-compatible vision endpoint (OpenRouter by default). Best on faint print and hard tables.
- **local**: for people with a GPU. Serve a vision model with llama-server, vLLM, Ollama or LM Studio, then set `LOCAL_VLM_URL` and `LOCAL_VLM_MODEL`. Off until configured. Point it at **Qwen2.5-VL** or **DeepSeek-OCR**.
- **auto**: Tesseract, then cloud when unsure. With no cloud key set it returns the Tesseract text with a warning.

Example call: `ocr_pdf_pages(pdf_path="/scans/plan.pdf", pages="14", engine="auto", rotate="auto")`.

## Benchmark (indicative only)

Intel i7-8550U laptop, 8 threads, no GPU, 4 synthetic pages (a numeric table, a paragraph, a rotated table, a faint paragraph). Character error rate (CER), lower is better; the CER column reads table / paragraph / rotated / faint. The pages and reference text are in `tests/`.

| engine | s/page | peak RAM | CER |
|---|---|---|---|
| Tesseract, psm 6, no rotation fix | 0.5-2.2 | 70 MB | 0.000 / 0.000 / 1.506 / 0.354 |
| Tesseract after OSD rotation | +0.9 | 70 MB | 0.000 / 0.000 / 0.000 / 0.354 |
| Qwen2.5-VL-3B Q4 (llama.cpp, CPU) | 345-520 | 10.4 GB | 0.000 / 0.000 / 0.101 / 0.000 |
| granite-docling-258M Q8 (CPU) | 25-59 | 0.63 GB | 0.938 / 1.478 / 0.775 / 0.001 |

OSD was correct and confident (13-18) on the clean pages at every rotation, and unreliable on the faint page (0.3-0.8), hence the confidence gate. Four pages prove little; treat the numbers as a guide. On a GPU a vision model is far faster than the CPU times above: set `LOCAL_VLM_URL` to Qwen2.5-VL or DeepSeek-OCR.

## Tests

```
python -m unittest discover -s tests -v
```

The routing and OSD-gating tests need no network. One test runs a real OSD pass and skips if `osd.traineddata` is not available.

## Licence

MIT, see LICENSE. Check the licence of any model you serve locally.