Notes/client/src/lib/ocr.ts
Claudio Schaad 7107846624 Fix OCR dropping German umlauts by loading the German model
Tesseract only loaded the English model, which has no ä/ö/ü/ß, so they were
misread as plain letters. Load deu+eng and normalize output to NFC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 17:00:01 +01:00

25 lines
688 B
TypeScript

let workerPromise: Promise<import("tesseract.js").Worker> | null = null;
function getWorker() {
if (!workerPromise) {
// "deu" is needed for umlauts and ß; the English model can't produce them.
workerPromise = import("tesseract.js").then(({ createWorker }) => createWorker("deu+eng"));
}
return workerPromise;
}
function cleanUpText(raw: string): string {
return raw
.normalize("NFC")
.replace(/[ \t]+\n/g, "\n")
.replace(/\n{3,}/g, "\n\n")
.trim();
}
export async function recognizeText(image: Blob): Promise<string> {
const worker = await getWorker();
const {
data: { text }
} = await worker.recognize(image);
return cleanUpText(text);
}