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} Promise of extracted text with abort method */ ocr(path: string): AbortablePromise { let worker: any; const p = 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); }); return Object.assign(p, {abort: () => worker?.terminate()}); } }