Sub topic 1
What OCR Does and Where It Is Used
Optical character recognition (OCR) is the process of converting text in an image into machine-readable characters. An image of a printed page contains pixels, not letters that a program can search, copy, or calculate with. OCR analyzes those pixels and produces text such as Invoice total: $42.50.
OCR is useful whenever text arrives as a picture instead of as a document with a text layer. Common sources include scanned forms, photographed receipts, identity documents, shipping labels, books, and screenshots.
Prerequisites
This chapter assumes that you know basic Python and can install packages with pip. You should also understand that a digital image is an array of pixels with a width, height, and color channels. You should be comfortable running commands in a terminal.
What OCR Produces
OCR output is usually a string, but a useful OCR system can produce more than plain text. It may also return:
- The location of each word or line in the image.
- A confidence score for each recognized item.
- The reading order of text regions.
- Detected paragraphs, tables, or document fields.
- The language or script identified in the image.
The quality of the output depends on both the image and the OCR engine. A clear scan of a typed page is usually easier than a low-resolution photograph taken at an angle.
| Input image | Possible OCR output |
|---|---|
| Scanned invoice | `Invoice total: $42.50` |
| Street sign photograph | `MAIN STREET` |
| Handwritten note | Incomplete or incorrect text |
| Table with small cells | Text with uncertain reading order |
OCR does not understand an image in the same way a person does. It estimates which shapes represent characters. If two characters look similar, such as O and 0, the engine may choose the wrong one. Later processing must use the surrounding context to detect and correct such errors.
The Basic OCR Pipeline
An OCR pipeline is a sequence of operations that turns pixels into usable text.
- Text detection finds regions that appear to contain text. Some engines assume that the whole image contains one page. Others locate separate text areas in a photograph.
- Text recognition examines each region and predicts characters and words.
- Output generation returns the recognized text, and possibly positions and confidence values.
- Application processing cleans the result, validates important fields, and stores or sends it to another system.
The first two stages are often called detection and recognition. A traditional OCR engine may perform them with image-processing rules and trained character models. A modern deep-learning engine may use neural networks that learn visual patterns from large collections of labeled text images.
The stages are related but not interchangeable. If detection misses a line, recognition never receives that line. If detection is correct but the image is blurry, recognition may still produce incorrect characters. If recognition is accurate but the output is not validated, an application may store a wrong invoice amount.
Where OCR Fits
OCR is often one component in a larger workflow:
- A document archive uses OCR so users can search scanned pages.
- An accounts-payable system extracts invoice numbers and totals.
- A mobile application reads a product label or a meter display.
- A postal system reads addresses to support sorting.
- A data-entry tool converts paper forms into records.
The correct output depends on the use case. Search indexing may tolerate a few character errors. A payment system should reject or review uncertain totals. A navigation application may need text locations and reading order, not just one combined string.
Choosing an OCR Approach
A classic engine such as Tesseract is a practical choice for clean, printed documents and controlled layouts. It can run locally and expose useful configuration options. It may require careful image preparation and layout settings.
A deep-learning OCR engine is often a better fit for natural scene text, unusual fonts, curved signs, difficult backgrounds, or varied layouts. These engines can be more accurate in such cases, but they may require more memory, compute time, or model setup.
Choose based on the text source, accuracy requirement, latency, privacy constraints, and available hardware. Test with representative images rather than relying on a single successful example.
Exercise
Why might an OCR result be unsuitable for automatically approving a payment, even when the returned text looks readable?
Show answer
OCR can confuse similar characters or misread digits. A payment workflow should check confidence, validate the field format and value, and route uncertain results for review.