Files
ai-utils/src/vision.ts
ztimson 39537a4a8f
All checks were successful
Publish Library / Build NPM Project (push) Successful in 38s
Publish Library / Tag Version (push) Successful in 5s
Switching to processes and whisper.cpp to avoid transformers.js memory leaks
2026-02-20 21:50:01 -05:00

24 lines
760 B
TypeScript

import {createWorker} from 'tesseract.js';
import {AbortablePromise, Ai} from './ai.ts';
export class Vision {
constructor(private ai: Ai) {}
/**
* Convert image to text using Optical Character Recognition
* @param {string} path Path to image
* @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);
});
return Object.assign(p, {abort: () => worker?.terminate()});
}
}