Queue OCR & ASR work
This commit is contained in:
@@ -2,8 +2,26 @@ import {createWorker} from 'tesseract.js';
|
||||
import {AbortablePromise, Ai} from './ai.ts';
|
||||
|
||||
export class Vision {
|
||||
private worker: any = null;
|
||||
private queue: Array<{ path: string, resolve: any, reject: any }> = [];
|
||||
private busy = false;
|
||||
|
||||
constructor(private ai: Ai) { }
|
||||
constructor(private ai: Ai) {}
|
||||
|
||||
private async processQueue() {
|
||||
if(this.busy || !this.queue.length) return;
|
||||
this.busy = true;
|
||||
const job = this.queue.shift()!;
|
||||
if(!this.worker) this.worker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path});
|
||||
try {
|
||||
const {data} = await this.worker.recognize(job.path);
|
||||
job.resolve(data.text.trim() || null);
|
||||
} catch(err) {
|
||||
job.reject(err);
|
||||
}
|
||||
this.busy = false;
|
||||
this.processQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert image to text using Optical Character Recognition
|
||||
@@ -11,13 +29,16 @@ export class Vision {
|
||||
* @returns {AbortablePromise<string | null>} Promise of extracted text with abort method
|
||||
*/
|
||||
ocr(path: string): AbortablePromise<string | null> {
|
||||
let worker: any;
|
||||
const p = new Promise<string | null>(async res => {
|
||||
worker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path});
|
||||
const {data} = await worker.recognize(path);
|
||||
await worker.terminate();
|
||||
res(data.text.trim() || null);
|
||||
let aborted = false;
|
||||
const abort = () => { aborted = true; };
|
||||
const p = new Promise<string | null>((resolve, reject) => {
|
||||
this.queue.push({
|
||||
path,
|
||||
resolve: (text: string | null) => !aborted && resolve(text),
|
||||
reject: (err: Error) => !aborted && reject(err)
|
||||
});
|
||||
this.processQueue();
|
||||
});
|
||||
return Object.assign(p, {abort: () => worker?.terminate()});
|
||||
return Object.assign(p, {abort});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user