TTS
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {createWorker} from 'tesseract.js';
|
||||
import {Ai} from './ai.ts';
|
||||
import {AbortablePromise, Ai} from './ai.ts';
|
||||
|
||||
export class Vision {
|
||||
|
||||
@@ -8,18 +8,16 @@ export class Vision {
|
||||
/**
|
||||
* Convert image to text using Optical Character Recognition
|
||||
* @param {string} path Path to image
|
||||
* @returns {{abort: Function, response: Promise<string | null>}} Abort function & Promise of extracted text
|
||||
* @returns {AbortablePromise<string | null>} Promise of extracted text with abort method
|
||||
*/
|
||||
ocr(path: string): {abort: () => void, response: Promise<string | null>} {
|
||||
ocr(path: string): AbortablePromise<string | null> {
|
||||
let worker: any;
|
||||
return {
|
||||
abort: () => { worker?.terminate(); },
|
||||
response: new Promise(async res => {
|
||||
worker = await createWorker(this.ai.options.tesseract?.model || 'eng', 2, {cachePath: this.ai.options.path});
|
||||
const {data} = await worker.recognize(path);
|
||||
await worker.terminate();
|
||||
res(data.text.trim() || null);
|
||||
})
|
||||
}
|
||||
const p = new Promise<string | null>(async res => {
|
||||
worker = await createWorker(this.ai.options.tesseract?.model || 'eng', 2, {cachePath: this.ai.options.path});
|
||||
const {data} = await worker.recognize(path);
|
||||
await worker.terminate();
|
||||
res(data.text.trim() || null);
|
||||
});
|
||||
return Object.assign(p, {abort: () => worker?.terminate()});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user