ocr-text-extract
v1.0.0
Published
Browser OCR with smart preprocessing, multi-pass recognition, and layout reconstruction — powered by Tesseract.js
Maintainers
Readme
ocr-text-extract
Browser OCR with smart preprocessing, multi-pass recognition, and layout reconstruction — powered by Tesseract.js.
All processing runs in the browser. Images are never sent to a server.
Features
- Multi-pass OCR with automatic best-result selection
- Smart image preprocessing (contrast stretch, sharpening, adaptive threshold)
- Modal/dialog auto-focus for UI screenshots
- Multi-column layout reconstruction
- Clean mode with confidence-based word filtering
- Dark image and logo/single-line fallbacks
- 12+ language support via Tesseract
Install
npm install ocr-text-extract tesseract.jstesseract.js is a peer dependency — install it alongside this package.
Usage
import { extractTextFromImage } from "ocr-text-extract";
// From a File input
const file = document.querySelector("input[type=file]").files[0];
const result = await extractTextFromImage(file, {
language: "eng",
cleanMode: true,
output: "text", // or "json"
onProgress: (pct) => console.log(`${pct}%`),
});
console.log(result.text); // always plain extracted text
console.log(result.formatted); // text or JSON string, based on `output`
console.log(result.output); // "text" or "json"
console.log(result.confidence); // 0–100 score, or null if empty
console.log(result.passUsed); // e.g. "enhanced", "focused", "binary"Output formats
| output | result.formatted | result.text |
|----------|-------------------|---------------|
| "text" (default) | Plain extracted text | Same plain text |
| "json" | JSON string with text, confidence, passUsed, metrics | Plain text (always available) |
// JSON output example
const result = await extractTextFromImage(file, { output: "json" });
const data = JSON.parse(result.formatted);
// { text: "...", confidence: 87, passUsed: "enhanced", metrics: { ... } }Input types
extractTextFromImage accepts:
- Data URL string (
"data:image/png;base64,...") - File or Blob
Options
| Option | Default | Description |
|--------|---------|-------------|
| language | "eng" | Tesseract language code |
| cleanMode | false | Filter low-confidence words; best for screenshots |
| targetLongEdge | 2000 | Target image long edge when scaling |
| maxLongEdge | 3200 | Maximum long edge when scaling |
| ocrDpi | "300" | DPI hint passed to Tesseract |
| output | "text" | Result format: "text" or "json" |
| onProgress | — | Callback receiving 0–100 progress |
Lower-level API
Individual preprocessing and formatting helpers are exported for advanced use:
import {
loadScaledCanvas,
preprocessFromScaled,
detectModalRegion,
formatOcrOutput,
normalizeText,
scoreOcrResult,
SUPPORTED_LANGUAGES,
} from "ocr-text-extract";Browser support
Requires a modern browser with canvas, Image, and FileReader APIs. Designed for client-side use only.
Development
npm install
npm test
npm run buildLicense
MIT
