let workerPromise: Promise | 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 { const worker = await getWorker(); const { data: { text } } = await worker.recognize(image); return cleanUpText(text); }