feat: add error handling to OCR image processing function
This commit is contained in:
parent
ef51cf85ff
commit
4438613c2d
@ -1,18 +1,23 @@
|
||||
import { createWorker } from "tesseract.js"
|
||||
|
||||
export async function processImageForOCR(imageData: string): Promise<string> {
|
||||
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 ""
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user