Skip to main content
Glama
fast-scan

fast-scan

by edjan11
README.md
# fast-scan

**Fast document digitization from photos.** Point a phone or webcam at a page and get back a clean, flat, correctly rotated scan — plus its text. Use it as a Python library, a CLI, an HTTP API or an **MCP server** for AI assistants.

[![CI](https://github.com/edjan11/fast-scan/actions/workflows/ci.yml/badge.svg)](https://github.com/edjan11/fast-scan/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
![Python](https://img.shields.io/badge/python-3.10%2B-blue)

## Why

Digitizing paper is usually slow and error-prone: someone takes a photo, crops it by hand, finds out later that it was blurry, and retypes what the OCR got wrong. fast-scan targets three outcomes:

1. **No retakes discovered too late:** the quality check says *at capture time* what is wrong (blur, glare, a hand in the frame).
2. **No manual cropping or rotating:** the page comes out flat, upright and clean.
3. **Fits into any workflow:** the same engine works as a library, a CLI, an HTTP API or an AI-assistant tool (MCP).

### Design principles

- **Never guess silently.** When orientation or quality is uncertain, the result says "needs review" instead of applying a wrong fix.
- **Private by default.** Nothing is stored; the vision-LLM backend is opt-in and can run fully local.
- **Works on modest hardware.** CPU-only engines first; heavy models are optional extras.

## Features

- **Page detection & perspective correction:** finds the sheet in a photo and flattens it.
- **Shadow removal:** turns uneven lighting into evenly white paper.
- **Auto-rotation:** detects 0/90/180/270° from the text layout, Tesseract OSD or OCR readability. When it isn't sure, it says so instead of guessing.
- **Quality checks:** blur, exposure, glare, framing, blank pages and hands covering the page, each with a human-readable reason to retake.
- **Hands-free capture:** `AutoCapture` watches a camera feed and fires once per page when it is sharp, framed and still, and never captures the same page twice.
- **Pluggable OCR:** Tesseract, RapidOCR, EasyOCR or any OpenAI-compatible **vision LLM** (Ollama, vLLM, LM Studio…).
- **PDF:** many photos in, one optimized PDF out.
- **Private by default:** the API processes everything in memory and stores nothing.

## How it works

```mermaid
flowchart LR
    A[Photo] --> B[Quality check]
    B --> C[Detect page<br/>+ fix perspective]
    C --> D[Remove shadows]
    D --> E[Auto-rotate]
    E --> F[Optimized JPEG / PDF]
    E --> G[OCR engine]
    G --> H[Text + confidence]
```

## Install

```bash
pip install "fast-scan[rapidocr] @ git+https://github.com/edjan11/fast-scan"
```

| Extra | Adds |
|---|---|
| `rapidocr` | RapidOCR (ONNX, fast on CPU, no system binary needed). **Recommended.** |
| `easyocr` | EasyOCR (deep learning, better on handwriting; heavier) |
| `vlm` | Vision-LLM backend over an OpenAI-compatible API |
| `all` | All of the above |

For Tesseract, install the binary ([instructions](https://tesseract-ocr.github.io/tessdoc/Installation.html)). fast-scan finds it on `PATH`.

## Usage

### CLI

```bash
fastscan scan photo.jpg                     # -> photo_scan.jpg
fastscan scan *.jpg --pdf document.pdf      # many photos -> one PDF
fastscan scan receipt.jpg --ocr             # print the text
fastscan engines                            # which OCR engines are installed
```

### Python

```python
from fastscan import process_image, ScanOptions

result = process_image("photo.jpg", ScanOptions(ocr=True))
open("clean.jpg", "wb").write(result.jpeg)
print(result.quality.status, result.quality.reasons)
print(result.text)
```

### HTTP API

```bash
fastscan serve --port 8080      # interactive docs at http://127.0.0.1:8080/docs
```

| Method | Path | Description |
|---|---|---|
| `POST` | `/scan` | One photo → JSON (image as base64), `output=jpeg` or `output=pdf` |
| `POST` | `/scan/batch` | Many photos → one PDF (or JSON report) |
| `POST` | `/ocr` | Text only |
| `GET` | `/engines` | Installed OCR engines |
| `GET` | `/health` | Health check |

```bash
curl -F file=@photo.jpg -F ocr=true http://127.0.0.1:8080/scan
```

### MCP server (Claude Desktop, Claude Code, other MCP clients)

The server exposes the tools `scan_document`, `extract_text`, `enhance_image`, `check_quality`, `images_to_pdf` and `list_engines`.

```json
{
  "mcpServers": {
    "fast-scan": {
      "command": "fastscan",
      "args": ["mcp"],
      "env": { "FASTSCAN_OCR_LANG": "eng" }
    }
  }
}
```

With Claude Code: `claude mcp add fast-scan -- fastscan mcp`

Then just ask: *"Scan ~/Desktop/contract.jpg and give me the text"*.

## OCR engines

| Engine | Best for | Needs |
|---|---|---|
| `rapidocr` | Photos of printed documents, fast on CPU | `pip` only |
| `tesseract` | Clean printed pages, many languages, orientation detection (OSD) | system binary |
| `easyocr` | Noisy photos and some handwriting (slower) | PyTorch |
| `vlm` | Hard cases: handwriting, complex layouts | a vision-LLM server |

`auto` picks the first installed engine, in this order: rapidocr, tesseract, easyocr, vlm.

## Configuration

All settings are environment variables (see [`.env.example`](.env.example)):

| Variable | Default | Description |
|---|---|---|
| `FASTSCAN_OCR_LANG` | `eng` | Tesseract language(s), e.g. `por+eng` |
| `FASTSCAN_TESSERACT_CMD` | *(PATH)* | Path to the Tesseract binary |
| `FASTSCAN_VLM_URL` | | OpenAI-compatible base URL, e.g. `http://localhost:11434/v1` |
| `FASTSCAN_VLM_MODEL` | | e.g. `qwen2.5vl:7b` |
| `FASTSCAN_VLM_API_KEY` | | Only if your endpoint needs one |
| `FASTSCAN_MAX_UPLOAD_MB` | `25` | API upload limit |
| `FASTSCAN_JPEG_QUALITY` | `90` | Output JPEG quality |

## Roadmap

Planned work is tracked as [issues](https://github.com/edjan11/fast-scan/issues), each written as a user story with acceptance criteria:

- [ ] Web capture page (phone camera → API) using `AutoCapture`
- [ ] Two-page spread splitting (open books)
- [ ] Searchable PDF (text layer)
- [ ] Docker image

## Development

```bash
git clone https://github.com/edjan11/fast-scan && cd fast-scan
pip install -e ".[dev,rapidocr]"
pytest
```

The test suite uses only synthetic images generated at runtime. See [CONTRIBUTING.md](CONTRIBUTING.md).

## License

[MIT](LICENSE) © 2026 Edjan

---

## Português

**fast-scan** digitaliza documentos a partir de fotos. Ele recorta a página, corrige a perspectiva e a rotação, remove sombras, avalia a qualidade da foto e extrai o texto por OCR. Dá para usar como biblioteca Python, pela linha de comando, por uma API HTTP ou como servidor MCP para assistentes de IA. Para português, use `FASTSCAN_OCR_LANG=por`. Contribuições são bem-vindas!