PaperPod/tests/test_ocr_integration.py
ilia 2c28a623fb Add OCR naming and PNG-to-PDF export (modules 5-7, single-page only)
- ocr/: Tesseract-based vendor/date/total extraction. PSM 6 (single
  uniform text block) instead of the default fixes receipts where
  item-name/price columns were otherwise split into separate blocks and
  dropped. Vendor line picked by median (not max) word height within the
  top fraction of the crop, breaking ties by topmost line - max() was
  fooled by single descender/ascender glyphs (commas, parens) inflating
  one word's bounding box.
- naming/: merge OCR metadata into YYYY-MM-DD_vendor.pdf filenames,
  degrading gracefully to UNSORTED_<timestamp>.pdf; per-run dedup.
- pdf/: img2pdf-based multi-page-capable assembly (images_to_pdf).
- pipeline.run_export(): OCR + name + PDF each detected crop from a
  detect() report, writes pdf/ and export_summary.csv.
- New `paperpod export <video>` CLI command.
- Verified against synthetic Home Depot/Metro/Petro-Canada receipts:
  3/3 correct vendor, date, and total after tuning.

Known limitation (documented in README): no page-flip/multi-page
grouping yet (that's events/, still unbuilt) - every detected document
becomes its own single-page PDF.
2026-07-07 18:04:06 -04:00

52 lines
1.3 KiB
Python

"""End-to-end OCR tests against the synthetic receipt renderer.
Skipped automatically if the tesseract binary isn't installed.
"""
from __future__ import annotations
import shutil
import sys
from pathlib import Path
import pytest
pytestmark = pytest.mark.skipif(
shutil.which("tesseract") is None, reason="tesseract binary not installed"
)
sys.path.insert(0, str(Path(__file__).parent.parent / "sample_data"))
def _receipt_image(index: int):
from make_receipt_video import RECEIPTS, make_receipt_image
return make_receipt_image(RECEIPTS[index])
def test_home_depot_receipt_fields():
from paperpod.ocr.extract import run_ocr
result = run_ocr(_receipt_image(0))
assert result.date == "2023-03-14"
assert result.total == "81.52"
assert "HOME DEPOT" in (result.vendor or "")
def test_metro_receipt_fields():
from paperpod.ocr.extract import run_ocr
result = run_ocr(_receipt_image(1))
assert result.date == "2023-06-02"
assert result.total == "30.63"
assert "METRO" in (result.vendor or "")
def test_petro_canada_receipt_fields():
from paperpod.ocr.extract import run_ocr
result = run_ocr(_receipt_image(2))
assert result.date == "2023-08-19"
assert result.total == "65.10"
assert "PETRO" in (result.vendor or "")