feat(paddleocr): add PaddleOCR OCR service (Docker images, server, tests, docs) and CI workflows
This commit is contained in:
@@ -22,16 +22,11 @@ interface IInvoice {
|
||||
* Extract OCR text from an image using PaddleOCR
|
||||
*/
|
||||
async function extractOcrText(imageBase64: string): Promise<string> {
|
||||
const formData = new FormData();
|
||||
const imageBuffer = Buffer.from(imageBase64, 'base64');
|
||||
const blob = new Blob([imageBuffer], { type: 'image/png' });
|
||||
formData.append('img', blob, 'image.png');
|
||||
formData.append('outtype', 'json');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${PADDLEOCR_URL}/ocr`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ image: imageBase64 }),
|
||||
});
|
||||
|
||||
if (!response.ok) return '';
|
||||
@@ -180,29 +175,64 @@ function hashInvoice(invoice: IInvoice): string {
|
||||
|
||||
/**
|
||||
* Extract with majority voting - run until 2 passes match
|
||||
* Optimization: Run Pass 1, OCR, and Pass 2 (after OCR) in parallel
|
||||
*/
|
||||
async function extractWithConsensus(images: string[], invoiceName: string, maxPasses: number = 5): Promise<IInvoice> {
|
||||
const results: Array<{ invoice: IInvoice; hash: string }> = [];
|
||||
const hashCounts: Map<string, number> = new Map();
|
||||
|
||||
// Extract OCR text from first page
|
||||
const ocrText = await extractOcrText(images[0]);
|
||||
if (ocrText) {
|
||||
console.log(` [OCR] Extracted ${ocrText.split('\n').length} text lines`);
|
||||
const addResult = (invoice: IInvoice, passLabel: string): number => {
|
||||
const hash = hashInvoice(invoice);
|
||||
results.push({ invoice, hash });
|
||||
hashCounts.set(hash, (hashCounts.get(hash) || 0) + 1);
|
||||
console.log(` [${passLabel}] ${invoice.invoice_number} | ${invoice.invoice_date} | ${invoice.total_amount} ${invoice.currency}`);
|
||||
return hashCounts.get(hash)!;
|
||||
};
|
||||
|
||||
// OPTIMIZATION: Run Pass 1 (no OCR) in parallel with OCR -> Pass 2 (with OCR)
|
||||
let ocrText = '';
|
||||
const pass1Promise = extractOnce(images, 1, '').catch((err) => ({ error: err }));
|
||||
|
||||
// OCR then immediately Pass 2
|
||||
const ocrThenPass2Promise = (async () => {
|
||||
ocrText = await extractOcrText(images[0]);
|
||||
if (ocrText) {
|
||||
console.log(` [OCR] Extracted ${ocrText.split('\n').length} text lines`);
|
||||
}
|
||||
return extractOnce(images, 2, ocrText).catch((err) => ({ error: err }));
|
||||
})();
|
||||
|
||||
// Wait for both to complete
|
||||
const [pass1Result, pass2Result] = await Promise.all([pass1Promise, ocrThenPass2Promise]);
|
||||
|
||||
// Process Pass 1 result
|
||||
if ('error' in pass1Result) {
|
||||
console.log(` [Pass 1] Error: ${(pass1Result as {error: unknown}).error}`);
|
||||
} else {
|
||||
const count = addResult(pass1Result as IInvoice, 'Pass 1');
|
||||
if (count >= 2) {
|
||||
console.log(` [Consensus] Reached after parallel passes`);
|
||||
return pass1Result as IInvoice;
|
||||
}
|
||||
}
|
||||
|
||||
for (let pass = 1; pass <= maxPasses; pass++) {
|
||||
// Process Pass 2 result
|
||||
if ('error' in pass2Result) {
|
||||
console.log(` [Pass 2+OCR] Error: ${(pass2Result as {error: unknown}).error}`);
|
||||
} else {
|
||||
const count = addResult(pass2Result as IInvoice, 'Pass 2+OCR');
|
||||
if (count >= 2) {
|
||||
console.log(` [Consensus] Reached after parallel passes`);
|
||||
return pass2Result as IInvoice;
|
||||
}
|
||||
}
|
||||
|
||||
// Continue with passes 3+ using OCR text if no consensus yet
|
||||
for (let pass = 3; pass <= maxPasses; pass++) {
|
||||
try {
|
||||
const invoice = await extractOnce(images, pass, ocrText);
|
||||
const hash = hashInvoice(invoice);
|
||||
const count = addResult(invoice, `Pass ${pass}+OCR`);
|
||||
|
||||
results.push({ invoice, hash });
|
||||
hashCounts.set(hash, (hashCounts.get(hash) || 0) + 1);
|
||||
|
||||
console.log(` [Pass ${pass}] ${invoice.invoice_number} | ${invoice.invoice_date} | ${invoice.total_amount} ${invoice.currency}`);
|
||||
|
||||
// Check if we have consensus (2+ matching)
|
||||
const count = hashCounts.get(hash)!;
|
||||
if (count >= 2) {
|
||||
console.log(` [Consensus] Reached after ${pass} passes`);
|
||||
return invoice;
|
||||
@@ -267,6 +297,7 @@ function compareInvoice(
|
||||
|
||||
/**
|
||||
* Find all test cases (PDF + JSON pairs) in .nogit/invoices/
|
||||
* Priority invoices (like vodafone) run first for quick feedback
|
||||
*/
|
||||
function findTestCases(): Array<{ name: string; pdfPath: string; jsonPath: string }> {
|
||||
const testDir = path.join(process.cwd(), '.nogit/invoices');
|
||||
@@ -290,6 +321,22 @@ function findTestCases(): Array<{ name: string; pdfPath: string; jsonPath: strin
|
||||
}
|
||||
}
|
||||
|
||||
// Sort with priority invoices first, then alphabetically
|
||||
const priorityPrefixes = ['vodafone'];
|
||||
testCases.sort((a, b) => {
|
||||
const aPriority = priorityPrefixes.findIndex((p) => a.name.startsWith(p));
|
||||
const bPriority = priorityPrefixes.findIndex((p) => b.name.startsWith(p));
|
||||
|
||||
// Both have priority - sort by priority order
|
||||
if (aPriority >= 0 && bPriority >= 0) return aPriority - bPriority;
|
||||
// Only a has priority - a comes first
|
||||
if (aPriority >= 0) return -1;
|
||||
// Only b has priority - b comes first
|
||||
if (bPriority >= 0) return 1;
|
||||
// Neither has priority - alphabetical
|
||||
return a.name.localeCompare(b.name);
|
||||
});
|
||||
|
||||
return testCases;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user