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).
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:
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.txtpip install -r requirements.txt.
├── 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
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>data_uncropped/data/Extracts structured fields — store name, date, subtotal, total, and line items — from the preprocessed receipt.
python main.py data/<receipt-filename>data/out/out/json/uvicorn app:app --reloadThis 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.
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" }
]
}# 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.jpgDeveloped by Prajwal Jaiswal