From 4438613c2d0cb5c770cc3fbebad49f90cd34235e Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sat, 8 Feb 2025 23:07:57 +0530 Subject: [PATCH] feat: add error handling to OCR image processing function --- src/utils/ocr.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/utils/ocr.ts b/src/utils/ocr.ts index 7b43b74..59d1ed1 100644 --- a/src/utils/ocr.ts +++ b/src/utils/ocr.ts @@ -1,18 +1,23 @@ import { createWorker } from "tesseract.js" export async function processImageForOCR(imageData: string): Promise { - const isOCROffline = import.meta.env.BROWSER === "edge" - const worker = await createWorker(!isOCROffline ? "eng-fast" : "eng", undefined, { - workerPath: "/ocr/worker.min.js", - workerBlobURL: false, - corePath: "/ocr/tesseract-core-simd.js", - errorHandler: (e) => console.error(e), - langPath: !isOCROffline ? "/ocr/lang" : undefined - }) + try { + const isOCROffline = import.meta.env.BROWSER === "edge" + const worker = await createWorker(!isOCROffline ? "eng-fast" : "eng", undefined, { + workerPath: "/ocr/worker.min.js", + workerBlobURL: false, + corePath: "/ocr/tesseract-core-simd.js", + errorHandler: (e) => console.error(e), + langPath: !isOCROffline ? "/ocr/lang" : undefined + }) - const result = await worker.recognize(imageData) + const result = await worker.recognize(imageData) - await worker.terminate() + await worker.terminate() - return result.data.text + return result.data.text + } catch (error) { + console.error("Error processing image for OCR:", error) + return "" + } }