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>
25 lines
688 B
TypeScript
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);
|
|
}
|