Re-organized functions and added semantic embeddings
This commit is contained in:
25
src/vision.ts
Normal file
25
src/vision.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
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('eng');
|
||||
const {data} = await worker.recognize(path);
|
||||
await worker.terminate();
|
||||
res(data.text.trim() || null);
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user