{Project Readme}

Receipt Understanding System

This project implements a complete pipeline for receipt image preprocessing, OCR-based text extraction, and semantic field understanding. It combines traditional computer vision techniques with modern transformer-based models (DocTR and Donut) to detect and extract meaningful information — store name, date, totals, tax, and line items — from real-world receipt images.

The system was developed as the final project for CSCI 731 (Advanced Computer Vision).


Overview

Receipts vary widely in layout, print quality, and lighting conditions, which makes naive text extraction unreliable. This project addresses that by combining two complementary models with a custom matching layer:

  1. Geometric preprocessing locates the receipt in a raw photo, corrects perspective distortion, and enhances contrast so downstream models see a clean, top-down scan.
  2. DocTR performs OCR to recover every word and line on the receipt along with its bounding box, giving precise spatial grounding for the text.
  3. Donut is an end-to-end document understanding transformer that reads the receipt image directly and predicts a structured JSON of the fields it recognizes (store name, totals, line items, etc.), without relying on a separate OCR step.
  4. Field matching reconciles the two outputs — Donut's structured predictions are aligned back onto DocTR's spatially grounded text using fuzzy string matching and positional heuristics, which grounds each predicted field to a bounding box on the original image and lets low-confidence Donut predictions be corrected or confirmed with OCR evidence.

The result is a structured JSON record of the receipt together with an annotated image showing where each extracted field was located.

A lightweight FastAPI service (app.py) is also included so the pipeline can be run as a REST endpoint rather than a CLI script.


Requirements

  • Python 3.8 or higher
  • Dependencies listed in requirements.txt
pip install -r requirements.txt

Project Structure

.
├── preprocessing/
│   └── receipt_scanner.py       # Detects, crops, and straightens the receipt
├── inference/
│   ├── doctr_inference.py       # Runs DocTR OCR on the receipt image
│   └── donut_inference.py       # Loads and runs the Donut model
├── postprocessing/
│   ├── match_fields.py          # Aligns Donut predictions with OCR text/bboxes
│   ├── extract_donut_fields.py  # Parses raw Donut output into fields
│   ├── section_labeling.py      # Labels header / line-item / footer regions
│   ├── draw_boxes.py            # Renders annotated output images
│   └── json_formatter.py        # Simplifies matched fields into final JSON
├── utils/
│   ├── image_utils.py
│   └── text_utils.py
├── classify_receipt_sections.py # Heuristics for classifying OCR lines by section
├── data_uncropped/              # Raw captured receipt images
├── data/                        # Preprocessed (cropped) receipt images
├── out/                         # Annotated images and JSON results
├── main.py                      # CLI entry point for the full pipeline
├── app.py                       # FastAPI service exposing the pipeline as an API
├── config.py
├── requirements.txt
└── README.md

How to Run

Step 1: Scan and Preprocess the Receipt

Detects and crops the receipt from the raw image, then applies perspective correction and contrast enhancement.

python preprocessing/receipt_scanner.py data_uncropped/<receipt-filename>
  • Input: raw receipt image in data_uncropped/
  • Output: cropped, cleaned receipt saved to data/

Step 2: Run the Text Extraction and Understanding Pipeline

Extracts structured fields — store name, date, subtotal, total, and line items — from the preprocessed receipt.

python main.py data/<receipt-filename>
  • Input: cropped receipt image from data/
  • Output:
    • Annotated image with bounding boxes saved to out/
    • Structured JSON result saved to out/json/

Alternative: Run as a REST API

uvicorn app:app --reload

This exposes a POST /extract_receipt endpoint that accepts an uploaded receipt image and returns the Donut output, matched fields, and matched line items as JSON.


Models Used

  • DocTR: Detects words and lines with bounding boxes using a transformer-based OCR model.
  • Donut: An end-to-end document understanding transformer that predicts structured output directly from the image, without an intermediate OCR step.
  • Matching logic: Custom fuzzy matching and spatial heuristics that align Donut's field predictions with DocTR's OCR regions.

Output Format

The final output is returned as structured JSON:

{
  "store_name": "Walmart",
  "date": "04/25/2025",
  "total": "59.06",
  "line_items": [
    { "item_name": "Milk", "item_value": "2.99", "item_quantity": "2" },
    { "item_name": "Bread", "item_value": "1.50", "item_quantity": "1" }
  ]
}

Example Workflow

# Step 1: Crop and preprocess the image
python preprocessing/receipt_scanner.py data_uncropped/walmart_receipt.jpg
 
# Step 2: Run the OCR + semantic extraction pipeline
python main.py data/walmart_receipt.jpg

Future Work

  • A Tkinter-based interactive interface for reviewing and correcting extracted fields visually.
  • Export options to CSV or cloud storage, in addition to the current JSON output.
  • A mobile front end built with React Native, backed by the existing FastAPI service.
  • Broader receipt coverage through additional training data and fine-tuning of the Donut model on more diverse store formats.

Author

Developed by Prajwal Jaiswal