feat(ocr): add smartai extraction support
This commit is contained in:
+45
-3
@@ -1,5 +1,6 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as smartocr from '../ts/index.js';
|
||||
import type { ISmartAiOcrEngine } from '@push.rocks/smartai/ocr';
|
||||
|
||||
let testOcrInstance: smartocr.SmartOcr;
|
||||
|
||||
@@ -8,11 +9,52 @@ tap.test('should create a valid instance of Smartocr', async () => {
|
||||
expect(testOcrInstance).toBeInstanceOf(smartocr.SmartOcr);
|
||||
});
|
||||
|
||||
tap.test('should recognize image buffers through SmartAI OCR', async () => {
|
||||
const calls: unknown[] = [];
|
||||
const smartAiOcrEngine: ISmartAiOcrEngine = {
|
||||
recognizeImage: async (input) => {
|
||||
calls.push(input);
|
||||
return {
|
||||
text: 'hello from smartai',
|
||||
confidence: 0.92,
|
||||
pages: [
|
||||
{
|
||||
index: 0,
|
||||
text: 'hello from smartai',
|
||||
confidence: 0.92,
|
||||
},
|
||||
],
|
||||
raw: {
|
||||
pages: [
|
||||
{
|
||||
index: 0,
|
||||
markdown: 'hello from smartai',
|
||||
},
|
||||
],
|
||||
model: 'mock-ocr',
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const result = await testOcrInstance.recognizeImageBufferWithSmartAi(Buffer.from('image data'), {
|
||||
mimeType: 'image/png',
|
||||
smartAiOcrEngine,
|
||||
});
|
||||
|
||||
expect(result.text).toEqual('hello from smartai');
|
||||
expect(result.confidence).toEqual(0.92);
|
||||
expect(calls[0]).toEqual({
|
||||
dataBase64: Buffer.from('image data').toString('base64'),
|
||||
mimeType: 'image/png',
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should ocr a pdfBuffer', async () => {
|
||||
const smartfile = await import('@push.rocks/smartfile');
|
||||
const pdfBuffer = (await smartfile.SmartFile.fromFilePath('./test/demo_without_textlayer.pdf'))
|
||||
.contentBuffer;
|
||||
const resultBuffer = await testOcrInstance.processPdfBuffer(pdfBuffer);
|
||||
const smartfileFactory = smartfile.SmartFileFactory.nodeFs();
|
||||
const pdfFile = await smartfileFactory.fromFilePath('./test/demo_without_textlayer.pdf');
|
||||
const resultBuffer = await testOcrInstance.processPdfBuffer(pdfFile.contentBuffer);
|
||||
console.log(resultBuffer);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user