This commit is contained in:
30
docs/snippets/wasm/cache/ocr-cache.ts
vendored
Normal file
30
docs/snippets/wasm/cache/ocr-cache.ts
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
import { extractBytes, initWasm, TesseractWasmBackend } from "@kreuzberg/wasm";
|
||||
|
||||
async function demonstrateOcrCaching() {
|
||||
await initWasm();
|
||||
|
||||
const backend = new TesseractWasmBackend();
|
||||
await backend.initialize();
|
||||
|
||||
console.log("Tesseract WASM backend loaded - models cached");
|
||||
|
||||
const imageBytes = new Uint8Array(await fetch("page1.png").then((r) => r.arrayBuffer()));
|
||||
|
||||
console.time("First OCR (with model load)");
|
||||
const _result1 = await extractBytes(imageBytes, "image/png", {
|
||||
ocr: { backend: "tesseract-wasm", language: "eng" },
|
||||
});
|
||||
console.timeEnd("First OCR (with model load)");
|
||||
|
||||
console.log("Model cached in memory");
|
||||
|
||||
const imageBytes2 = new Uint8Array(await fetch("page2.png").then((r) => r.arrayBuffer()));
|
||||
|
||||
console.time("Second OCR (model cached)");
|
||||
const _result2 = await extractBytes(imageBytes2, "image/png", {
|
||||
ocr: { backend: "tesseract-wasm", language: "eng" },
|
||||
});
|
||||
console.timeEnd("Second OCR (model cached)");
|
||||
}
|
||||
|
||||
demonstrateOcrCaching().catch(console.error);
|
||||
Reference in New Issue
Block a user