Skip to content

Repository files navigation

πŸš€ ScanX Expense

AI-powered Intelligent Document Processing (IDP) System for Bills & Invoices

Extract structured information from bills, automatically categorize expenses, store them in a database, and interact with your expenses using natural language.


⭐ Overview

Managing receipts manually is time-consuming.

ScanX Expense automates the complete workflow by combining:

  • Computer Vision
  • OCR
  • Local LLMs
  • SQLite
  • Streamlit

The system converts unstructured invoice images into structured records and allows users to chat with their expense database.


🎯 Project Goals

βœ” Process invoices automatically

βœ” Extract structured data

βœ” Categorize expenses

βœ” Store in database

βœ” Query using Natural Language

βœ” Run completely offline using Ollama


✨ Features

πŸ“· Image Processing

  • Image cleaning
  • Noise removal
  • Grayscale conversion
  • Thresholding
  • Batch processing

πŸ” OCR

  • Reads receipts
  • Supports multiple invoices
  • Batch OCR
  • Generates raw text

πŸ€– AI Parsing

Local LLM extracts

  • Invoice Number
  • Vendor
  • Customer
  • Date
  • Amount
  • Total
  • Line Items

Returns structured JSON.


🧠 AI Categorization

Each line item is classified into

  • Food
  • Grocery
  • Travel
  • Logistic
  • Utilities
  • Drinks
  • Other

πŸ’Ύ Database

Stores

  • invoice
  • line items
  • totals
  • categories

using SQLite.


πŸ“Š Dashboard

Interactive Streamlit dashboard

Supports

  • Search
  • Filters
  • Invoice lookup
  • Category lookup

πŸ’¬ Chat with Bills

Ask questions like

How much did I spend on food?

Show invoices from Amazon.

Highest spending category.

The LLM generates SQL automatically.


πŸ— System Architecture

               Bill Images
                    β”‚
                    β–Ό
         Image Preprocessing
              (OpenCV)
                    β”‚
                    β–Ό
              OCR Engine
          (Tesseract OCR)
                    β”‚
                    β–Ό
          Raw OCR Text Output
                    β”‚
                    β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Agent 1 (Ollama)    β”‚
        β”‚ Invoice Extraction  β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
        Structured Invoice JSON
                    β”‚
                    β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Agent 2 (Ollama)    β”‚
        β”‚ Expense Category    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
             SQLite Database
                    β”‚
                    β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Streamlit Dashboard β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Agent 3 (Ollama)    β”‚
        β”‚ NL β†’ SQL            β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
             Chat with Bills

πŸ“‚ Project Structure

ScanX_Expense/

β”‚
β”œβ”€β”€ bill_images/
β”‚
β”œβ”€β”€ image_cleaning.py
β”œβ”€β”€ ocr_processor.py
β”œβ”€β”€ parser.py
β”œβ”€β”€ data_insertion.py
β”œβ”€β”€ table_creation.py
β”‚
β”œβ”€β”€ ollama1.py
β”œβ”€β”€ ollama2.py
β”œβ”€β”€ ollama3.py
β”‚
β”œβ”€β”€ prompt1.py
β”œβ”€β”€ prompt2.py
β”œβ”€β”€ prompt3.py
β”‚
β”œβ”€β”€ frontend1.py
β”œβ”€β”€ frontend2.py
β”‚
β”œβ”€β”€ extracted_text.txt
β”œβ”€β”€ ocr_master.db
β”‚
└── README.md

βš™ Tech Stack

Layer Technology
Language Python
Image Processing OpenCV
OCR Tesseract OCR
LLM Ollama
Model Gemma 3 / Phi-3
Database SQLite
Dashboard Streamlit
Data Pandas

πŸš€ Installation

Clone

git clone https://github.com/<your-username>/ScanX_Expense.git

cd ScanX_Expense

Install Dependencies

pip install -r requirements.txt

Install Tesseract

Download

https://github.com/UB-Mannheim/tesseract/wiki

Update

pytesseract.tesseract_cmd = r"YOUR_PATH"

Install Ollama

ollama pull gemma3:4b

or

ollama pull phi3:3.8b

β–Ά Running the Project

Step 1

Put receipts inside

bill_images/

Step 2

Image Cleaning

python image_cleaning.py

Step 3

OCR

python ocr_processor.py

Step 4

Create Database

python table_creation.py

Step 5

Extract + Categorize + Store

python data_insertion.py

Step 6

Launch Dashboard

streamlit run frontend2.py

πŸ€– AI Agents

Agent 1

Invoice Extraction

Input

OCR Text

Output

{
Invoice_No,
Vendor,
Customer,
Description,
Amount
}

Agent 2

Expense Categorization

Input

Description

Output

Food
Travel

etc.


Agent 3

Natural Language β†’ SQL

Input

How much did I spend on food?

Output

SELECT SUM(amount)

πŸ“Š Database

Table

ocr_line_items

Stores

  • Invoice Number
  • Vendor
  • Customer
  • Amount
  • Category
  • Description
  • Total
  • Source Image

)


⚠ Current Limitations

  • Uses Tesseract OCR, which struggles with noisy or low-quality receipts.
  • OCR accuracy drops on handwritten notes, skewed images, and faded receipts.
  • Image preprocessing is basic and may not handle all receipt conditions.
  • Local LLMs can occasionally return malformed JSON that requires validation.
  • SQLite is suitable for local usage but not for multi-user or production deployments.

πŸš€ Future Roadmap

πŸ” OCR Improvements

  • Replace Tesseract with PaddleOCR
  • Add TrOCR support
  • EasyOCR benchmark
  • Google Vision OCR support
  • Azure Document Intelligence integration

πŸ–Ό Image Processing

  • CLAHE enhancement
  • Deskew receipts automatically
  • Perspective correction
  • Adaptive thresholding
  • Morphological filtering
  • Shadow removal
  • Reflection removal
  • Blur detection
  • Receipt edge detection
  • Auto crop

πŸ€– AI

  • Multi-Agent Workflow
  • LLM Output Validation
  • Retry Strategy
  • JSON Schema Validation
  • Confidence Score
  • Vendor Normalization
  • Duplicate Bill Detection

πŸ“Š Database

  • PostgreSQL support
  • MongoDB support
  • Vector Database
  • Semantic Search
  • Embeddings

πŸ’¬ Chatbot

  • RAG Pipeline
  • Memory
  • Charts
  • Spending Trends
  • Monthly Reports
  • PDF Export
  • Excel Export

🌐 Web App

  • User Authentication
  • Multi-user Support
  • REST API
  • Docker Deployment
  • Cloud Deployment

🀝 Contributions

Contributions are welcome!

If you'd like to improve OCR accuracy, image preprocessing, LLM prompts, or the dashboard, feel free to open an issue or submit a pull request.


πŸ“œ License

MIT License


⭐ If you found this project useful, consider giving it a star on GitHub!

About

AI-powered expense tracker that extracts, categorizes, stores, and lets you chat with bill data using OCR, Ollama, SQLite, and Streamlit.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages