Files
ai-utils/src/vision.ts
ztimson 1c59379c7d
All checks were successful
Publish Library / Build NPM Project (push) Successful in 31s
Publish Library / Tag Version (push) Successful in 5s
Set tesseract model
2026-01-16 20:33:51 -05:00

26 lines
781 B
TypeScript

import {createWorker} from 'tesseract.js';
import {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 {{abort: Function, response: Promise<string | null>}} Abort function & Promise of extracted text
*/
ocr(path: string): {abort: () => void, response: Promise<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);
})
}
}
}