TTS
All checks were successful
Publish Library / Build NPM Project (push) Successful in 49s
Publish Library / Tag Version (push) Successful in 16s

This commit is contained in:
2026-01-30 15:39:20 -05:00
parent d5bf1ec47e
commit 28904cddbe
10 changed files with 696 additions and 944 deletions

View File

@@ -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()});
}
}