ffocr
v0.1.11
Published
Browser-first PaddleOCR wrapper powered by ONNX Runtime Web with automatic WebGPU/WASM selection.
Downloads
842
Maintainers
Readme
ffocr
Browser OCR for frontend apps using PaddleOCR models converted to ONNX.
npm: https://www.npmjs.com/package/ffocr
Live Demo: https://zxc88645.github.io/ffocr/demo/
Install
npm install ffocrQuick start
If you want the built-in hosted model:
import { createDefaultPPOcrV5 } from "ffocr";
const ocr = createDefaultPPOcrV5();
const result = await ocr.ocr(fileOrBlobOrUrl);
console.log(result.text);If you want to host the model files yourself:
import { createPPOcrV5 } from "ffocr";
const ocr = createPPOcrV5({
baseUrl: "https://your-cdn.example.com/models/pp-ocrv5"
});
const result = await ocr.ocr(fileOrBlobOrUrl);
console.log(result.text);Model variant
PP-OCRv5 ships two model variants: mobile (default) and server. The mobile variant is smaller and faster, while server offers higher accuracy at a larger size.
// Uses the default mobile model
const ocr = createDefaultPPOcrV5();
// Switch to the server model
const ocr = createDefaultPPOcrV5({ modelVariant: "server" });The same option works with a custom baseUrl:
const ocr = createPPOcrV5({
baseUrl: "https://your-cdn.example.com/models/pp-ocrv5",
modelVariant: "server"
});Model caching
Enable cacheModels to persist downloaded models in the browser's Cache API. Subsequent page loads will read from cache instead of re-downloading:
const ocr = createDefaultPPOcrV5({ cacheModels: true });Progress callback
Track loading, download, and inference progress via onProgress:
const result = await ocr.ocr(file, {
onProgress({ phase, current, total, loaded, totalBytes }) {
// phase: "loading_dictionary" | "loading_detection_model"
// | "loading_recognition_model" | "warmup"
// | "preprocessing" | "detecting" | "recognizing"
if (loaded != null && totalBytes != null) {
const pct = Math.round((loaded / totalBytes) * 100);
console.log(`${phase}: ${pct}%`);
} else if (phase === "recognizing") {
console.log(`Recognizing ${current}/${total}`);
} else {
console.log(phase);
}
}
});Main API
createDefaultPPOcrV5(options?)
Creates an OCR instance using the package default hosted model URL.
ocrWithDefaultPPOcrV5(source, options?, ocrOptions?)
One-shot OCR helper using the package default hosted model URL.
createPPOcrV5({ baseUrl, ...options })
Recommended when you want full control over model hosting.
ocrWithPPOcrV5(source, options, ocrOptions?)
One-shot OCR helper for a custom baseUrl.
createPaddleOcr({ manifest, ...options })
Lower-level API for custom manifests or model paths.
Required model files
Your baseUrl should point to a folder that contains the detection and recognition ONNX pair for the variant you use (mobile by default):
- Mobile:
det_mobile.onnx,rec_mobile.onnx - Server:
det_server.onnx,rec_server.onnx
Override filenames with detectionModelPath / recognitionModelPath if your layout differs.
Dictionary: By default the dictionary is loaded from the PaddleOCR project (not from baseUrl). To self-host it, set dictionaryUrl to your ppocrv5_dict.txt URL.
Default model URL
createDefaultPPOcrV5 loads weights from:
https://zxc88645.github.io/ffocr/models/pp-ocrv5Use createPPOcrV5 with your own baseUrl when you need to self-host or pin assets.
The bundled conversion flow patches the generated ONNX files so the default PP-OCRv5 models stay compatible with ONNX Runtime WebGPU.
Supported inputs
ImageDataImageBitmapHTMLImageElementHTMLCanvasElementOffscreenCanvasBlobstringURL
Runtime
- Detects available ONNX Runtime Web execution providers including
webgpu,webnn,webgl, andwasm - In
automode, picks the first available provider in this order:webgpu->webnn->webgl->wasm
Extra docs
- Model conversion:
docs/MODEL_CONVERSION.md - Example app:
examples/vite-demo
