Guided project · Foundations · NLP

Read a receipt with OCR, then let AI understand it

Turn a picture of a receipt into text with OCR, see where OCR makes mistakes, then use AI to pull out the details and check the totals.

About 60 minIntermediateCopy-paste code
0 of 10 steps done

What you'll build

A mini document reader: image → text (OCR) → structured details (date, items, total) → an automatic check that the numbers add up.

What you'll learn

  • What OCR does, and where it struggles
  • Why OCR plus AI is more useful than OCR alone
  • Pulling fields out of text with simple rules and with AI
  • Checking AI output instead of trusting it

You'll need

Google Colab (free, in your browser), any AI chatbot, and optionally Google Lens on your phone

Create a free account to unlock this project

The step-by-step instructions, copy-ready prompts, progress tracker and portfolio texts are for members. It's free, takes under a minute, and unlocks all guided projects.

Want to try first? These projects are open to everyone: Prompt lab and One idea, five posts.

Before you start

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.

Step by step

  1. Open a notebook

    Go to colab.research.google.com and click New notebook.

    You should see: An empty notebook with one code cell.

  2. Install the OCR engine

    Run this cell. It installs Tesseract, a free OCR engine, inside Colab (about 30 seconds).

    Copy this code
    !apt-get -qq install -y tesseract-ocr > /dev/null
    !pip -q install pytesseract
    print('OCR engine ready')

    You should see: A few lines of output and no red error. Nothing is installed on your own computer.

  3. Make a sample receipt

    This draws a small café receipt as a picture, so we have something safe to read.

    Copy this code
    from PIL import Image, ImageDraw, ImageFont
    from matplotlib import font_manager
    
    font = ImageFont.truetype(font_manager.findfont('DejaVu Sans'), 28)
    lines = ['CHAI POINT CAFE', 'Invoice: INV-2048', 'Date: 27/10/2026',
             'Masala chai x2      120.00', 'Veg sandwich x1     150.00',
             'Samosa x3            90.00', 'TOTAL               360.00', 'Thank you!']
    img = Image.new('RGB', (640, 60 + 44 * len(lines)), 'white')
    draw = ImageDraw.Draw(img)
    for i, line in enumerate(lines):
        draw.text((30, 30 + 44 * i), line, fill='black', font=font)
    img.save('receipt.png')
    img

    You should see: A picture of a receipt appears under the cell.

  4. Read it with OCR

    Ask Tesseract to turn the picture into text.

    Copy this code
    import pytesseract
    
    text = pytesseract.image_to_string(Image.open('receipt.png'))
    print(text)

    You should see: The receipt's text printed out, almost perfectly. To the computer, this was only pixels until now.

    Why it matters: OCR (optical character recognition) recognises the shapes of letters and numbers. It doesn't understand what they mean.

  5. Make it messy

    Real photos are tilted, blurry and small. Damage the picture and read it again.

    Copy this code
    from PIL import ImageFilter
    
    messy = img.rotate(4, expand=True, fillcolor='white').filter(ImageFilter.GaussianBlur(1.4))
    messy = messy.resize((messy.width // 2, messy.height // 2))
    display(messy)
    print(pytesseract.image_to_string(messy))

    You should see: A tilted, blurry receipt, and OCR text that may now have mistakes, e.g. O instead of 0, missing letters or broken lines.

    Why it matters: This is why real systems clean and straighten images first, and why people still check important documents.

  6. Pull out details with simple rules

    Use patterns to find the invoice number, date and total in the clean text.

    Copy this code
    import re
    
    invoice = re.search(r'INV-\d+', text)
    date = re.search(r'\d{2}/\d{2}/\d{4}', text)
    total = re.search(r'TOTAL\s+([\d.]+)', text)
    print('Invoice:', invoice and invoice.group())
    print('Date:', date and date.group())
    print('Total:', total and total.group(1))

    You should see: Three values: INV-2048, 27/10/2026 and 360.00. Rules are fast but break easily when the layout changes.

  7. Let AI understand it

    Copy the OCR text printed in step 4. Open an AI chatbot in a new tab, paste this prompt, and put the OCR text where it says.

    Copy this text
    Below is text read from a receipt by OCR. It may contain OCR mistakes.
    Return only JSON in this format:
    {"shop": "", "invoice": "", "date": "", "items": [{"name": "", "qty": 0, "price": 0}], "total": 0}
    If a value is unclear, use null instead of guessing.
    
    [paste the OCR text here]

    You should see: Neat JSON with the shop, date, each item with quantity and price, and the total. The AI understands what each line means, which OCR alone can't.

  8. Check the AI's work

    Copy the JSON the chatbot gave you, paste it between the triple quotes below (replacing the placeholder line), and run the cell. It checks that the items add up to the total.

    Copy this code
    import json
    
    result = json.loads("""
    paste the JSON here
    """)
    items_sum = sum(item['price'] for item in result['items'])
    print('Items add up to', items_sum, '| receipt total', result['total'])
    print('Totals match ✓' if abs(items_sum - result['total']) < 0.01 else 'Mismatch: check by hand ✗')

    You should see: "Totals match ✓" (120 + 150 + 90 = 360). If they don't match, either OCR or the AI made a mistake, and a person should look.

    Why it matters: Never trust AI-extracted numbers blindly. A simple automatic check catches many errors.

  9. Try the one-step way

    Modern multimodal AI can do OCR and understanding together. Download receipt.png (Files panel on the left → ⋮ next to the file → Download), upload it to ChatGPT, Gemini or Claude, and use the same prompt.

    You should see: Usually the same JSON, straight from the picture. Try the messy version too and compare which approach was more accurate.

  10. Write your conclusion

    Add a text cell (+ Text) with 3 lines: what OCR is good at, what AI adds, and why a human or an automatic check is still needed for invoices, IDs or medical forms.

    You should see: A clear explanation of intelligent document processing that you can use in an interview.

Level up

  • Read a printed page in Hindi: install the language with !apt-get install -y tesseract-ocr-hin and use lang='hin'.
  • Use pytesseract.image_to_data to see how confident the OCR was about each word, and flag the unsure ones.
  • Process three different sample receipts and save the results into a table with pandas.

Lessons behind this project

Show it off

Finished? Post about it on LinkedIn. Here's a starting point; add a screenshot and your own words.

Copy this post
I built a mini document reader today: OCR turns a receipt picture into text, AI pulls out the items and total, and a small check makes sure the numbers add up. OCR reads, AI understands, checks keep it honest! Learned with AI From Zero.
Share on LinkedIn

Add it to your portfolio

Employers love seeing what you've built. Use these ready-made texts, then add your own results and a screenshot.

1. A line for your CV or LinkedIn "Projects" section

Copy for your CV
Read a receipt with OCR, then let AI understand it: A mini document reader: image → text (OCR) → structured details (date, items, total) → an automatic check that the numbers add up. Skills: What OCR does, and where it struggles, Why OCR plus AI is more useful than OCR alone, Pulling fields out of text with simple rules and with AI, Checking AI output instead of trusting it. Tools: Google Colab (free, in your browser), any AI chatbot, and optionally Google Lens on your phone.

2. A README for GitHub

Create a free GitHub account, make a new repository, add a file called README.md and paste this in along with your notebook (File → Download → .ipynb).

Copy README.md
# Read a receipt with OCR, then let AI understand it

Turn a picture of a receipt into text with OCR, see where OCR makes mistakes, then use AI to pull out the details and check the totals.

## What I built
A mini document reader: image → text (OCR) → structured details (date, items, total) → an automatic check that the numbers add up.

## What I learned
- What OCR does, and where it struggles
- Why OCR plus AI is more useful than OCR alone
- Pulling fields out of text with simple rules and with AI
- Checking AI output instead of trusting it

## Tools
Google Colab (free, in your browser), any AI chatbot, and optionally Google Lens on your phone

## How it works
1. Open a notebook
2. Install the OCR engine
3. Make a sample receipt
4. Read it with OCR
5. Make it messy
6. Pull out details with simple rules
7. Let AI understand it
8. Check the AI's work
9. Try the one-step way
10. Write your conclusion

## Results
(Add a screenshot and 2–3 sentences about what you found. Never include API keys or personal data.)

## Next steps
- Read a printed page in Hindi: install the language with !apt-get install -y tesseract-ocr-hin and use lang='hin'.
- Use pytesseract.image_to_data to see how confident the OCR was about each word, and flag the unsure ones.
- Process three different sample receipts and save the results into a table with pandas.

---
Built by following the free guided project at https://learnaifromzero.com/projects/ocr-ai-documents.html