Skip to content

How OCR works

Bodhi42 edited this page Nov 1, 2026 · 5 revisions

🌐 English · Русский

What happens when you call OCR?

Install 0.5.1 and get the sample files.

Imagine reading an invoice. You find the lines, read the words, then decide which paragraph or column comes next. Occular follows the same broad steps:

  1. Find text. Locate the text lines on the page.
  2. Read it. Turn each line image into characters.
  3. Put it in order. Arrange the lines into readable text.

For a normal scan, all of this happens with one call:

from occular import ocr

print(ocr("receipt.jpg"))

The lines the reader found

Use detection boxes to distinguish a missed line from a recognition mistake. This example disables deskew so boxes can be drawn over the original input:

from occular import ocr_detailed

lines = ocr_detailed("receipt.jpg", deskew=False)
print("Lines found:", len(lines))
for line in lines[:4]:
    print(line["text"])

Actual output:

Lines found: 44
СУПЕРМАРКЕТ «ВОЛГА-МАРКЕТ»
ул. Белинского, 32
ООО «Волга-Ритейл»
ИНН 5260998741

The notebook's second cell draws the first four outlines with Matplotlib, zoomed to the header. All drawing code is visible in the run below.

Four detected lines over a legible source header crop

Detection finds image locations; recognition assigns text to each location. Good boxes can still contain wrong words.

Open the real Jupyter run (all code visible)

Find and read text lines: all notebook code and actual output

Download notebook · receipt.jpg

What do the extra features add?

Feature Example of when it helps
Deskew A scan leans a few degrees
Orientation A photo is sideways or upside down
Dewarp Lines bend around the spine of a book
Reading order A page has two columns
Tables You need to keep invoice items in their cells
Language model Some letters are blurry and word context helps

Layout analysis finds larger areas such as paragraphs, tables and pictures. It helps organize the page. You can also get these areas yourself.

One useful way to debug

A missing line, a misspelled word and mixed-up columns are different problems. Look at the troubleshooting page to choose what to change first.

Clone this wiki locally