Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 69b3297bb3 | |||
| 710c6ce52c | |||
| 4ac3036000 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "1.0.4",
|
"version": "1.0.6",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -12,12 +12,31 @@ export class Vision {
|
|||||||
*/
|
*/
|
||||||
ocr(path: string): AbortablePromise<string | null> {
|
ocr(path: string): AbortablePromise<string | null> {
|
||||||
let worker: any;
|
let worker: any;
|
||||||
|
let reject: (err: any) => void;
|
||||||
|
|
||||||
|
const handler = (err: Error) => {
|
||||||
|
if(err.stack?.includes('tesseract.js')) {
|
||||||
|
process.off('uncaughtException', handler);
|
||||||
|
reject?.(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
};
|
||||||
|
process.on('uncaughtException', handler);
|
||||||
|
|
||||||
const p = (async () => {
|
const p = (async () => {
|
||||||
worker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path});
|
worker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path});
|
||||||
const {data} = await worker.recognize(path);
|
return await new Promise<string | null>((res, rej) => {
|
||||||
await worker.terminate();
|
reject = rej;
|
||||||
return data.text.trim() || null;
|
worker.recognize(path)
|
||||||
})().finally(() => worker?.terminate());
|
.then(({data}: any) => res(data.text.trim() || null))
|
||||||
|
.catch(rej);
|
||||||
|
});
|
||||||
|
})().finally(() => {
|
||||||
|
process.off('uncaughtException', handler);
|
||||||
|
worker?.terminate();
|
||||||
|
});
|
||||||
|
|
||||||
return Object.assign(p, {abort: () => worker?.terminate()});
|
return Object.assign(p, {abort: () => worker?.terminate()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user