Learning lab · Receipt recognition architecture

Trace one screenshot through the package

Your win for this lesson: explain what reads the image, what chooses the payable amount, what keeps the suggestion safe—and why the package never writes an expense itself.

8–10 minutes One architecture Four retrieval checks Open stack reference Read pipeline report

The big idea

Recognition and persistence are separate responsibilities

payable-receipt-ocr is a local Python module. An application may call it directly or through an adapter, but authentication, expense validation, and persistence remain the application's responsibility.

Consumer · your application

Expense application

Owns people, groups, expenses, authentication, UI, validation, and persistence.

  • Input: an authorized image and currency context
  • Review: the user edits or confirms the suggestion
  • Data: the application owns all persistence

Module · Python package

payable-receipt-ocr

Turns a local screenshot into ranked amount evidence and a confirmation-only suggestion.

  • Image: Pillow, NumPy, OpenCV
  • Reading: Tesseract 5 through pytesseract
  • Decision: Python rules, Decimal, JSON

Optional adapter · not included

Self-hosted HTTP adapter

Can expose the Python interface over HTTP without gaining permission to save expenses.

  • Input: authorized receipt image
  • Output: amount, currency, evidence, warnings
  • Guard: user confirms before app persistence

Interactive trace

Click each stage: image → evidence

The recognizer is a pipeline. Each stage produces a more useful representation, but only the final human confirmation makes the suggested total usable in an expense.

1 · Input is evidence, not an expense

The package accepts JPG, PNG, or WebP. Pillow fixes EXIF orientation and converts the image into a predictable RGB representation. No database record is created here.

2 image variants × 2 language models × 3 layout modes = 12 OCR passes

OpenCV's official guide explains why adaptive thresholding helps when illumination varies; pytesseract's official repository explains that image_to_data returns boxes, confidence, and other recognition metadata. OpenCV source · pytesseract source

Technology roles

Do not confuse the reader with the judge

Pillow + OpenCV prepare pixels

Pillow loads and orients the file. OpenCV converts, deskews, resizes, normalizes, and thresholds it. Neither decides which rupee value is the final payment.

Tesseract reads visible text

Tesseract is the OCR engine. pytesseract calls that executable from Python and returns text, geometry, and confidence. It may read several plausible numbers incorrectly.

Python interprets receipt meaning

Repository code identifies money, recognizes labels, groups agreement across passes, and ranks payment labels above fallback totals. Decimal keeps currency math exact.

The user authorizes the expense

requires_confirmation is always true. Even “strong” means “safe to suggest,” never “safe to silently save.” Caller-owned persistence belongs after confirmation, not before it.

Current versus future

Know which arrows exist

Exists now

Local command flow

image file → recognize(...) → Tesseract subprocesses → typed suggestion

Proposed after holdout

Product integration flow

application → optional adapter → Python package → suggestion → user confirmation → application persistence

Retrieval practice

Close the explanation; recover the model

Answer without scrolling upward. Immediate feedback builds a more durable mental model than rereading alone. Score: 0 / 4

Which component actually recognizes text?

What is the package-to-application relationship?

What happens after OCR suggests a total?

Which three dimensions create 12 OCR passes?

Teach-back drill

Explain the flow in your own words

Imagine a teammate asks, “What happens when we give this system a Zepto screenshot?” Write a short explanation from memory. The checker looks for five architectural ideas, not exact phrasing.

Code map

Where to look when curiosity strikes

Question Source Key location
What is installed? pyproject.toml Dependencies and package metadata
How are pixels prepared? src/payable_receipt_ocr/_engine.py _load_image, _deskew, _preprocess
How is text read? src/payable_receipt_ocr/_engine.py _ocr_lines, process_receipt
How is the payable total chosen? src/payable_receipt_ocr/_engine.py _classify_line, _aggregate_candidates
What makes evidence strong? src/payable_receipt_ocr/_engine.py _evidence_grade, _warnings
What verifies package behavior? tests/test_public_interface.py recognize(...) interface tests

Primary source recommendation

Read one thing next

Tesseract 5 User Manual

Read only the introduction and command-line usage sections. Your goal is to recognize Tesseract as the OCR engine and understand that language data, OCR-engine mode, and page segmentation are explicit inputs—not magic hidden inside Python.